IMP  2.0.1
The Integrative Modeling Platform
OrderedDict.py
1 ##
2 ## The Inferential Structure Determination (ISD) software library
3 ##
4 ## Authors: Michael Habeck and Wolfgang Rieping
5 ##
6 ## Copyright (C) Michael Habeck and Wolfgang Rieping
7 ##
8 ## All rights reserved.
9 ##
10 ## NO WARRANTY. This library is provided 'as is' without warranty of any
11 ## kind, expressed or implied, including, but not limited to the implied
12 ## warranties of merchantability and fitness for a particular purpose or
13 ## a warranty of non-infringement.
14 ##
15 ## Distribution of substantively modified versions of this module is
16 ## prohibited without the explicit permission of the copyright holders.
17 ##
18 
19 
20 class OrderedDict(dict):
21 
22  def __init__(self, order = None):
23 
24  dict.__init__(self)
25 
26  if order is None:
27  order = []
28 
29  self.__order = order
30 
31  def keys(self):
32 
33  if self.__order is not None:
34  return self.__order
35  else:
36  return dict.keys(self)
37 
38  def values(self):
39  return map(lambda k, s = self: s[k], self.keys())
40 
41  def items(self):
42  return map(lambda k, s = self: (k, s[k]), self.keys())
43 
44  def get_first(self):
45  return dict.__getitem__(self, self.__order[0])
46 
47  def pop_first(self):
48 
49  key = self.__order[0]
50 
51  return self.pop(key)
52 
53  def get_first_item(self):
54 
55  key = self.__order[0]
56  value = dict.__getitem__(self, key)
57 
58  return key, value
59 
60  def pop_first_item(self):
61 
62  key = self.__order[0]
63 
64  return key, self.pop(key)
65 
66  def pop(self, key):
67 
68  value = dict.__getitem__(self, key)
69 
70  del self[key]
71 
72  return value
73 
74  def __setitem__(self, key, value):
75 
76  if key not in self.__order:
77  self.__order.append(key)
78 
79  dict.__setitem__(self, key, value)
80 
81  def __delitem__(self, key):
82  dict.__delitem__(self, key)
83  self.__order.remove(key)