Giter Club home page Giter Club logo

Comments (7)

Healthedata1 avatar Healthedata1 commented on August 20, 2024 2

here is what I did:

element_type = next(i.type_.__resource_type__ for i in resource.element_properties() if i.name == element)

from fhir.resources.

nazrulworld avatar nazrulworld commented on August 20, 2024

We should have a class method available.

      >>> from fhir.resources.observation import Observation
 
      >>> Observation.get_resource_type()

from fhir.resources.

Healthedata1 avatar Healthedata1 commented on August 20, 2024

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'

from fhir.resources.

nazrulworld avatar nazrulworld commented on August 20, 2024

@Healthedata1 that is something not possible, unfortunately.

from fhir.resources.

theGOTOguy avatar theGOTOguy commented on August 20, 2024

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.

from fhir.resources.

ArdonToonstra avatar ArdonToonstra commented on August 20, 2024

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.

from fhir.resources.

vadi2 avatar vadi2 commented on August 20, 2024

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.

from fhir.resources.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.