Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: best way in code to parse a variety of resources? #106

Open
ArdonToonstra opened this issue Jul 15, 2022 · 1 comment
Open

Question: best way in code to parse a variety of resources? #106

ArdonToonstra opened this issue Jul 15, 2022 · 1 comment

Comments

@ArdonToonstra
Copy link

ArdonToonstra commented Jul 15, 2022

  • fhir.resources version: 6.4.0
  • Python version: 3.7.3
  • Operating System: Windows

Description

How to efficiently parse resource instances into their suitable python classes?

What I Did

I have a bunch of XML instances that I am trying to parse. I got it working by first checking the XML root and then parsing it to the correct class like the snippet below. This, however, results in a lot of lines of code. Is there a more efficient way?

if root.tag == '{http://hl7.org/fhir}AllergyIntolerance':
        resource = AllergyIntolerance.parse_file(f)
    elif root.tag == '{http://hl7.org/fhir}CarePlan': 
        resource = CarePlan.parse_file(f)
    elif root.tag == '{http://hl7.org/fhir}Condition': 
        resource = Condition.parse_file(f)
    elif root.tag == '{http://hl7.org/fhir}Consent': 
        resource = Consent.parse_file(f)
@ArdonToonstra
Copy link
Author

Update: I have now rewritten it to:

resource_name = root.tag.replace('{http://hl7.org/fhir}','')
resource = import_from("fhir.resources." + resource_name.lower(), resource_name).parse_file(f)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant