comparison utils/odict.py @ 5:0d97b11ed3d5 draft default tip

planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
author devteam
date Thu, 22 Jun 2017 18:51:24 -0400
parents
children
comparison
equal deleted inserted replaced
4:a43e5a6390c1 5:0d97b11ed3d5
1 """
2 Ordered dictionary implementation.
3 """
4
5 from UserDict import UserDict
6
7
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 """
16 def __init__( self, dict=None ):
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 )