nazrulworld / fhir.resources

FHIR Resources https://www.hl7.org/fhir/resourcelist.html
https://pypi.org/project/fhir.resources/
Other
365 stars 104 forks source link

A way to get list of all resources and their inner elements, help needed in getting sub elements of inner elements #153

Open Vamshi3130 opened 5 months ago

Vamshi3130 commented 5 months ago

Description

In my project ,I wanted to get List of all fhir resources and their inner elements for ex [Patient,Patient.name,Patient.id,Patient.birthDate,Observation,Observation.id ........] Like this.

What I Did

@staticmethod def list_resource_types():

Get the path to the resources directory

    path = os.path.dirname(fhir.resources.__file__)
    # List all modules in the resources directory
    modules = [name for _, name, _ in pkgutil.iter_modules([path])]
    # Filter out modules that are all lowercase (assuming they are resource types)
    resources = sorted(i for i in modules if i.lower() == i)
    return resources

@staticmethod
def get_fields():
    # Get the list of resource types
    resource_types = ResourceGetter.list_resource_types()
    output = []
    for resource in resource_types:
        try:
            # Import the module for the resource type
            resource_module = importlib.import_module(f"fhir.resources.{resource}")
            # Iterate over all attributes of the module
            for attr_name in dir(resource_module):
                # Check if the attribute name matches the expected class name
                if attr_name.lower() == resource:
                    # Get the resource class
                    resource_class = getattr(resource_module, attr_name)
                    # Check if the class has the elements_sequence method
                    if hasattr(resource_class, 'elements_sequence'):
                        # Get the field names from the elements_sequence method
                        field_names = resource_class.elements_sequence()
                        for field in field_names:
                            output.append(f"{resource_class.__name__}.{field}")

                        break
        except Exception as e:
            # Handle exceptions (e.g., module import error, attribute access error)
            print(f"Error processing resource {resource}: {e}")
    return output

this is the method I'm using to do so , but I couldn't get sub elements of inner elements ,like Patient.name.given ,Patient.name.family please help me ,is there a way to get those

Vamshi3130 commented 5 months ago

I'm new to this ,so forgive me if anything is wrong