/fhir.resources

FHIR resources Release R5

Primary LanguagePythonOtherNOASSERTION

FHIR® Resources (Release R5)

HL7® FHIR®

Powered by pydantic, all FHIR Resources are available as python class with built-in data validation, faster in performance and optionally orjson support has been included as a performance booster! Written in modern python.

  • Easy to construct, easy to extended validation, easy to export.
  • By inheriting behaviour from pydantic, compatible with ORM.
  • Full support of FHIR® Extensibility for Primitive Data Types are available.
  • Preview release of the next FHIR version.
  • Free software: BSD license

Package Origin

Forked from https://github.com/nazrulworld/fhir.resources, recreated to only include FHIR Release R5 resources.

FHIR® Version Info

FHIR® (Release R5, version 5.0.0) - https://hl7.org/fhir/R5/

Installation

Install from github:

$ pip install git+https://github.com/glichtner/fhir.resources.git

FHIR class creation

The classes in this package were created using the fhir-parser package, cloned from https://github.com/glichtner/fhir-parser.git. To create the classes yourself, clone fhir-parser and run generate.py:

$ git clone https://github.com/glichtner/fhir-parser.git
$ cd fhir-parser
$ ./generate.py --fhir-release R5
$ mv generated/* <path-to-fhir.resources>

Usages

Example: 1: Construct Resource Model object

from fhir.resources.organization import Organization
from fhir.resources.address import Address

data = {
    "id": "f001",
    "active": True,
    "name": "Acme Corporation",
    "address": [{"country": "Switzerland"}]
}
org = Organization(**data)
org.resource_type == "Organization"
# True

isinstance(org.address[0], Address)
# True

org.address[0].country == "Switzerland"
# True

org.dict()['active'] is True
# True

Example: 2: Resource object created from json string

from fhir.resources.organization import Organization
from fhir.resources.address import Address

json_str = '''{"resourceType": "Organization",
    "id": "f001",
    "active": True,
    "name": "Acme Corporation",
    "address": [{"country": "Switzerland"}]
}'''

org = Organization.parse_raw(json_str)
isinstance(org.address[0], Address)
# True

org.address[0].country == "Switzerland"
# True

org.dict()['active'] is True
# True

Example: 3: Resource object created from json object(py dict)

from fhir.resources.patient import Patient
from fhir.resources.humanname import HumanName
from datetime import date

json_obj = {"resourceType": "Patient",
    "id": "p001",
    "active": True,
    "name": [
        {"text": "Adam Smith"}
     ],
    "birthDate": "1985-06-12"
}

pat = Patient.parse_obj(json_obj)
isinstance(pat.name[0], HumanName)
# True

org.birthDate == date(year=1985, month=6, day=12)
# True

org.active is True
# True

Example: 4: Construct Resource object from json file

from fhir.resources.patient import Patient
import os
import pathlib

filename = pathlib.Path("foo/bar.json")
pat = Patient.parse_file(filename)
pat.resource_type == "Patient"
# True

Example: 5: Construct resource object in python way

from fhir.resources.organization import Organization
from fhir.resources.address import Address

json_obj = {"resourceType": "Organization",
    "id": "f001",
    "active": True,
    "name": "Acme Corporation",
    "address": [{"country": "Switzerland"}]
}

org = Organization.construct()
org.id = "f001"
org.active = True
org.name = "Acme Corporation"
org.address = list()
address = Address.construct()
address.country = "Switzerland"
org.address.append(address)
org.dict() == json_obj
# True

Note

Please note that due to the way the validation works, you will run into issues if you are using construct() to create resources that have more than one mandatory field. See this comment in issue#56 for details.

Example: 6: Using Resource Factory Function

from fhir.resources import construct_fhir_element

json_dict = {"resourceType": "Organization",
    "id": "mmanu",
    "active": True,
    "name": "Acme Corporation",
    "address": [{"country": "Switzerland"}]
}
org = construct_fhir_element('Organization', json_dict)
org.address[0].country == "Switzerland"
# True

org.dict()['active'] is True
# True

Example: 7: Auto validation while providing wrong datatype

try:
    org = Organization({"id": "fmk", "address": ["i am wrong type"]})
    raise AssertionError("Code should not come here")
except ValueError:
    pass

More Information

For more information and usages, please visit the original repository of the package: https://github.com/nazrulworld/fhir.resources

Credits

This repository just applied the code from nazrulworld to create python classes from the FHIR Release R5 Preview#3 build (with a few tweaks to make it run with FHIR R5).

All FHIR® Resources (python classes) are generated using fhir-parser which is forked from https://github.com/smart-on-fhir/fhir-parser.git.

© Copyright HL7® logo, FHIR® logo and the flaming fire are registered trademarks owned by Health Level Seven International