Ticket #29 (reopened enhancement)
Keeping mapping keys ordered
|Reported by:||edemaine@…||Owned by:||xi|
|Cc:||xi@…, edemaine@…, strombrg@…|
Would you be interested in adding the following kind of functionality to the public distribution of PyYAML?
>>> import yaml >>> d = yaml.load('z: 1\ny: 2\nx: 3\n', Loader=yaml.order.OrderedLoader) >>> d yaml.order.odict([('z', 1), ('y', 2), ('z', 3)]) >>> for key, value in d.iteritems (): print key, value z 1 y 2 x 3 >>> print yaml.dump(d, Dumper=yaml.order.OrderedDumper, default_flow_style=False), z: 1 y: 2 x: 3 >>> s = yaml.dump(d, default_flow_style=False) >>> print s, !!omap z: 1 y: 2 x: 3 >>> yaml.load(y) yaml.order.odict([('z', 1), ('y', 2), ('z', 3)])
There are two things going on here:
- Add real !!omap functionality. When loading an !!omap object, create an odict object (defined by a new class that maintains a dictionary along with a key order), instead of the current behavior of creating a regular Python dictionary. Conversely, when dumping such an object, preserve the key order (don't sort), and output an !!omap directive. Both of these features seem quite desirable from a YAML standard point of view.
Perhaps, more generally, dumping could check for a special 'keys_in_order' attribute, in which case it follows the order of keys(), instead of sorting the keys as in the recent patch.
- Add special yaml.order.OrderedLoader, which loads regular !!map values as if they were !!omap values, and yaml.order.OrderedDumper, which dumps odict types as regular !!map values (to avoid the ugly !!omap specifier).
Personally I would find this functionality very useful in many projects. It would enable a computer program edit a human-written YAML file, without messing up all the key orders, so that the computer output looks pretty similar to what the human had just before. I understand that YAML does not guarantee preservation of key order in a map type, or more precisely, it does not give it any significance to the order in absense of an !!omap or !!pairs type specification. But this is a practically useful feature in some cases, so it seems natural to provide it as an optional functionality in yaml.order.
I'd be happy to write the code for all of this, because I need it myself. My question is whether you'd consider including it in the PyYAML distribution.
- Cc xi@…, edemaine@…, strombrg@… added; xi@… removed
- Status changed from closed to reopened
- Resolution wontfix deleted