Stream: python
Topic: simple python app to read and set some properties
Jose Costa Teixeira (Mar 19 2021 at 07:07):
Hi. I am thinking of making a simple script that reads a few variables from FHIR resources, whether they are xml or json.
The variables are only 3 or 4.
I'm wondering if the existing FHIR libraries will allow me to abstract from all that, and simply pass the fhirpath and let the library do the rest.
So instead of a
if (is_xml):
var1 = resourcexml.findall("./path/to/element]")
elif (is_json):
var1 = resourcejson[path][to][element]
I would like to have a
var1 = read(resource, path_to_element)
Jose Costa Teixeira (Mar 19 2021 at 07:08):
(don't really mind the syntax.)
Jose Costa Teixeira (Mar 19 2021 at 07:08):
Is that available in the current libraries?
Jose Costa Teixeira (Mar 19 2021 at 07:08):
I can do this manually but if I can reuse something, that would be better
Jose Costa Teixeira (Mar 19 2021 at 21:13):
anyone?
Jose Costa Teixeira (Mar 19 2021 at 21:13):
Can i use one of the libraries to just get values from resource files using a FHIR path expression?
Eric Haas (Mar 20 2021 at 20:24):
I use the libraries but never use fhirpath... I just grab value directly as a class attribute. or using get() method.on a dicf if not using FHIR models. maybe @Ilya Beda has python fhirpath engine.
Ilya Beda (Mar 21 2021 at 18:54):
Hi @Jose Costa Teixeira
Here is a FHIRPath implementation in python https://github.com/beda-software/fhirpath-py
https://github.com/beda-software/fhir-py only supports JSON.
Eric Haas (Mar 25 2021 at 02:05):
@Md Nazrul Islam I noticed that the dict form date and datetime uses the datetime class..
e.g ;
my_patient = {'id': 'subject1',
'meta': {'profile': ['http://hl7.org/fhir/us/core/StructureDefinition/us-core-patient|4.0.0']},
'active': True,
'address': [{'city': 'Anytown',
'country': 'USA',
'line': ['100 Main St'],
'postalCode': '99999',
'state': 'CA',
'text': '100 Main St\nAnytown, CA 99999\nUSA'}],
'birthDate': datetime.date(1964, 6, 19), #<<<<<<<<<<<< datetime object <<<<<<<<<<<<<<<<<<<<<<
'communication': [{'language': {'coding': [{'code': 'en',
'display': 'English',
'system': 'urn:ietf:bcp:47'}],
'text': 'English'}}],
'gender': 'male',
'identifier': [{'system': 'http://example.org/pids', 'value': '1234'}],
'name': [{'family': 'Doe', 'given': ['John', 'M']}],
'resourceType': 'Patient'}
Eric Haas (Mar 25 2021 at 02:12):
I think this should be documented because the natural inclination would be to use a string "1964-06-19"
Eric Haas (Mar 25 2021 at 02:14):
Do you have a sample using this + FastAPI?
Eric Haas (Mar 25 2021 at 02:29):
also the parse_raw method is choking on the json div element escape char "\" ...
e.g.
{
"resourceType": "Provenance",
"id": "example",
"text": {
"status": "generated",
"div": "<div xmlns=\"http://www.w3.org/1999/xhtml\">procedure record authored on 27-June 2015 by Harold Hippocrates, MD Content extracted from XDS managed CDA Referral received 26-June as authorized by a referenced Consent.</div>"
},
....
give this error....
ValidationError: 1 validation error for Provenance
__root__
Expecting ',' delimiter: line 6 column 25 (char 116) (type=value_error.jsondecode; msg=Expecting ',' delimiter;
Eric Haas (Mar 25 2021 at 02:33):
double backslashes work. may need a CR ...right?
Md Nazrul Islam (Mar 25 2021 at 04:38):
Eric Haas said:
Md Nazrul Islam I noticed that the dict form date and datetime uses the datetime class..
e.g ;
my_patient = {'id': 'subject1', 'meta': {'profile': ['http://hl7.org/fhir/us/core/StructureDefinition/us-core-patient|4.0.0']}, 'active': True, 'address': [{'city': 'Anytown', 'country': 'USA', 'line': ['100 Main St'], 'postalCode': '99999', 'state': 'CA', 'text': '100 Main St\nAnytown, CA 99999\nUSA'}], 'birthDate': datetime.date(1964, 6, 19), #<<<<<<<<<<<< datetime object <<<<<<<<<<<<<<<<<<<<<< 'communication': [{'language': {'coding': [{'code': 'en', 'display': 'English', 'system': 'urn:ietf:bcp:47'}], 'text': 'English'}}], 'gender': 'male', 'identifier': [{'system': 'http://example.org/pids', 'value': '1234'}], 'name': [{'family': 'Doe', 'given': ['John', 'M']}], 'resourceType': 'Patient'}
'BirthDate' is FHIR Date primitive type that should represent python's date object normally (unless full date is not provided, for example only year).
Pydantic keep the original data type for dict()
method but when you will user json()
method that value should be a string as you described.
The issue with div value
You are welcome to add issue here https://github.com/nazrulworld/fhir.resources/issues
Last updated: Apr 12 2022 at 19:14 UTC