Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #108760

Re: OrderedDict

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Peter Otten <__peter__@web.de>
Newsgroups comp.lang.python
Subject Re: OrderedDict
Date Wed, 18 May 2016 14:24:39 +0200
Organization None
Lines 101
Message-ID <mailman.5.1463574298.27390.python-list@python.org> (permalink)
References <7522e947-03d5-46e4-a74a-e5b312da47ad@googlegroups.com> <nhhcjb$qe6$1@ger.gmane.org> <CAPTjJmouPkqdKy3Rrtr9c+ubUjj_fQ7LdFd+ZZzoexHhnBOeNA@mail.gmail.com> <nhhmu9$4av$1@ger.gmane.org>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Trace news.uni-berlin.de SXrd5VMwursJW+lhfnwFxwIgfNVKWthwHspbh6xl7W4g==
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; '+++': 0.03; 'diff': 0.05; 'skip:p 60': 0.05; 'sys': 0.05; '"__main__":': 0.07; '+class': 0.07; '__name__': 0.07; 'filename': 0.07; 'main()': 0.07; 'works.': 0.07; 'collections': 0.09; 'dict': 0.09; 'key)': 0.09; 'least)': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'skip:[ 30': 0.09; 'python': 0.10; '(at': 0.13; 'def': 0.13; 'wed,': 0.15; '-35,6': 0.16; '2016': 0.16; '__version__': 0.16; 'dictionaries': 0.16; 'doc:': 0.16; 'key):': 0.16; 'len(self)': 0.16; 'main():': 0.16; 'ordereddict': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'received:t-ipconnect.de': 0.16; 'wrote:': 0.16; 'library,': 0.18; '+0200': 0.20; 'proposed': 0.20; 'fix': 0.21; 'doc': 0.22; 'pass': 0.22; 'import': 0.24; 'patch': 0.24; 'header:User-Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'chris': 0.26; 'skip:" 20': 0.26; 'skip:m 30': 0.27; 'specify': 0.27; 'turns': 0.27; 'skip:u 20': 0.28; '---': 0.28; 'cat': 0.29; 'print': 0.30; 'code': 0.30; 'entry': 0.31; 'skip:_ 10': 0.32; 'class': 0.33; 'problem': 0.33; 'source': 0.33; 'skip:_ 30': 0.33; 'skip:p 30': 0.35; 'but': 0.36; 'should': 0.36; 'to:addr:python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'received:org': 0.37; 'self': 0.38; 'wrong': 0.38; 'skip:o 20': 0.38; 'does': 0.39; 'to:addr:python.org': 0.40; 'received:de': 0.40; 'skip:u 10': 0.61; 'improved': 0.63; 'here': 0.66; 'offer': 0.66; 'afraid': 0.67; "op's": 0.84; 'otten': 0.84
X-Injected-Via-Gmane http://gmane.org/
X-Gmane-NNTP-Posting-Host p57bd80a1.dip0.t-ipconnect.de
User-Agent KNode/4.13.3
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.22
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID <nhhmu9$4av$1@ger.gmane.org>
X-Mailman-Original-References <7522e947-03d5-46e4-a74a-e5b312da47ad@googlegroups.com> <nhhcjb$qe6$1@ger.gmane.org> <CAPTjJmouPkqdKy3Rrtr9c+ubUjj_fQ7LdFd+ZZzoexHhnBOeNA@mail.gmail.com>
Xref csiph.com comp.lang.python:108760

Show key headers only | View raw


Chris Angelico wrote:

