Update notes on integration with ConfigParser and json.

This commit is contained in:
Raymond Hettinger 2009-02-27 21:23:56 +00:00
parent d1a30cb1fa
commit 413f573f69
1 changed files with 16 additions and 6 deletions

View File

@ -181,7 +181,7 @@ Does odict support alternate sort orders such as alphabetical?
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 @@ How well does odict work with the json module and PyYAML?
>>> 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 @@ How well does odict work with the json module and PyYAML?
>>> 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