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

add YAML support #47

Closed
Healthedata1 opened this issue Dec 17, 2020 · 6 comments
Closed

add YAML support #47

Healthedata1 opened this issue Dec 17, 2020 · 6 comments
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@Healthedata1
Copy link

  • fhir.resources version: latest
  • Python version: 3.6+
  • Operating System: MAC/Windows/Linux

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 nazrulworld added enhancement New feature or request good first issue Good for newcomers labels Dec 17, 2020
@nazrulworld
Copy link
Owner

nazrulworld commented Dec 17, 2020

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. Support for parsing from YAML file pydantic/pydantic#136
  2. Support for exporting to YAML pydantic/pydantic#1043

@nazrulworld
Copy link
Owner

@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 module mimetypes``

@Healthedata1
Copy link
Author

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 added a commit that referenced this issue Mar 26, 2021
* more details changes log yet to done.
@nazrulworld
Copy link
Owner

@Healthedata1 did you try the latest beta release yet?

@Healthedata1
Copy link
Author

Healthedata1 commented Apr 1, 2021 via email

@Healthedata1
Copy link
Author

used it and it works great. many thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants