def to_yaml(r): '''serialize defaultdict, dict or pyfhir object to yaml''' try: r_dict = dict(r.as_json()) except AttributeError: try: r_dict = dict(r) except TypeError: r_dict = r return(y_dump(r_dict, sort_keys=False))
def dump_struct(content, as_yaml=False): ''' Contextmanager to pickle either *json* or *yaml* into a string :param content: data to pickle :param as_yaml: output as *yaml* instead of *json* :yield str: pickled ``content`` ''' try: yield y_dump(content, indent=4, default_flow_style=False) if as_yaml else j_dump( content, indent=2, sort_keys=True) except TypeError: yield None
def dump_struct(content, as_yaml=False): ''' Contextmanager to pickle either *json* or *yaml* into a string :param content: data to pickle :param as_yaml: output as *yaml* instead of *json* :yield str: pickled ``content`` ''' try: yield y_dump( content, indent=4, default_flow_style=False ) if as_yaml else j_dump( content, indent=2, sort_keys=True ) except TypeError: yield None
def test_dump_struct_yaml(): for ty in [None, 'a', {'a': None}, {'a': 'b'}, {'a': ['b', 'c']}, range]: with dump_struct(ty, as_yaml=True) as t: assert t == y_dump(ty, indent=4, default_flow_style=False)
def test_dump_struct_yaml(): for ty in [None, 'a', {'a': None}, {'a': 'b'}, {'a': ['b', 'c']}, range]: with dump_struct(ty, as_yaml=True) as t: assert t == y_dump( ty, indent=4, default_flow_style=False )
def yaml(r_dict): return y_dump(r_dict, allow_unicode=True, sort_keys=False)
def bundle_to_file(dict_bundle, type='bundle'): write_out(app.root_path, f"test-argo-pl-{type}.json", dumps(dict_bundle, indent=4)) write_out(app.root_path, f"test-argo-pl-{type}.yml", y_dump(dict_bundle, sort_keys=False))
'validation': ['check-must-support', 'allow-any-extensions', 'check-aggregation', 'no-broken-links', 'show-reference-messages']} ''' # convert to yaml from yaml import load as y_load, dump as y_dump try: from yaml import CLoader as Loader, CDumper as Dumper except ImportError: from yaml import Loader, Dumper yig_params = y_dump(ig_params, Dumper=Dumper, sort_keys=False) print(yig_params) ig_json = { "resourceType": "ImplementationGuide", "id": "healthedatainc.ig-template4", "text": { "status": "generated", "div": "<div xmlns=\"http://www.w3.org/1999/xhtml\"><h2>IGTest4</h2><p>The official URL for this implementation guide is: </p><pre>http://www.fhir.org/guides/test4/ImplementationGuide/healthedatainc.ig-template4</pre></div>" }, "extension": [ { "url": "http://hl7.org/fhir/StructureDefinition/igpublisher-spreadsheet", "valueUri": "patient-on-usprofile-spreadsheet.xml" }, {