Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PyYAML : Control ordering of items called by yaml.load()

Tags:

python

pyyaml

I have a yaml setting file which creates some records in db:

setting1:
  name: [item,item]
  name1: text
anothersetting2:
  name: [item,item]
  sub_setting:
      name :[item,item]

when i update this file with setting3 and regenerate records in db by:

import yaml
fh = open('setting.txt', 'r')
setting_list = yaml.load(fh)
for i in setting_list:
    add_to_db[i]

it's vital that the order of them settings (id numbers in db) stay the same each time as im addig them to the db... and setting3 just gets appended to the yaml.load()'s end so that its id doesn't confuse any records which are already in the db ... At the moment each time i add another setting and call yaml.load() records get loaded in different order which results in different ids. I would welcome any ideas ;)

EDIT: I've followed abarnert tips and took this gist https://gist.github.com/844388

Works as expected thanks !

like image 594
zzart Avatar asked Nov 08 '12 21:11

zzart


People also ask

What does YAML load do?

load converts a YAML document to a Python object. yaml. load accepts 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 encoded with utf-8, utf-16-be or utf-16-le encoding.

Does YAML guarantee order?

Yes, the order of sequences is preserved. In YAML a sequence (which maps to a python list): Represents a collection indexed by sequential integers starting with zero.

Is PyYAML same as YAML?

YAML is a data serialization format designed for human readability and interaction with scripting languages. PyYAML is a YAML parser and emitter for Python.

What is YAML safe load?

YAML Safe_Load() 1 month ago. by Saeed Raza. YAML is a serialization language that can be read by humans and is frequently used to store the data and configuration of files. Parsing is the process of further examining the logical structure after reading the data from the YAML file.


2 Answers

My project oyaml is a drop-in replacement for PyYAML, which will load maps into collections.OrderedDict instead of regular dicts. Just pip install it and use as normal - works on both Python 3 and Python 2.

Demo with your example:

>>> import oyaml as yaml  # pip install oyaml
>>> yaml.load('''setting1:
...   name: [item,item]
...   name1: text
... anothersetting2:
...   name: [item,item]
...   sub_setting:
...       name :[item,item]''')
OrderedDict([('setting1',
              OrderedDict([('name', ['item', 'item']), ('name1', 'text')])),
             ('anothersetting2',
              OrderedDict([('name', ['item', 'item']),
                           ('sub_setting', 'name :[item,item]')]))])

Note that if the stdlib dict is order preserving (Python >= 3.7, CPython >= 3.6) then oyaml will use an ordinary dict.

like image 75
wim Avatar answered Oct 04 '22 11:10

wim


You can now use ruaml.yaml for this.

From https://pypi.python.org/pypi/ruamel.yaml:

ruamel.yaml is a YAML parser/emitter that supports roundtrip preservation of comments, seq/map flow style, and map key order

like image 23
jan Avatar answered Oct 04 '22 13:10

jan