annotate utils/odict.py @ 2:d94e778c3ad1 draft

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