Open Healthedata1 opened 3 years ago
We should have a class method available.
>>> from fhir.resources.observation import Observation
>>> Observation.get_resource_type()
Thanks but it does not work for the elements...
1 from fhir.resources.observation import Observation
----> 2 Observation.code.get_resource_type
AttributeError: type object 'Observation' has no attribute 'code'
@Healthedata1 that is something not possible, unfortunately.
here is what I did:
element_type = next(i.type_.__resource_type__ for i in resource.element_properties() if i.name == element)
For my application, I wished to search a FHIR resource for objects of type Coding, and return the list of all Coding relevant to the resource.
For any who follow, I used a different approach:
from fhir.resources.coding import Coding
def reflect_get_coding(fhir):
response_objects = []
if not isinstance(fhir, str):
if isinstance(fhir, Coding):
response_objects.append(fhir)
elif isinstance(fhir, Iterable):
for item in fhir:
response_objects.extend(reflect_get_coding(item))
elif hasattr(fhir, '__dict__'):
for prop, value in vars(fhir).items():
if not prop.startswith("_"):
response_objects.extend(reflect_get_coding(value))
return response_objects
It would be awesome if there were a clearer story for using reflection in nested fhir.resources objects.
Hi @theGOTOguy , thanks! That helped me a lot. I use it to translate Coding.displays in all my resources examples. :) And +1 for having something that is clearer to filter on element types.
I had a hard time finding if an element is a List - in case anyone else runs into it, this works:
from typing import get_origin
get_origin(parent.__fields__[element_name].annotation) is list
Suggestions for improvement welcome.
Description
Describe what you were trying to get done.
I am trying to discover the simplest and most direct way to discover the type for an element:
ideally this :
obviously this isn't going to work on this instance but don't know the best way to do this on the class
What I Did