FHIR Resources as Model Classes - Python classes for all FHIR resources with validation and serialization
—
Essential utilities for dynamic FHIR resource creation and manipulation. These functions enable flexible resource handling without requiring hardcoded resource types, making them ideal for dynamic applications and generic FHIR processing.
Get FHIR model classes dynamically by name, supporting all FHIR resource types and data types across all supported versions.
def get_fhir_model_class(model_name: str) -> Type[FHIRAbstractModel]:
"""
Get FHIR model class by resource or element name.
Parameters:
- model_name: str - Name of FHIR resource or element type (e.g., "Patient", "Observation", "HumanName")
Returns:
Type[FHIRAbstractModel] - The corresponding FHIR model class
Raises:
KeyError - If model_name is not a valid FHIR element type
"""Usage Example:
from fhir.resources import get_fhir_model_class
# Get Patient class dynamically
PatientClass = get_fhir_model_class("Patient")
patient = PatientClass(active=True)
# Get complex data type class
HumanNameClass = get_fhir_model_class("HumanName")
name = HumanNameClass(family="Doe", given=["John"])
# Handle invalid types
try:
InvalidClass = get_fhir_model_class("InvalidType")
except KeyError as e:
print(f"Invalid FHIR type: {e}")Construct FHIR elements from various data formats including dictionaries, JSON strings, raw bytes, or file paths.
def construct_fhir_element(
element_type: str,
data: Union[Dict[str, Any], str, bytes, Path]
) -> FHIRAbstractModel:
"""
Construct FHIR element from various data formats.
Parameters:
- element_type: str - FHIR resource or element type name
- data: Union[Dict, str, bytes, Path] - Source data in various formats
Returns:
FHIRAbstractModel - Instantiated and validated FHIR element
Raises:
LookupError - If element_type is not a valid FHIR element type
ValidationError - If data doesn't conform to FHIR specification
"""Usage Examples:
from fhir.resources import construct_fhir_element
from pathlib import Path
# From dictionary
patient_dict = {
"resourceType": "Patient",
"id": "123",
"active": True,
"name": [{"family": "Doe", "given": ["John"]}]
}
patient = construct_fhir_element("Patient", patient_dict)
# From JSON string
json_data = '{"resourceType": "Patient", "active": true}'
patient = construct_fhir_element("Patient", json_data)
# From bytes
json_bytes = b'{"resourceType": "Patient", "active": true}'
patient = construct_fhir_element("Patient", json_bytes)
# From file path
file_path = Path("patient.json")
patient = construct_fhir_element("Patient", file_path)
# Handle invalid element types
try:
invalid = construct_fhir_element("InvalidType", {"test": "data"})
except LookupError as e:
print(f"Invalid element type: {e}")from typing import Any, Dict, Union, Type
from pathlib import Path
from fhir.resources.core.fhirabstractmodel import FHIRAbstractModel
# Type aliases used in function signatures
DataInput = Union[Dict[str, Any], str, bytes, Path]
ModelClass = Type[FHIRAbstractModel]Install with Tessl CLI
npx tessl i tessl/pypi-fhir--resources