YAML in Python: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 42: Line 42:
{{Internal|Python Safely Navigate a Complex Data Structure#Overview|Safely Navigate a Complex Data Structure}}
{{Internal|Python Safely Navigate a Complex Data Structure#Overview|Safely Navigate a Complex Data Structure}}


=Configure <tt>yaml.dump()</tt> to render blank instead of <tt>null</tt>=
=Representers=
==Configure <tt>yaml.dump()</tt> to render blank instead of <tt>null</tt>==
PyYAML <code>dump()</code> uses <code>Representer()</code> to represent <code>None</code>. By default, the representer <code>dump()</code> is configured with represents <code>None</code> as "null". To change that:
PyYAML <code>dump()</code> uses <code>Representer()</code> to represent <code>None</code>. By default, the representer <code>dump()</code> is configured with represents <code>None</code> as "null". To change that:


Line 76: Line 77:


<font color=darkkhaki>what is the lifecycle of the method registered to the module?</font>
<font color=darkkhaki>what is the lifecycle of the method registered to the module?</font>
==Configure <tt>yaml.dump()</tt> to render | multi-lines==
<syntaxhighlight lang='py'>
def literal_presenter(dumper, data):
    return dumper.represent_scalar('tag:yaml.org,2002:str', data, style='|')
def default_presenter(dumper, data):
    return dumper.represent_scalar('tag:yaml.org,2002:str', data, style='')
try:
  yaml.add_representer(str, literal_presenter)
  yaml_text = yaml.dump(config)
  return yaml_text
finally:
    yaml.add_representer(str, default_presenter)
</syntaxhighlight>

Revision as of 05:10, 4 October 2022

Internal

PyYAML

https://pyyaml.org/
https://pyyaml.org/wiki/PyYAMLDocumentation
https://pypi.org/project/PyYAML/

PyYAML provides YAML serialization/deserialization in Python.

Installation

pip install pyyaml

requirements.txt:

pyyaml == 5.3.1

To install, see the "Installation" section from https://pyyaml.org/wiki/PyYAMLDocumentation.

Overview

Deserialize YAML

import yaml

with open('some-file.yaml', 'rt') as f:
  content = f.read()
data = yaml.load(content, Loader=yaml.Loader)

Serialize YAML

import yaml

# with the default flow style, the document is rendered in a non-indented manner 
print (yaml.dump(data, default_flow_style=False))

Safely Navigate a Complex Data Structure

Suggestions on how to safely recursively navigate a complex data structure:

Safely Navigate a Complex Data Structure

Representers

Configure yaml.dump() to render blank instead of null

PyYAML dump() uses Representer() to represent None. By default, the representer dump() is configured with represents None as "null". To change that:

1. Define a method that "represents" None

def representer_for_none(self, _):
    return self.represent_scalar('tag:yaml.org,2002:null', '')

2. Add it to the module:

import yaml

[...]

yaml.add_representer(type(None), represent_none)

This will render:

d = {
    'a': None,
    'b': 'c'
}

as:

a:
b: c

what is the lifecycle of the method registered to the module?

Configure yaml.dump() to render | multi-lines

def literal_presenter(dumper, data):
    return dumper.represent_scalar('tag:yaml.org,2002:str', data, style='|')

def default_presenter(dumper, data):
     return dumper.represent_scalar('tag:yaml.org,2002:str', data, style='')

try:
   yaml.add_representer(str, literal_presenter)
   yaml_text = yaml.dump(config)
   return yaml_text
finally:
    yaml.add_representer(str, default_presenter)