Yaml introduction and Python applications

Source: Internet
Author: User
Tags traits

Javaeye http://angeloce.iteye.com/admin/blogs/385976

========================================================== ============

 

Yaml is an intuitive data serialization format that can be recognized by computers. It is easy for humans to read and interact with scripting languages. Yaml is similar to XML, but its syntax is much simpler than XML. It is very effective for converting data into arrays or hash data.

 

Yaml syntax rules:

Http://www.ibm.com/developerworks/cn/xml/x-cn-yamlintro/

Http://www.yaml.org/

 

Yaml is considered by many to be a file format that can surpass XML and JSON. Compared with XML, XML is simple enough and easy to use. For JSON, yaml can be written as a standardized configuration file (which I think is much better than JSON, and writing a configuration file in JSON will drive people crazy ).

Yaml uses the data type of the host language, which may cause compatibility issues when circulating in multiple languages.

 

How to Write yaml? (Copied)

name: Tom Smithage: 37spouse:    name: Jane Smith    age: 25children: - name: Jimmy Smith   age: 15 - name1: Jenny Smith   age1: 12

 

For more information about the syntax, see yaml syntax rules.

 

Bytes --------------------------------------------------------------------------------------------

 

Implementation of yaml in Python: pyyaml

 

Write yaml as the configuration script test. yaml. The following describes how to read and write yaml configurations.

 

Use Python's yaml library pyyaml. Http://pyyaml.org/

 

After installing Python Lib, you can use it normally.

 

# Load yamlimport yaml # Read File F = open ('test. yaml') # import x = yaml. Load (f) print x

 

You may get strings similar to the following:

{'age': 37, 'spouse': {'age': 25, 'name': 'Jane Smith'}, 'name': 'Tom Smith', 'children': [{'age': 15, 'name': 'Jimmy Smith'}, {'age1': 12, 'name1': 'Jenny Smith'}]}

 

Using the yaml library in python is simple. Basically, two functions are used:

 

Yaml. Load

 

Yaml. Dump

 

For children who have used pickle, what does this mean?

 

Warning: it is not safe to callYaml. LoadWith any data stored ed from an untrusted source!Yaml. LoadIs as powerfulPickle. LoadAnd so may call any Python function.

 

For yaml reading, the most difficult thing is to write the correct yaml data format. If an error occurs accidentally, the load exception will occur, but sometimes no exception is reported, but no data can be read.

 

Pyyaml is a complete Python implementation and is called more Nb than pickle. (Who knows ?)

 

Yaml. LoadAccepts a byte string, a unicode string, an open binary file object, or an Open Text File object. A byte string or a file must be encodedUTF-8,Utf-16-beOrUtf-16-leEncoding.Yaml. LoadDetects the encoding by checkingBOM(Byte order mark) sequence at the beginning of the string/file. If noBOMIs present,UTF-8Encoding is assumed.

 

Yaml. Load can receive a byte string, Unicode string, a binary file opened or a text file object. The Byte string and file must be UTF-8, utf-16-be or utf-16-le-encoded. yaml. Load validate encoding by checking the BOM (byte mark) starting with the string/file. If Bom is not available, the default value is UTF-8.

 

The BOM on Baidu contains a character named "Zero Width no-break space" in the UCS encoding. Its Encoding is feff. Fffe does not exist in the UCs, so it should not appear in actual transmission. We recommend that you transmit the character "Zero Width no-break space" before transmitting the byte stream in the UCS specification ". In this way, if the receiver receives feff, it indicates that the byte stream is big-Endian; if it receives fffe, it indicates that the byte stream is little-Endian. Therefore, the character "Zero Width no-break space" is also called Bom.
The UTF-8 does not need BOM to indicate the byte order, but BOM can be used to indicate the encoding method. The UTF-8 code for the character "Zero Width no-break space" is ef bb bf. So if the receiver receives a byte stream starting with ef bb bf, it will know that this is UTF-8 encoding. Windows uses BOM to mark the encoding of text files.

 

Yaml. Load returns a python object. What will happen ...... Look at what your data is ......

 

 

If a string or a file contains several documents, you may load them all withYaml. load_allFunction.

 

If the string or file contains several yaml documents, you can use yaml. load_all to parse all the documents.

 

Yaml. load (stream, loader = <class 'yaml. loader. loader '>) parse the first yaml document in a stream # Only parse the first and produce the corresponding Python object. yaml. load_all (stream, loader = <class 'yaml. loader. loader '>) Parse all yaml events in a stream and produce corresponding Python objects.

 

