Skip to content

Commit

Permalink
wip patient class
Browse files Browse the repository at this point in the history
  • Loading branch information
ThiagoTrabach committed Aug 30, 2023
1 parent fc9f510 commit 62af206
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 41 deletions.
23 changes: 14 additions & 9 deletions fhir_utils/fhir/patient/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,26 @@

def fhir_to_dict(fhir_json: str, source: str) -> dict:
# Extract identifiers
identifiers = []
identifiers = {}
for identifier in fhir_json["identifier"]:
identifiers.append({"code": identifier["type"]["coding"][0]["code"],
"value": identifier["value"]})
identifiers[identifier["type"]["coding"][0]["code"].lower()] = identifier["value"]

# Extract extensions
parents = []
mother = ""
father = ""
race_ethnicity = {"race":"", "indigenous_ethnicity":""}

# TODO: add case for other extensions
for extension in fhir_json["extension"]:
match extension["extension"][0]["url"]:
match extension["extension"][0]["url"]:
case "relationship":
parents.append({"code": extension["extension"][0]["valueCode"],
"name": extension["extension"][1]["valueHumanName"]["text"]
})
match extension["extension"][0]["valueCode"]:
case "mother":
mother = extension["extension"][1]["valueHumanName"]["text"]
case "father":
father = extension["extension"][1]["valueHumanName"]["text"]
case _:
pass
case "race":
race_ethnicity.update({"race": extension["extension"][0]["valueCodeableConcept"]["coding"][0]["code"]})
case _:
Expand All @@ -34,7 +38,8 @@ def fhir_to_dict(fhir_json: str, source: str) -> dict:
"name": fhir_json["name"][0]["text"] if "name" in fhir_json.keys() else "",
"nationality": "",
"naturalization": "",
"parents": parents,
"mother": mother,
"father": father,
"protected_person": "",
"race_ethnicity": race_ethnicity,
"register_quality": "",
Expand Down
60 changes: 31 additions & 29 deletions fhir_utils/fhir/patient/resource.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,39 @@
# -*- coding: utf-8 -*-
from dataclasses import dataclass, field

@dataclass
class Patient:
source : str
name : str
birth_date : str
cpf : str
cns : str = ""
gender : str = "unknow"
active : bool = True
address : list = field(default_factory=list)
birth_city : str = ""
birth_country : str = "01"
deceased : bool = True
nationality : str = ""
naturalization : str = ""
mother : str = ""
father : str = ""
protected_person : bool = False
race: str = ""
ethnicity : str = ""
register_quality : float = 0
telecom : list = field(default_factory=list)

elements_required = ["active", "birthCountry", "birthDate", "gender", "identifier", "name"]
def __post_init__(self):
self.name = self.name.title()
# verifica validade da pk

def __init__(self, patient: dict, source: str):
# verifica presença dos campos obrigatórios

#verify

# verify if all required elements are in patient
elements_required_missing = []
for key in elements_required:
if key not in patient:
elements_required_missing.append(key)
if len(elements_required_missing) > 0:
raise f"Missing required element(s): {elements_required_missing}"
#def compliance_check

#def padroniza textos

self.source = source
self.active = None
self.address = None
self.birth_city = None
self.birth_country = None
self.birth_date = None
self.deceased = None
self.gender = None
self.identifier = None
self.name = None
self.nationality = None
self.naturalization = None
self.parents = None
self.protected_person = None
self.race_ethnicity = None
self.register_quality = None
self.telecom = None

#def dicarcad_incomplinat

#def to_fhir
12 changes: 9 additions & 3 deletions run.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
from fhir_utils.utils import json_to_dict
from fhir_utils.fhir.patient.parsers import fhir_to_dict
from fhir_utils.fhir.patient.resource import Patient

patient = json_to_dict("/home/thiagotrabach/tmp/patient_sample.json")
patient_json = json_to_dict("/home/thiagotrabach/tmp/patient_sample.json")

patient_clean = fhir_to_dict(patient, "vitacare")
patient = fhir_to_dict(patient_json, "vitacare")

print(patient_clean)
gabriela = Patient(name = patient["data"]["name"],
cpf = patient["data"]["identifiers"]["tax"],
birth_date = patient["data"]["birth_date"],
source ="vitai" )

print(gabriela)


0 comments on commit 62af206

Please sign in to comment.