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

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 this issue Mar 26, 2024 · 1 comment

Comments

@Vamshi3130
Copy link

  • fhir.resources version:R5
  • Python version:
  • Operating System:

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
Copy link
Author

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

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