Yaml. load_all will generate an iterator. All you need to do is read it

 

documents = """name: The Set of Gauntlets 'Pauraegen'description: >  A set of handgear with sparks that crackle  across its knuckleguards. ---name: The Set of Gauntlets 'Paurnen'description: >   A set of gauntlets that gives off a foul,   acrid odour yet remains untarnished. ---name: The Set of Gauntlets 'Paurnimmen'description: >   A set of handgear, freezing with unnatural cold."""for data in yaml.load_all(documents):print data#{'description': 'A set of handgear with sparks that crackle across its #knuckleguards.\n',#'name': "The Set of Gauntlets 'Pauraegen'"}#{'description': 'A set of gauntlets that gives off a foul, acrid odour #yet remains untarnished.\n',#'name': "The Set of Gauntlets 'Paurnen'"}#{'description': 'A set of handgear, freezing with unnatural cold.\n',#'name': "The Set of Gauntlets 'Paurnimmen'"}

 

Pyyaml allows you to construct a python object of any type.

Even instances of Python classes can be constructed using!! Python/ObjectTag.

 

Pyyaml allows you to build any type of Python objects or even Python class instances. You only need to use the yaml label !! Python/object.

This will be useful in the future.

 

Note that the ability to construct an arbitrary Python object may be dangerous if you receive a yaml document from an untrusted source such as Internet. The functionYaml. safe_loadLimits this ability to simple Python objects like integers or lists.

 

Note that building Python objects in yaml is dangerous, especially when an unknown yaml document is received. Yaml. safe_load can limit this capability. Just use some simple objects.

 

---------------------------------------

Dumping yaml

 

TheYaml. DumpFunction accepts a python object and produces a yaml document.

 

Yaml. Dump generates a python object as a yaml document and works with yaml. Load.

Dump (data, stream = none, dumper = <class 'yaml. dumper. dumper '>, ** kwds) serialize a python object into a yaml stream. if stream is none, return the produced string instead. # Good. If the default data stream is null, a string will be returned to you as the yaml document.

 

 

Aproject = {'name': 'silenthand olleander ', 'race': 'human', 'traits ': ['one _ hand ', 'One _ ey']} print yaml. dump (aproject) # return # Name: silenthand olleander # race: Human # traits: [one_hand, one_eye]

 

 

 

 

Yaml. DumpAccepts the second Optional argument, which must be an open text or binary file. In this case,Yaml. DumpWill write the produced yaml document into the file. Otherwise,Yaml. DumpReturns the produced document.

 

The second parameter received by yaml. Dump must be an open text file or binary file. yaml. dump writes the generated yaml file to the file. Otherwise, yaml. Dump returns the generated document.

 

If you need to dump several yaml documents to a single stream, use the FunctionYaml. dump_all.Yaml. dump_allAccepts a list or a generator producing

Python objects to be serialized into a yaml document. The second Optional argument is an open file.

 

If you need to write several yaml documents into a data stream at the same time, use the yaml. dump_all function. Yaml. dump_all can receive a list or generate a serializable Generator for python objects (this is awkward). The second parameter is the file opened. This corresponds to yaml. load_all.

 

You may even dump instances of Python classes.

 

