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