[Python-checkins] r70031 - peps/trunk/pep-0372.txt
raymond.hettinger
python-checkins at python.org
Fri Feb 27 22:23:57 CET 2009
Author: raymond.hettinger
Date: Fri Feb 27 22:23:56 2009
New Revision: 70031
Log:
Update notes on integration with ConfigParser and json.
Modified:
peps/trunk/pep-0372.txt
Modified: peps/trunk/pep-0372.txt
==============================================================================
--- peps/trunk/pep-0372.txt (original)
+++ peps/trunk/pep-0372.txt Fri Feb 27 22:23:56 2009
@@ -181,7 +181,7 @@
dbm) is likely a better fit. It would be a mistake to try to be all
things to all users.
-How well does odict work with the json module and PyYAML?
+How well does odict work with the json module, PyYAML, and ConfigParser?
For json, the good news is that json's encoder respects odict's iteration order:
@@ -189,13 +189,15 @@
>>> json.dumps(OrderedDict(items))
'{"one": 1, "two": 2, "three": 3, "four": 4, "five": 5}'
- The bad news is that the object_hook for json decoders will pass in an
- already built dictionary so that the order is lost before the object
- hook sees it:
+ In Py2.6, the object_hook for json decoders passes-in an already built
+ dictionary so order is lost before the object hook sees it. This
+ problem is being fixed for Python 2.7/3.1 by adding an new hook that
+ preserves order (see http://bugs.python.org/issue5381 ).
+ With the new hook, order can be preserved:
>>> jtext = '{"one": 1, "two": 2, "three": 3, "four": 4, "five": 5}'
- >>> json.loads(jtext, object_hook=OrderedDict)
- OrderedDict({u'four': 4, u'three': 3, u'five': 5, u'two': 2, u'one': 1})
+ >>> json.loads(jtext, object_pairs_hook=OrderedDict)
+ OrderedDict({'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5})
For PyYAML, a full round-trip is problem free:
@@ -211,6 +213,14 @@
>>> yaml.load(ytext)
OrderedDict({'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5})
+ For the ConfigParser module, round-tripping is problem free. Custom
+ dicts were added in Py2.6 specifically to support ordered dictionaries:
+
+ >>> config = ConfigParser(dict_type=OrderedDict)
+ >>> config.read('myconfig.ini')
+ >>> config.remove_option('Log', 'error')
+ >>> config.write(open('myconfig.ini', 'w'))
+
How does odict handle equality testing?
Being a dict, one might expect equality tests to not care about order. For
More information about the Python-checkins
mailing list