You can even directly dump Python class instances (objects.

 

Yaml. DumpSupports a number of keyword arguments that specify formatting details for the emitter. For instance, you may set the preferred intendation and width, use the canonical yaml format or force preferred style for scalars and collections.

 

Yaml. Dump supports many keyword parameters used to determine the format of the transmitter (ignore this sentence first --#). For example, you can set indentation and width (for the yaml document), use the standard yaml format or force the priority style for scalar and collection (please ignore --#).

 

Look at the translation.

 

Dump_all (documents, stream = none, dumper = <class 'yaml. dumper. dumper '>, default_style = none, default_flow_style = none, canonical = none, indent = none, width = none, allow_unicode = none, line_break = none, encoding = 'utf-8 ', explicit_start = none, explicit_end = none, version = none, tags = none) # The parameters corresponding to specific function parameters can be seen # cannonical # indent # width # and so on

 

Example

>>> print yaml.dump(range(50))[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,  23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42,  43, 44, 45, 46, 47, 48, 49]>>> print yaml.dump(range(50), width=50, indent=4)[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,    16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27,    28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,    40, 41, 42, 43, 44, 45, 46, 47, 48, 49]>>> print yaml.dump(range(5), canonical=True)---!!seq [  !!int "0",  !!int "1",  !!int "2",  !!int "3",  !!int "4",]>>> print yaml.dump(range(5), default_flow_style=False)- 0- 1- 2- 3- 4>>> print yaml.dump(range(5), default_flow_style=True, default_style='"')[!!int "0", !!int "1", !!int "2", !!int "3", !!int "4"]

 

The key lies in the following parameters.

 

------------------------------------------------------

 

Constructors, representers, Resolvers

 

Constructor, Descriptor (?), Parser

 

You may define your own application-specific tags. The easiest way to do it is to define a subclassYaml. yamlobject

 

You can customize a tag to define a subclass of yaml. yamlobject in the simplest way:

 

 

class Monster(yaml.YAMLObject):    yaml_tag = u'!Monster'    def __init__(self, name, hp, ac, attacks):        self.name = name        self.hp = hp        self.ac = ac        self.attacks = attacks    def __repr__(self):        return "%s(name=%r, hp=%r, ac=%r, attacks=%r)" % (            self.__class__.__name__, self.name, self.hp, self.ac,self.attacks)

 

 

The above definition is enough to automatically load and dumpMonsterObjects:

 

The monster class defined above is enough for load and dump:

 

>>> yaml.load("""... --- !Monster... name: Cave spider... hp: [2,6]    # 2d6... ac: 16... attacks: [BITE, HURT]... """)Monster(name='Cave spider', hp=[2, 6], ac=16, attacks=['BITE', 'HURT'])>>> print yaml.dump(Monster(...     name='Cave lizard', hp=[3,6], ac=16, attacks=['BITE','HURT']))!Monsterac: 16attacks: [BITE, HURT]hp: [3, 6]name: Cave lizard

 

 

 

Yaml. yamlobjectUses metaclass magic to register a constructor, which transforms a yaml node to a class instance, and a representer, which serializes a class instance to a yaml node.

 

Yaml. yamlobject registers a constructor that converts yaml encoding into a class instance using the MAGIC Meta class, And serializes the class instance into a yaml encoding descriptor.

 

If you don't want to use metaclasses, you may register your constructors and representers using the functionsYaml. add_constructorAndYaml. add_representer. For instance, you may want to add a constructor and a representer for the followingDiceClass:

 

If you do not want to use a metadatabase, you can use the yaml. add_constructor and yaml. add_representer functions to register the constructor and describe. For example, you can add a constructor and descriptor to the following dice class:

 

>>> class Dice(tuple):...     def __new__(cls, a, b):...         return tuple.__new__(cls, [a, b])...     def __repr__(self):...         return "Dice(%s,%s)" % self>>> print Dice(3,6)Dice(3,6)

 

 

The default representationDiceObjects is not nice:

 

The default yaml description of this dice object is not very nice:

 

>>> print yaml.dump(Dice(3,6))!!python/object/new:__main__.Dice- !!python/tuple [3, 6]

 

 

Suppose you wantDiceObject To representedADBIn yaml:

 

Well, Now suppose you want to describe the dice object in the form of "ADB" in yaml (A and B are variables ).

 

First we define a representer that convert a dice object to scalar node with the tag! DiceAnd register it.

 

First, we define a dice object that can be converted into a dice object '! Dice 'label node descriptor, and then register.

 

>>> def dice_representer(dumper, data):...     return dumper.represent_scalar(u'!dice', u'%sd%s' % data)>>> yaml.add_representer(Dice, dice_representer)

 

 

Now you may dump an instance ofDiceObject:

 

Now you can dump a dice instance:

 

>>> print yaml.dump({'gold': Dice(10,6)}){gold: !dice '10d6'}

 

Let us add the code to construct a dice object:

 

Let's add the node to the constructor of the dice object.

 

>>> def dice_constructor(loader, node):...     value = loader.construct_scalar(node)...     a, b = map(int, value.split('d'))...     return Dice(a, b)>>> yaml.add_constructor(u'!dice', dice_constructor)

 

 

Then you may loadDiceObject as well:

 

Then you can use it.

 

>>> print yaml.load("""... initial hit points: !dice 8d4... """){'initial hit points': Dice(8,4)}

 

 

It can be seen from this that constructor and representer are opposite. One is load and the other is dump.

 

 

-------------------------------------------------------

 

Most of the above comes from http://pyyaml.org/wiki/PyYAMLDocumentation

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.