FHIR Resources as Model Classes - Python classes for all FHIR resources with validation and serialization
—
Comprehensive set of FHIR primitive and complex data types with full validation according to FHIR specification. These types form the building blocks for all FHIR resources and ensure data integrity and interoperability.
Basic data types representing single values with FHIR-specific validation rules and constraints.
# Boolean type
class Boolean:
"""FHIR boolean - true or false values"""
# String types
class String:
"""Unicode string with FHIR length and character constraints"""
class Code:
"""Coded value from a terminology system"""
class Id:
"""Logical identifier for FHIR resources and elements"""
class Uri:
"""Uniform Resource Identifier"""
class Url:
"""Uniform Resource Locator"""
class Oid:
"""Object Identifier (dot-separated numeric identifier)"""
class Uuid:
"""Universally Unique Identifier"""
class Markdown:
"""Markdown-formatted text content"""
class Base64Binary:
"""Base64 encoded binary data"""
# Numeric types
class Decimal:
"""Decimal number with arbitrary precision"""
class Integer:
"""32-bit signed integer"""
class UnsignedInt:
"""Unsigned 32-bit integer (0 or positive)"""
class PositiveInt:
"""Positive integer (> 0)"""
# Date/time types
class Date:
"""Date in YYYY-MM-DD format"""
class DateTime:
"""Date and time with optional timezone"""
class Instant:
"""Precise timestamp in ISO 8601 format"""
class Time:
"""Time of day in HH:MM:SS format"""Usage Examples:
from fhir.resources.patient import Patient
from fhir.resources.datetime import DateTime
# Primitive types are used within resources
patient = Patient(
active=True, # Boolean
id="patient-123", # Id
birthDate="1990-01-15" # Date
)
# DateTime with timezone
from datetime import datetime
now = datetime.now().isoformat()Structured data types containing multiple elements that represent complex healthcare concepts.
class HumanName:
"""Person name with components and use context"""
def __init__(
self,
use: Optional[str] = None, # usual | official | temp | nickname | anonymous | old | maiden
text: Optional[str] = None,
family: Optional[str] = None,
given: Optional[List[str]] = None,
prefix: Optional[List[str]] = None,
suffix: Optional[List[str]] = None,
period: Optional["Period"] = None
): ...
class Address:
"""Physical/postal address"""
def __init__(
self,
use: Optional[str] = None, # home | work | temp | old | billing
type: Optional[str] = None, # postal | physical | both
text: Optional[str] = None,
line: Optional[List[str]] = None,
city: Optional[str] = None,
district: Optional[str] = None,
state: Optional[str] = None,
postalCode: Optional[str] = None,
country: Optional[str] = None,
period: Optional["Period"] = None
): ...
class ContactPoint:
"""Phone, email, or other contact method"""
def __init__(
self,
system: Optional[str] = None, # phone | fax | email | pager | url | sms | other
value: Optional[str] = None,
use: Optional[str] = None, # home | work | temp | old | mobile
rank: Optional[int] = None,
period: Optional["Period"] = None
): ...
class Identifier:
"""Business identifier for resources"""
def __init__(
self,
use: Optional[str] = None, # usual | official | temp | secondary | old
type: Optional["CodeableConcept"] = None,
system: Optional[str] = None,
value: Optional[str] = None,
period: Optional["Period"] = None,
assigner: Optional["Reference"] = None
): ...
class CodeableConcept:
"""Coded concept with text"""
def __init__(
self,
coding: Optional[List["Coding"]] = None,
text: Optional[str] = None
): ...
class Coding:
"""Code from a terminology system"""
def __init__(
self,
system: Optional[str] = None,
version: Optional[str] = None,
code: Optional[str] = None,
display: Optional[str] = None,
userSelected: Optional[bool] = None
): ...
class Quantity:
"""Measured amount with units"""
def __init__(
self,
value: Optional[float] = None,
comparator: Optional[str] = None, # < | <= | >= | >
unit: Optional[str] = None,
system: Optional[str] = None,
code: Optional[str] = None
): ...
class Period:
"""Time period with start and end"""
def __init__(
self,
start: Optional[str] = None, # DateTime
end: Optional[str] = None # DateTime
): ...
class Range:
"""Range with low and high values"""
def __init__(
self,
low: Optional["Quantity"] = None,
high: Optional["Quantity"] = None
): ...
class Ratio:
"""Ratio of two quantities"""
def __init__(
self,
numerator: Optional["Quantity"] = None,
denominator: Optional["Quantity"] = None
): ...
class Attachment:
"""File attachment with metadata"""
def __init__(
self,
contentType: Optional[str] = None,
language: Optional[str] = None,
data: Optional[bytes] = None,
url: Optional[str] = None,
size: Optional[int] = None,
hash: Optional[bytes] = None,
title: Optional[str] = None,
creation: Optional[str] = None # DateTime
): ...
class Reference:
"""Reference to another resource"""
def __init__(
self,
reference: Optional[str] = None,
type: Optional[str] = None,
identifier: Optional["Identifier"] = None,
display: Optional[str] = None
): ...
class Annotation:
"""Text note with author attribution"""
def __init__(
self,
authorReference: Optional["Reference"] = None,
authorString: Optional[str] = None,
time: Optional[str] = None, # DateTime
text: str
): ...Usage Examples:
from fhir.resources.patient import Patient
from fhir.resources.humanname import HumanName
from fhir.resources.address import Address
from fhir.resources.contactpoint import ContactPoint
from fhir.resources.identifier import Identifier
# Create a patient with complex data types
patient = Patient(
identifier=[
Identifier(
use="usual",
system="http://hospital.example.org/patients",
value="MRN-123456"
)
],
name=[
HumanName(
use="official",
family="Doe",
given=["John", "Michael"],
prefix=["Mr."]
)
],
address=[
Address(
use="home",
type="postal",
line=["123 Main Street", "Apt 4B"],
city="Springfield",
state="IL",
postalCode="62701",
country="US"
)
],
telecom=[
ContactPoint(
system="phone",
value="555-123-4567",
use="home"
),
ContactPoint(
system="email",
value="john.doe@example.com",
use="home"
)
]
)Advanced data types for specific clinical and administrative purposes.
class Dosage:
"""Medication dosage instructions"""
def __init__(
self,
sequence: Optional[int] = None,
text: Optional[str] = None,
additionalInstruction: Optional[List["CodeableConcept"]] = None,
patientInstruction: Optional[str] = None,
timing: Optional["Timing"] = None,
asNeededBoolean: Optional[bool] = None,
asNeededCodeableConcept: Optional["CodeableConcept"] = None,
site: Optional["CodeableConcept"] = None,
route: Optional["CodeableConcept"] = None,
method: Optional["CodeableConcept"] = None,
doseAndRate: Optional[List["DosageDoseAndRate"]] = None
): ...
class Timing:
"""Timing schedule for events"""
def __init__(
self,
event: Optional[List[str]] = None, # List of DateTime
repeat: Optional["TimingRepeat"] = None,
code: Optional["CodeableConcept"] = None
): ...
class Money:
"""Monetary amount with currency"""
def __init__(
self,
value: Optional[float] = None,
currency: Optional[str] = None # 3-letter ISO 4217 currency code
): ...
class Signature:
"""Digital signature"""
def __init__(
self,
type: List["Coding"],
when: str, # Instant
who: "Reference",
onBehalfOf: Optional["Reference"] = None,
targetFormat: Optional[str] = None,
sigFormat: Optional[str] = None,
data: Optional[bytes] = None
): ...Structural elements that form the backbone of resource definitions.
class Extension:
"""FHIR extension mechanism"""
def __init__(
self,
url: str,
valueAddress: Optional["Address"] = None,
valueAge: Optional["Age"] = None,
valueAnnotation: Optional["Annotation"] = None,
valueAttachment: Optional["Attachment"] = None,
valueBase64Binary: Optional[bytes] = None,
valueBoolean: Optional[bool] = None,
valueCanonical: Optional[str] = None,
valueCode: Optional[str] = None,
# ... many more value[x] options
extension: Optional[List["Extension"]] = None
): ...
class Meta:
"""Resource metadata"""
def __init__(
self,
versionId: Optional[str] = None,
lastUpdated: Optional[str] = None, # Instant
source: Optional[str] = None,
profile: Optional[List[str]] = None,
security: Optional[List["Coding"]] = None,
tag: Optional[List["Coding"]] = None
): ...
class Narrative:
"""Human-readable narrative"""
def __init__(
self,
status: str, # generated | extensions | additional | empty
div: str # XHTML content
): ...Install with Tessl CLI
npx tessl i tessl/pypi-fhir--resources