annotate utils/odict.py @ 0:5b3c6135a982

Imported from capsule None
author devteam
date Tue, 01 Apr 2014 10:53:10 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
5b3c6135a982 Imported from capsule None
devteam
parents:
diff changeset
1 """
5b3c6135a982 Imported from capsule None
devteam
parents:
diff changeset
2 Ordered dictionary implementation.
5b3c6135a982 Imported from capsule None
devteam
parents:
diff changeset
3 """
5b3c6135a982 Imported from capsule None
devteam
parents:
diff changeset
4
5b3c6135a982 Imported from capsule None
devteam
parents:
diff changeset
5 from UserDict import UserDict
5b3c6135a982 Imported from capsule None
devteam
parents:
diff changeset
6
5b3c6135a982 Imported from capsule None
devteam
parents:
diff changeset
7 class odict(UserDict):
5b3c6135a982 Imported from capsule None
devteam
parents:
diff changeset
8 """
5b3c6135a982 Imported from capsule None
devteam
parents:
diff changeset
9 http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/107747
5b3c6135a982 Imported from capsule None
devteam
parents:
diff changeset
10
5b3c6135a982 Imported from capsule None
devteam
parents:
diff changeset
11 This dictionary class extends UserDict to record the order in which items are
5b3c6135a982 Imported from capsule None
devteam
parents:
diff changeset
12 added. Calling keys(), values(), items(), etc. will return results in this
5b3c6135a982 Imported from capsule None
devteam
parents:
diff changeset
13 order.
5b3c6135a982 Imported from capsule None
devteam
parents:
diff changeset
14 """
5b3c6135a982 Imported from capsule None
devteam
parents:
diff changeset
15 def __init__( self, dict = None ):
5b3c6135a982 Imported from capsule None
devteam
parents:
diff changeset
16 self._keys = []
5b3c6135a982 Imported from capsule None
devteam
parents:
diff changeset
17 UserDict.__init__( self, dict )
5b3c6135a982 Imported from capsule None
devteam
parents:
diff changeset
18
5b3c6135a982 Imported from capsule None
devteam
parents:
diff changeset
19 def __delitem__( self, key ):
5b3c6135a982 Imported from capsule None
devteam
parents:
diff changeset
20 UserDict.__delitem__( self, key )
5b3c6135a982 Imported from capsule None
devteam
parents:
diff changeset
21 self._keys.remove( key )
5b3c6135a982 Imported from capsule None
devteam
parents:
diff changeset
22
5b3c6135a982 Imported from capsule None
devteam
parents:
diff changeset
23 def __setitem__( self, key, item ):
5b3c6135a982 Imported from capsule None
devteam
parents:
diff changeset
24 UserDict.__setitem__( self, key, item )
5b3c6135a982 Imported from capsule None
devteam
parents:
diff changeset
25 if key not in self._keys:
5b3c6135a982 Imported from capsule None
devteam
parents:
diff changeset
26 self._keys.append( key )
5b3c6135a982 Imported from capsule None
devteam
parents:
diff changeset
27
5b3c6135a982 Imported from capsule None
devteam
parents:
diff changeset
28 def clear( self ):
5b3c6135a982 Imported from capsule None
devteam
parents:
diff changeset
29 UserDict.clear( self )
5b3c6135a982 Imported from capsule None
devteam
parents:
diff changeset
30 self._keys = []
5b3c6135a982 Imported from capsule None
devteam
parents:
diff changeset
31
5b3c6135a982 Imported from capsule None
devteam
parents:
diff changeset
32 def copy(self):
5b3c6135a982 Imported from capsule None
devteam
parents:
diff changeset
33 new = odict()
5b3c6135a982 Imported from capsule None
devteam
parents:
diff changeset
34 new.update( self )
5b3c6135a982 Imported from capsule None
devteam
parents:
diff changeset
35 return new
5b3c6135a982 Imported from capsule None
devteam
parents:
diff changeset
36
5b3c6135a982 Imported from capsule None
devteam
parents:
diff changeset
37 def items( self ):
5b3c6135a982 Imported from capsule None
devteam
parents:
diff changeset
38 return zip( self._keys, self.values() )
5b3c6135a982 Imported from capsule None
devteam
parents:
diff changeset
39
5b3c6135a982 Imported from capsule None
devteam
parents:
diff changeset
40 def keys( self ):
5b3c6135a982 Imported from capsule None
devteam
parents:
diff changeset
41 return self._keys[:]
5b3c6135a982 Imported from capsule None
devteam
parents:
diff changeset
42
5b3c6135a982 Imported from capsule None
devteam
parents:
diff changeset
43 def popitem( self ):
5b3c6135a982 Imported from capsule None
devteam
parents:
diff changeset
44 try:
5b3c6135a982 Imported from capsule None
devteam
parents:
diff changeset
45 key = self._keys[-1]
5b3c6135a982 Imported from capsule None
devteam
parents:
diff changeset
46 except IndexError:
5b3c6135a982 Imported from capsule None
devteam
parents:
diff changeset
47 raise KeyError( 'dictionary is empty' )
5b3c6135a982 Imported from capsule None
devteam
parents:
diff changeset
48 val = self[ key ]
5b3c6135a982 Imported from capsule None
devteam
parents:
diff changeset
49 del self[ key ]
5b3c6135a982 Imported from capsule None
devteam
parents:
diff changeset
50 return ( key, val )
5b3c6135a982 Imported from capsule None
devteam
parents:
diff changeset
51
5b3c6135a982 Imported from capsule None
devteam
parents:
diff changeset
52 def setdefault( self, key, failobj=None ):
5b3c6135a982 Imported from capsule None
devteam
parents:
diff changeset
53 if key not in self._keys:
5b3c6135a982 Imported from capsule None
devteam
parents:
diff changeset
54 self._keys.append( key )
5b3c6135a982 Imported from capsule None
devteam
parents:
diff changeset
55 return UserDict.setdefault( self, key, failobj )
5b3c6135a982 Imported from capsule None
devteam
parents:
diff changeset
56
5b3c6135a982 Imported from capsule None
devteam
parents:
diff changeset
57 def update( self, dict ):
5b3c6135a982 Imported from capsule None
devteam
parents:
diff changeset
58 for ( key, val ) in dict.items():
5b3c6135a982 Imported from capsule None
devteam
parents:
diff changeset
59 self.__setitem__( key, val )
5b3c6135a982 Imported from capsule None
devteam
parents:
diff changeset
60
5b3c6135a982 Imported from capsule None
devteam
parents:
diff changeset
61 def values( self ):
5b3c6135a982 Imported from capsule None
devteam
parents:
diff changeset
62 return map( self.get, self._keys )
5b3c6135a982 Imported from capsule None
devteam
parents:
diff changeset
63
5b3c6135a982 Imported from capsule None
devteam
parents:
diff changeset
64 def iterkeys( self ):
5b3c6135a982 Imported from capsule None
devteam
parents:
diff changeset
65 return iter( self._keys )
5b3c6135a982 Imported from capsule None
devteam
parents:
diff changeset
66
5b3c6135a982 Imported from capsule None
devteam
parents:
diff changeset
67 def itervalues( self ):
5b3c6135a982 Imported from capsule None
devteam
parents:
diff changeset
68 for key in self._keys:
5b3c6135a982 Imported from capsule None
devteam
parents:
diff changeset
69 yield self.get( key )
5b3c6135a982 Imported from capsule None
devteam
parents:
diff changeset
70
5b3c6135a982 Imported from capsule None
devteam
parents:
diff changeset
71 def iteritems( self ):
5b3c6135a982 Imported from capsule None
devteam
parents:
diff changeset
72 for key in self._keys:
5b3c6135a982 Imported from capsule None
devteam
parents:
diff changeset
73 yield key, self.get( key )
5b3c6135a982 Imported from capsule None
devteam
parents:
diff changeset
74
5b3c6135a982 Imported from capsule None
devteam
parents:
diff changeset
75 def __iter__( self ):
5b3c6135a982 Imported from capsule None
devteam
parents:
diff changeset
76 for key in self._keys:
5b3c6135a982 Imported from capsule None
devteam
parents:
diff changeset
77 yield key
5b3c6135a982 Imported from capsule None
devteam
parents:
diff changeset
78
5b3c6135a982 Imported from capsule None
devteam
parents:
diff changeset
79 def reverse( self ):
5b3c6135a982 Imported from capsule None
devteam
parents:
diff changeset
80 self._keys.reverse()
5b3c6135a982 Imported from capsule None
devteam
parents:
diff changeset
81
5b3c6135a982 Imported from capsule None
devteam
parents:
diff changeset
82 def insert( self, index, key, item ):
5b3c6135a982 Imported from capsule None
devteam
parents:
diff changeset
83 if key not in self._keys:
5b3c6135a982 Imported from capsule None
devteam
parents:
diff changeset
84 self._keys.insert( index, key )
5b3c6135a982 Imported from capsule None
devteam
parents:
diff changeset
85 UserDict.__setitem__( self, key, item )