nazrulworld / fhir.resources

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

add YAML support #47

Closed Healthedata1 closed 1 year ago

Healthedata1 commented 3 years ago

Description

would like to add native YAML support so that a Resource object can be created from YAML object or file and serialize as a YAML string.

e.g:

>>> from fhir.resources.organization import Organization
>>> from fhir.resources.address import Address
>>> yaml_str = '''
resourceType: Organization
id: f001
active: true
name: Acme Corporation
address:
  - country: Swizterland
'''
>>> org = Organization.parse_raw(yaml_str)
>>> isinstance(org.address[0], Address)
>>> True
>>> org.address[0].country == "Swizterland"
True
>>> org.dict()['active'] is True
True

and

>>> from fhir.resources.patient import Patient
>>> import os
>>> import pathlib
>>> filename = pathlib.Path("foo/bar.yaml")
>>> pat = Patient.parse_file(filename)
>>> pat.resource_type == "Patient"
True

and


>>> pat.as_yaml()
'''
resourceType: Organization
id: f001
active: true
name: Acme Corporation
address:
  - country: Swizterland
'''

See pyyaml.org
nazrulworld commented 3 years ago

This seems is a very good idea 💡 and also I think this will open up to support for XML. For me, I could try after the holiday season ends (after the new year) but anyone welcomes to try now. Related pydantic issues

  1. https://github.com/samuelcolvin/pydantic/issues/136
  2. https://github.com/samuelcolvin/pydantic/issues/1043
nazrulworld commented 3 years ago

@Healthedata1 what should be the proper mime type for YAML? some folk suggest text/yml` or `application/x-yaml` https://stackoverflow.com/questions/332129/yaml-media-type I don't find any definition inside python's built-in modulemimetypes``

Healthedata1 commented 3 years ago

I don't have an opinion. It seems from the pydantic CRs that just using the yaml dump and load methods is the preferred choice.

e.g.

Testing out fhir.resources

start with fhirstring class...

from fhir.resources.patient import Patient as P
from yaml import dump as y_dump, load as y_load
import datetime
p = P()
p.json()
'{"resourceType": "Patient"}'
print(y_dump(p.dict()))
resourceType: Patient
my_patient = {'id': 'subject1',
 'meta': {'profile': ['http://hl7.org/fhir/us/core/StructureDefinition/us-core-patient|4.0.0']},
 'active': True,
 'address': [{'city': 'Anytown',
   'country': 'USA',
   'line': ['100 Main St'],
   'postalCode': '99999',
   'state': 'CA',
   'text': '100 Main St\nAnytown, CA 99999\nUSA'}],
 'birthDate': datetime.date(1964, 6, 19),
 'communication': [{'language': {'coding': [{'code': 'en',
      'display': 'English',
      'system': 'urn:ietf:bcp:47'}],
    'text': 'English'}}],
 'gender': 'male',
 'identifier': [{'system': 'http://example.org/pids', 'value': '1234'}],
 'name': [{'family': 'Doe', 'given': ['John', 'M']}],
 'resourceType': 'Patient'}
p = P(**my_patient)

print(y_dump(p.dict()))
active: true
address:
- city: Anytown
  country: USA
  line:
  - 100 Main St
  postalCode: '99999'
  state: CA
  text: '100 Main St

    Anytown, CA 99999

    USA'
birthDate: 1964-06-19
communication:
- language:
    coding:
    - code: en
      display: English
      system: urn:ietf:bcp:47
    text: English
gender: male
id: subject1
identifier:
- system: http://example.org/pids
  value: '1234'
meta:
  profile:
  - http://hl7.org/fhir/us/core/StructureDefinition/us-core-patient|4.0.0
name:
- family: Doe
  given:
  - John
  - M
resourceType: Patient
p_yaml = '''
active: true
address:
- city: Anytown
  country: USA
  line:
  - 100 Main St
  postalCode: '99999'
  state: CA
  text: '100 Main St

    Anytown, CA 99999

    USA'
birthDate: 1964-06-19
communication:
- language:
    coding:
    - code: en
      display: English
      system: urn:ietf:bcp:47
    text: English
gender: male
id: subject1
identifier:
- system: http://example.org/pids
  value: '1234'
meta:
  profile:
  - http://hl7.org/fhir/us/core/StructureDefinition/us-core-patient|4.0.0
name:
- family: Doe
  given:
  - John
  - M
resourceType: Patient
'''
y = y_load(p_yaml)
y
c:\users\administrator\appdata\local\programs\python\python37-32\lib\site-packages\ipykernel_launcher.py:1: YAMLLoadWarning: calling yaml.load() without Loader=... is deprecated, as the default Loader is unsafe. Please read https://msg.pyyaml.org/load for full details.
  """Entry point for launching an IPython kernel.

{'active': True,
 'address': [{'city': 'Anytown',
   'country': 'USA',
   'line': ['100 Main St'],
   'postalCode': '99999',
   'state': 'CA',
   'text': '100 Main St\nAnytown, CA 99999\nUSA'}],
 'birthDate': datetime.date(1964, 6, 19),
 'communication': [{'language': {'coding': [{'code': 'en',
      'display': 'English',
      'system': 'urn:ietf:bcp:47'}],
    'text': 'English'}}],
 'gender': 'male',
 'id': 'subject1',
 'identifier': [{'system': 'http://example.org/pids', 'value': '1234'}],
 'meta': {'profile': ['http://hl7.org/fhir/us/core/StructureDefinition/us-core-patient|4.0.0']},
 'name': [{'family': 'Doe', 'given': ['John', 'M']}],
 'resourceType': 'Patient'}
p = P(**y)
print(y_dump(p.dict()))
active: true
address:
- city: Anytown
  country: USA
  line:
  - 100 Main St
  postalCode: '99999'
  state: CA
  text: '100 Main St

    Anytown, CA 99999

    USA'
birthDate: 1964-06-19
communication:
- language:
    coding:
    - code: en
      display: English
      system: urn:ietf:bcp:47
    text: English
gender: male
id: subject1
identifier:
- system: http://example.org/pids
  value: '1234'
meta:
  profile:
  - http://hl7.org/fhir/us/core/StructureDefinition/us-core-patient|4.0.0
name:
- family: Doe
  given:
  - John
  - M
resourceType: Patient
nazrulworld commented 3 years ago

@Healthedata1 did you try the latest beta release yet?

Healthedata1 commented 3 years ago

not yet, but I will. I am going through the FastAPI spec hoping to generate swagger docs using the models but I see that there are some issues with that.

Eric

Eric M Haas, DVM, MS Health eData Inc 35 Crescent Avenue, Sausalito, CA 94965 707.227.2608|Skype: haas.eric1 @.***

On Thu, Apr 1, 2021 at 9:57 AM Md Nazrul Islam @.***> wrote:

@Healthedata1 https://github.com/Healthedata1 did you try the latest beta release yet?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/nazrulworld/fhir.resources/issues/47#issuecomment-812041299, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABYRJ2VSJTKHAZEHSAKVTYDTGSQYTANCNFSM4VAAFBLA .

Healthedata1 commented 1 year ago

used it and it works great. many thanks