Package: py-fhir
py-fhirFHIR Resources/Elements for Python. Code generated bypy-fhir-codegen.IntroductionThis python package provides a python version of the Resources and Elements specified in theFHIR specification. Additionally, the package includes first steps towards database persistance (usingSQLAlchemy) and a client.How to use Resources and DataTypesRunning the following code creates aPatientand assigns values to different attributes. Finally, the object is serialized to JSON representation and the result is printed.# Import the relevant classes from fhir.modelfromfhir.modelimportPatient,HumanName,Identifier,CodeableConcept,Coding,uri# Create a new Patient object. Resource attributes can be passed in the constructor. Note# that 'id' is the logical id of the Resource and has no relation to the Medical Record# Number (MRN)p=Patient(id='patient1')# Give the patient an MRNidentifier=Identifier(type=CodeableConcept(coding=[Coding(system="http://hl7.org/fhir/v2/0203",code="MR")]),system='urn:oid:1.2.36.146.595.217.0.1',value='123456789')# Lists can be assigned to attributes.# Alternatively p.identifier.append(identifier) could have been used.p.identifier=[identifier]# Native python values are automatically coerced to FHIR classesp.active=Trueprint(type(p.active))# output: <class 'fhir.model._boolean.boolean'>name=HumanName()name.use='official'name.given=['Melle','Sjoerd']name.family='Sieswerda'p.name=[name]# Serialize to JSON and print the result. To serialize to XML use 'p.toXML()'.print(p.dumps('json'))This should yield the following output:{"resourceType":"Patient","id":"patient1","identifier":[{"type":{"coding":[{"system":"http://hl7.org/fhir/v2/0203","code":"MR"}]},"system":"urn:oid:1.2.36.146.595.217.0.1","value":"123456789"}],"active":true,"name":[{"use":"official","family":["Sieswerda"],"given":["Melle","Sjoerd"]}]}It is also possible to marshall an object from JSON (or XML) representation:jsonstring=p.dumps('json')p2=Patient.loads(jsonstring,'json')p2.id==p.id# output: TrueUsing the clientimportfhir.modelimportfhir.clientfromfhir.modelimportPatient,HumanNameURL=fhir.client.SERVERS['spark']client=fhir.client.Client(URL)client.set_properties(fhir.model.Resource)# The following is equivalent to# bundle = client.query('Patient')bundle=fhir.model.Patient.query()# Print the first result we receivedforpinbundle:print(p.dumps('json'))break# Create a new patient on the serverp=Patient()p.active=Truename=HumanName(use='official',given=['Melle'],family='Testpatient')p.name=[name]p.save()print('The server assigned id "{}"'.format(p.id))Using persistant storageCave: the current implementation should be considered a stub.
Searching/querying, updating, etc. is not (yet) supported.fromfhir.persistanceimportFHIRStoreimportfhir.model# By default FHIRStore creates a sqlite database in the current directory.# Still, be careful when using 'drop_all'!store=FHIRStore(drop_all=True)# Create a new Patientp=fhir.model.Patient()p.id='patient1'p.active=Truename=fhir.model.HumanName(use='official',given=['Melle','Sjoerd'],family='Sieswerda')p.name=[name]# Store the patientstore.post(p)# Retrieve the patient from the store and print the result.print(store.get('patient1'))# output: <fhir.model.patient.Patient object at 0x1096dd828>