comparison planemo/lib/python3.7/site-packages/galaxy/util/odict.py @ 1:56ad4e20f292 draft

"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
author guerler
date Fri, 31 Jul 2020 00:32:28 -0400
parents
children
comparison
equal deleted inserted replaced
0:d30785e31577 1:56ad4e20f292
1 """
2 Ordered dictionary implementation with `insert` functionality.
3
4 This is only used in one specific place in the codebase:
5 galaxy.tools.toolbox.panel
6
7 Whenever possible the stdlib `collections.OrderedDict` should be used instead of
8 this custom implementation.
9 """
10
11 from six.moves import UserDict
12 dict_alias = dict
13
14
15 class odict(UserDict):
16 """
17 http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/107747
18
19 This dictionary class extends UserDict to record the order in which items are
20 added. Calling keys(), values(), items(), etc. will return results in this
21 order.
22 """
23
24 def __init__(self, dict=None):
25 item = dict
26 self._keys = []
27 if isinstance(item, dict_alias):
28 UserDict.__init__(self, item)
29 else:
30 UserDict.__init__(self, None)
31 if isinstance(item, list):
32 for (key, value) in item:
33 self[key] = value
34
35 def __delitem__(self, key):
36 UserDict.__delitem__(self, key)
37 self._keys.remove(key)
38
39 def __setitem__(self, key, item):
40 UserDict.__setitem__(self, key, item)
41 if key not in self._keys:
42 self._keys.append(key)
43
44 def clear(self):
45 UserDict.clear(self)
46 self._keys = []
47
48 def copy(self):
49 new = odict()
50 new.update(self)
51 return new
52
53 def items(self):
54 return zip(self._keys, self.values())
55
56 def keys(self):
57 return self._keys[:]
58
59 def popitem(self):
60 try:
61 key = self._keys[-1]
62 except IndexError:
63 raise KeyError('dictionary is empty')
64 val = self[key]
65 del self[key]
66 return (key, val)
67
68 def setdefault(self, key, failobj=None):
69 if key not in self._keys:
70 self._keys.append(key)
71 return UserDict.setdefault(self, key, failobj)
72
73 def update(self, dict):
74 for (key, val) in dict.items():
75 self.__setitem__(key, val)
76
77 def values(self):
78 return map(self.get, self._keys)
79
80 def iterkeys(self):
81 return iter(self._keys)
82
83 def itervalues(self):
84 for key in self._keys:
85 yield self.get(key)
86
87 def iteritems(self):
88 for key in self._keys:
89 yield key, self.get(key)
90
91 def __iter__(self):
92 for key in self._keys:
93 yield key
94
95 def reverse(self):
96 self._keys.reverse()
97
98 def insert(self, index, key, item):
99 if key not in self._keys:
100 self._keys.insert(index, key)
101 UserDict.__setitem__(self, key, item)