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

Update PHDC recordTarget to switch to **kwargs logic, optimize #133

Open
robertandremitchell opened this issue Jan 23, 2024 · 0 comments
Open

Comments

@robertandremitchell
Copy link

This would switch the https://github.com/CDCgov/phdi/blob/main/containers/message-parser/app/phdc/phdc.py#L255 logic with two changes in mind:

  1. Change root and other ID extensions to a list
  2. Change loop through XML types to a dictionary

This was the scratch code I had worked on as part of the initial ticket:

    def _build_recordTarget(
        self,
        **kwargs,
        # id: str,
        # root: str = None,
        # assigningAuthorityName: str = None,
        # telecom_data: str = None,
        # addr_data: str = None,
        # patient_data: str = None,
    ):
        """
        Builds a `recordTarget` XML element for recordTarget data, which refers to
          the medical record of the patient.

        :param id: recordTarget identifier
        :param root: recordTarget root
        :param assigningAuthorityName: recordTarget assigningAuthorityName
        :param telecom_data: XML data from _build_telecom
        :param addr_data: XML data from _build_addr
        :param patient_data: XML data from _build_patient

        :raises ValueError: recordTarget needs ID to be defined.

        :return recordTarget_data: XML element of the recordTarget
        """
        if "id" not in kwargs or kwargs["id"] is None:
            raise ValueError("The recordTarget id parameter must be a defined.")

        # create recordTarget element
        recordTarget_data = ET.Element("recordTarget")

        # Create and append 'patientRole' element
        patientRole = ET.Element("patientRole")
        recordTarget_data.append(patientRole)

        # initialize id data, add data, then append to patientRole
        id_element = ET.Element("id")
        id_element.set("extension", kwargs["id"])

        element_list = ["root", "assigningAuthorityName"]
        for id_elem in element_list:
            if id_elem in kwargs:
                id_element.set(id_elem, kwargs[id_elem])

        patientRole.append(id_element)

        # add address, telecom, patient
        element_methods = {
            "addr_data": self._build_addr,
            "telecom_data": self._build_telecom,
            "patient_data": self._build_patient,
        }

        for elem_key, method in element_methods.items():
            if elem_key in kwargs:
                element = method(**kwargs[elem_key])
                patientRole.append(element)

The primary benefits to this are that it is a bit more compact and also allows more methods and extensions to be added to recordTarget more seamlessly and without initializing more variables, bringing it more in line with the other methods in the file.

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