@@ -295,8 +295,56 @@ def __repr__(self):
295295 '[%s]' % ', ' .join ('%s' % (item ,) for item in self .items ())
296296 )
297297
298+
299+ class _OrderedDictDelegator (collections .MutableMapping ):
300+
301+ # Start by filling-out the abstract methods
302+ def __init__ (self , dict = None , ** kwargs ):
303+ self .data = OrderedDict ()
304+ if dict is not None :
305+ self .update (dict )
306+ if len (kwargs ):
307+ self .update (kwargs )
308+ def __len__ (self ): return len (self .data )
309+ def __getitem__ (self , key ):
310+ if key in self .data :
311+ return self .data [key ]
312+ if hasattr (self .__class__ , "__missing__" ):
313+ return self .__class__ .__missing__ (self , key )
314+ raise KeyError (key )
315+ def __setitem__ (self , key , item ): self .data [key ] = item
316+ def __delitem__ (self , key ): del self .data [key ]
317+ def __iter__ (self ):
318+ return iter (self .data )
319+
320+ # Modify __contains__ to work correctly when __missing__ is present
321+ def __contains__ (self , key ):
322+ return key in self .data
323+
324+ # Now, add the methods in dicts but not in MutableMapping
325+ def __repr__ (self ): return repr (self .data )
326+ def copy (self ):
327+ if self .__class__ is _OrderedDictDelegator :
328+ return _OrderedDictDelegator (self .data .copy ())
329+ import copy
330+ data = self .data
331+ try :
332+ self .data = OrderedDict
333+ c = copy .copy (self )
334+ finally :
335+ self .data = data
336+ c .update (self )
337+ return c
338+ @classmethod
339+ def fromkeys (cls , iterable , value = None ):
340+ d = cls ()
341+ for key in iterable :
342+ d [key ] = value
343+ return d
344+
345+
298346
299- class Tally (_TallyMixin , FrozenDict ):
347+ class Tally (_TallyMixin , collections . UserDict ):
300348 '''
301349 blocktododoc
302350 An immutable tally.
@@ -315,8 +363,10 @@ class that has an identity crisis and doesn't know whether it's a
315363 counter or a dict.
316364
317365 '''
366+ _dict = property (lambda self : self .data )
367+
318368
319- class OrderedTally (_TallyMixin , FrozenDict ):
369+ class OrderedTally (_OrderedTallyMixin , _TallyMixin , _OrderedDictDelegator ):
320370 '''
321371 blocktododoc
322372 An immutable, ordered tally.
@@ -336,7 +386,9 @@ class that has an identity crisis and doesn't know whether it's a
336386
337387 - It has an order to its elements, like `collections.OrderedDict`.
338388
339- '''
389+ '''
390+ _dict = property (lambda self : self .data )
391+
340392
341393class FrozenTally (_BaseTallyMixin , FrozenDict ):
342394 '''
0 commit comments