> On Wed, May 18, 2016 at 7:28 PM, Peter Otten <__peter__@web.de> wrote:
>> I don't see an official way to pass a custom dict type to the library,
>> but if you are not afraid to change its source code the following patch
>> will allow you to access the value of dictionaries with a single entry as
>> d[0]:
>>
>> $ diff -u py2b_xmltodict/local/lib/python2.7/site-packages/xmltodict.py
>> py2_xmltodict/local/lib/python2.7/site-packages/xmltodict.py
>> --- py2b_xmltodict/local/lib/python2.7/site-packages/xmltodict.py      
>> 2016-05-18 11:18:44.000000000 +0200
>> +++ py2_xmltodict/local/lib/python2.7/site-packages/xmltodict.py       
>> 2016-05-18 11:11:13.417665697 +0200 @@ -35,6 +35,13 @@
>>  __version__ = '0.10.1'
>>  __license__ = 'MIT'
>>
>> +_OrderedDict = OrderedDict
>> +class OrderedDict(_OrderedDict):
>> +    def __getitem__(self, key):
>> +        if key == 0:
>> +            [result] = self.values()
>> +            return result
>> +        return _OrderedDict.__getitem__(self, key)
>>
>>  class ParsingInterrupted(Exception):
>>      pass
> 
> Easier than patching might be monkeypatching.
> 
> class OrderedDict(OrderedDict):
>     ... getitem code as above ...
> xmltodict.OrderedDict = OrderedDict
> 
> Try it, see if it works.

It turns out I was wrong on (at least) two accounts: 

- xmltodict does offer a way to specify the dict type
- the proposed dict implementation will not solve the OP's problem

Here is an improved fix which should work:


$ cat sample.xml 
<?xml version="1.0" encoding="utf-8" ?>
<profiles>
  <profile id='visio02' revision='2015051501' >
  <package package-id='0964-gpg4win' />
  </profile>
</profiles>
$ cat sample2.xml 
<?xml version="1.0" encoding="utf-8" ?>
<profiles>
  <profile id='visio02' revision='2015051501' >
  <package package-id='0964-gpg4win' />
  <package package-id='0965-gpg4win' />
  </profile>
</profiles>
$ cat demo.py
import collections
import sys
import xmltodict


class MyOrderedDict(collections.OrderedDict):
    def __getitem__(self, key):
        if key == 0 and len(self) == 1:
            return self
        return super(MyOrderedDict, self).__getitem__(key)


def main():
    filename = sys.argv[1]
    with open(filename) as f:
        doc = xmltodict.parse(f.read(), dict_constructor=MyOrderedDict)

    print "doc:\n{}\n".format(doc)
    print "package-id: {}".format(
        doc['profiles']['profile']['package'][0]['@package-id'])


if __name__ == "__main__":
    main()
$ python demo.py sample.xml 
doc:
MyOrderedDict([(u'profiles', MyOrderedDict([(u'profile', 
MyOrderedDict([(u'@id', u'visio02'), (u'@revision', u'2015051501'), 
(u'package', MyOrderedDict([(u'@package-id', u'0964-gpg4win')]))]))]))])

package-id: 0964-gpg4win
$ python demo.py sample2.xml 
doc:
MyOrderedDict([(u'profiles', MyOrderedDict([(u'profile', 
MyOrderedDict([(u'@id', u'visio02'), (u'@revision', u'2015051501'), 
(u'package', [MyOrderedDict([(u'@package-id', u'0964-gpg4win')]), 
MyOrderedDict([(u'@package-id', u'0965-gpg4win')])])]))]))])

package-id: 0964-gpg4win

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

OrderedDict silver0346@gmail.com - 2016-05-18 01:32 -0700
  Re: OrderedDict Chris Angelico <rosuav@gmail.com> - 2016-05-18 18:54 +1000
  Re: OrderedDict Peter Otten <__peter__@web.de> - 2016-05-18 11:28 +0200
  Re: OrderedDict Chris Angelico <rosuav@gmail.com> - 2016-05-18 20:47 +1000
  Re: OrderedDict Peter Otten <__peter__@web.de> - 2016-05-18 14:24 +0200
    Re: OrderedDict silver0346@gmail.com - 2016-05-19 22:15 -0700
      Re: OrderedDict silver0346@gmail.com - 2016-06-09 03:02 -0700

csiph-web