Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Chris Angelico Newsgroups: comp.lang.python Subject: Re: OrderedDict Date: Wed, 18 May 2016 20:47:01 +1000 Lines: 32 Message-ID: References: <7522e947-03d5-46e4-a74a-e5b312da47ad@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de q1M+gnOvxAg0UMZNUEWouQxNilJqmErSXBjXgntO8tlw== Return-Path: 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; '+class': 0.07; 'works.': 0.07; 'cc:addr :python-list': 0.09; 'dict': 0.09; 'key)': 0.09; 'def': 0.13; 'wed,': 0.15; '-35,6': 0.16; '2016': 0.16; '__version__': 0.16; 'dictionaries': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'key):': 0.16; 'ordereddict': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'wrote:': 0.16; 'library,': 0.18; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; '+0200': 0.20; 'pass': 0.22; 'patch': 0.24; 'header:In-Reply- To:1': 0.24; 'message-id:@mail.gmail.com': 0.27; '---': 0.28; 'code': 0.30; 'entry': 0.31; 'skip:_ 10': 0.32; 'class': 0.33; 'source': 0.33; 'skip:_ 30': 0.33; 'received:google.com': 0.35; 'received:74.125.82': 0.35; 'skip:p 30': 0.35; 'but': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'skip:o 20': 0.38; 'afraid': 0.67; 'chrisa': 0.84; 'otten': 0.84; 'to:none': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc; bh=glnYeIAcrXwILnMGUZgVId5Iuqag9VVl4V85SVFmB+s=; b=SZZvOaGiGO+8P90m9ZMLXkD2b8z7vzMKVe7wGUbuCWgCSHTCTwk0/yIabxo6ouf7pr hvXoXHaiz/I22Y9gSNsF5Di5/03cTCc/tUL4I2+oDsQp3FRQPzSdQ5PIztBYfCZKwXPX E4N2NRdRq7fjinKRm3116SMVbOb9rZCwgw8FpaR3iEVc95FPcXpkzWHjkyg9Dynsyt+K Kp/8jrkymOOTqtrAWqWjybT8vSlV+fiU3LOHY5A7zNQKdv3+MKezRgqirDrPpRJ92t/T ArLi59kkeyhouiV4ubxOylF/fY12z6XPdmIYYN4jXaJ2Yo7OPFhKxIXXZEiBTUgix5O3 YgRw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:cc; bh=glnYeIAcrXwILnMGUZgVId5Iuqag9VVl4V85SVFmB+s=; b=HmHU+AQ5nQ8CADyIbPFblqt9o26e4svUP6Kgo0e9axed1d5v42A5ATiKRw6CZ/Hp5c 0XIb/lH2r+wlYyudhrCuc6JZFWPpEZe5nKtRrLlcYaKVbeQkjd7TozGEAo8iDfRr1X0V 7KXIFKN1D0eCzDG5i3xvUKQk+0aq7aTFgFghYyZzuwZrWiEKdUtLggMm9IqP/x90HWS/ yXaXcezUZkt/SMWDUoW8jEmrekmGctrQYBqg2vbxg+o5uze5ksTXqsVLpJCJafJplTDs K1rYui0h+RY+bfDLc8Oe6FXeC4eW3JRg/B68EXVSqmt7Y5pYYA8PiIRxkMc0rqHSrwrx OAaQ== X-Gm-Message-State: AOPr4FVEHcHAjHh0SruTNnoQPgpAZznAwB1cbsQ9RMkUV8soUWwZ0/3qjC4/Q+nT9FvBbGcY97OvF4Wwv0TssQ== X-Received: by 10.28.150.211 with SMTP id y202mr6752972wmd.41.1463568421638; Wed, 18 May 2016 03:47:01 -0700 (PDT) In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: X-Mailman-Original-References: <7522e947-03d5-46e4-a74a-e5b312da47ad@googlegroups.com> Xref: csiph.com comp.lang.python:108758 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. ChrisA