From 413f573f694a6170176cd0eea21fb345057fe147 Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Fri, 27 Feb 2009 21:23:56 +0000 Subject: [PATCH] Update notes on integration with ConfigParser and json. --- pep-0372.txt | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/pep-0372.txt b/pep-0372.txt index f52397e67..93a13df9c 100644 --- a/pep-0372.txt +++ b/pep-0372.txt @@ -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