Dumping and loading with YAML

The https://yaml.org/ web page states the following about YAML:

YAML™ (rhymes with "camel") is a human-friendly, cross-language, Unicode-based data serialization language designed around the common native data types of agile programming languages.

The Python Standard Library documentation for the json module explains the following about JSON and YAML:

JSON is a subset of YAML 1.2. The JSON produced by this module's default settings (in particular, the default separators value) is also a subset of YAML 1.0 and 1.1. This module can thus also be used as a YAML serializer.

Technically, then, we can prepare YAML data using the json module. However, the json module cannot be used to de-serialize more sophisticated YAML data. There are two benefits to using YAML. First, it's a more sophisticated notation, allowing us to encode additional details about our objects. Second, the PyYAML implementation has a deep level of integration with Python that allows us to very simply create YAML encodings of Python objects. The drawback of YAML is that it is not as widely used as JSON. We'll need to download and install a YAML module. A good one can be found at http://pyyaml.org/wiki/PyYAML.

Once we've installed the package, we can dump our objects into the YAML notation:

import yaml 
text = yaml.dump(travel2) 
print(text) 

Here's what the YAML encoding for our microblog looks like:

!!python/object:__main__.Blog 
entries: 
- !!python/object:__main__.Post 
  date: 2013-11-14 17:25:00 
  rst_text: Some embarrassing revelation. Including ☹ and ⚓ 
  tags: !!python/tuple ['#RedRanger', '#Whitby42', '#ICW'] 
  title: Hard Aground 
- !!python/object:__main__.Post 
  date: 2013-11-18 15:30:00 
  rst_text: Some witty epigram. Including < & > characters. 
  tags: !!python/tuple ['#RedRanger', '#Whitby42', '#Mistakes'] 
  title: Anchor Follies 

The output is relatively terse but also delightfully complete. Also, we can easily edit the YAML file to make updates. The class names are encoded with a YAML !! tag. YAML contains 11 standard tags. The yaml module includes a dozen Python-specific tags, plus five complex Python tags.

The Python class names are qualified by the defining module. In our case, the module happened to be a simple script, so the class names are __main__.Blog and __main__.Post. If we had imported these from another module, the class names would reflect the module that defined the classes.

Items in a list are shown in a block sequence form. Each item starts with a - sequence; the rest of the items are indented with two spaces. When list or tuple is small enough, it can flow onto a single line. If it gets longer, it will wrap onto multiple lines. To load Python objects from a YAML document, we can use the following code:

copy = yaml.load(text) 

This will use the tag information to locate the class definitions and provide the values found in the YAML document to the constructor class. Our microblog objects will be fully reconstructed.

In the next chapter, we'll format YAML data on a file.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset