Thanks to visit codestin.com
Credit goes to github.com

Skip to content

adehad/pyyaml

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PyYAML

A full-featured YAML processing framework for Python.

The documentation is available on the PyYAML website or in the form of GitHub flavoured markdown.

Installation

Default

pip install pyyaml

From Source

pip install git+https://github.com/yaml/pyyaml.git@master

or checkout the repository, or extract the .tar.gz and navigate inside and run:

python setup.py install

with LibYAML

By default, the setup.py script checks whether LibYAML is installed and if so, builds and installs LibYAML bindings.

Why LibYAML?

When LibYAML bindings are installed, you may use fast C-based parser and emitter as follows:

yaml.load(stream, Loader=yaml.CLoader)
yaml.dump(data, Dumper=yaml.CDumper)

Force installation with LibYAML --with-libyaml

python setup.py --with-libyaml install

Force installation without LibYAML --without-libyaml

python setup.py --without-libyaml install

Testing

PyYAML includes a comprehensive test suite. To run the tests:

python setup.py test

Loadings and Dumping

safe_load and safe_dump

Your default and safest choice for YAML streams.

load and dump

WARNING: CVE reference, etc. from wiki page.

Can load Python objects:

The Loader options

e.g. SafeLoader etc ...

The yaml package

The keyword arguments

Mention that these are the keyword arguments of the Dumper class and subclasses.

data / documents {#arg-data-documents}

  • the python object to serialise

Applies to safe_dump(), safe_dump_all() dump(), dump_all functions and the Dumper class

stream - where to output {#arg-stream}

  • IOObject - anything with a .write(), will be written to
  • None - returns a string.

Dumper {#arg-dumper}

  • Type[Dumper] class/subclass of Dumper ...

default_flow_style {#arg-default_flow_style}

  • Optional[bool]
    • True - what true does
    • False - what false does
    • None - what None does

Mention that these are the keyword arguments of the Loader class and subclasses.

stream {#arg-stream}

Constructors, representers, resolvers

YAML syntax

Documents

Block sequences

Block mappings

Flow collections

Scalars

Aliases

YAML tags and Python types

String conversion (Python 2 only)

String conversion (Python 3 only)

Names and modules

Objects

Deviations from the specification

Reference

The yaml package

Mark

YAMLError

Tokens

Events

Nodes

Loader

Dumper

YAMLObject

Frequently Asked Questions (FAQs)

YAML Specification Suppport

Dictionaries without nested collections are not dumped correctly

Why does

import yaml
document = """
  a: 1
  b:
    c: 3
    d: 4
"""
print(yaml.dump(yaml.load(document)))

give

a: 1
b: {c: 3, d: 4}

(see #18, #24)?

It's a correct output despite the fact that the style of the nested mapping is different.

By default, PyYAML chooses the style of a collection depending on whether it has nested collections. If a collection has nested collections, it will be assigned the block style. Otherwise it will have the flow style.

If you want collections to be always serialized in the block style, set the parameter default_flow_style of dump() to False. For instance,

>>> print(yaml.dump(yaml.load(document), default_flow_style=False))
a: 1
b:
  c: 3
  d: 4

Python 3 support

Starting from the 3.08 release, PyYAML and LibYAML bindings provide a complete support for Python 3. Python 2 support was dropped in the 6.0 release. This is a short outline of differences in PyYAML API between Python 2 and Python 3 versions.

In Python 2:

  • str objects are converted into !!str, !!python/str or !binary nodes depending on whether the object is an ASCII, UTF-8 or binary string.
  • unicode objects are converted into !!python/unicode or !!str nodes depending on whether the object is an ASCII string or not.
  • yaml.dump(data) produces the document as a UTF-8 encoded str object.
  • yaml.dump(data, encoding=('utf-8'|'utf-16-be'|'utf-16-le')) produces a str object in the specified encoding.
  • yaml.dump(data, encoding=None) produces a unicode object.

In Python 3:

  • str objects are converted to !!str nodes.
  • bytes objects are converted to !!binary nodes.
  • For compatibility reasons, !!python/str and !python/unicode tags are still supported and the corresponding nodes are converted to str objects.
  • yaml.dump(data) produces the document as a str object.
  • yaml.dump(data, encoding=('utf-8'|'utf-16-be'|'utf-16-le')) produces a bytes object in the specified encoding.

Further Information

License

The PyYAML module was written by Kirill Simonov [email protected]. It is currently maintained by the YAML and Python communities.

PyYAML is released under the MIT license.

See the file LICENSE for more details.

Security

Please report any security vulnerabilities to :

About

Canonical source repository for PyYAML

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Python 81.3%
  • Cython 18.1%
  • Other 0.6%