CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
multivaluedict.py
Go to the documentation of this file.
1 # Sequential Dictionary Class #
2 '''
3 msedict - multiple value dictionary, keeps multiple values per key
4 You can slice, add, sort with dictionaries. The functional operations map, filter, reduce can also be used .
5 The dictionary can also be used like a stack with push and pop. It can be used to Split , reverse and swap keys and values.
6 '''
7 
8 from seqvaluedict import seqdict #Sequential Single Value Dictionary
9 from UserList import UserList
10 
11 class MyUserList(UserList):
12  from UserList import UserList
13  def __init__(self,parent,liste=None):
14  UserList.__init__(self,liste)
15  self.parent = parent #remember parent for call-back
16  def __delitem__(self, i):
17  del self.data[i]
18  if self.data==[]: #call-back, deletes item of parent
19  index = self.parent.values().index([])
20  del self.parent[index:index+1]
21 
22 class mseqdict(seqdict): #Sequential Multiple Value Dictionary
23  def __init__(self,List=[],Dict={}):
24  self.list = []
25  self.dict = {}
26  if not List:
27  pass
28  elif type(List)==type({}):
29  for key,value in List.items():
30  self.__setitem__(key,value)
31  elif List and not Dict: #dict.items()
32  for key,value in List:
33  if isinstance(value,MyUserList):
34  for v in value:
35  self.__setitem__(key,v)
36  else:
37  self.__setitem__(key,value)
38  elif type(List)==type(Dict)==type([]):
39  for key,value in map(None,List,Dict):
40  self.__setitem__(key,value)
41  else:
42  if isinstance(Dict.values()[0],MyUserList):
43  self.dict = Dict
44  self.list = List
45  else:
46  for key in List:
47  value = Dict[key]
48  if type(value)==type([]):
49  for v in value:
50  self.__setitem__(key,v)
51  else:
52  self.__setitem__(key,value)
53  def __setitem__(self,key,value):
54  if not self.dict.has_key(key):
55  self.list.append(key)
56  if isinstance(value,MyUserList):
57  self.dict[key] = value
58  else:
59  self.dict[key]=MyUserList(self,[value])
60  else:
61  values = self.dict[key]
62  if isinstance(value,MyUserList):
63  for v in value:
64  if not v in values:
65  values.extend(MyUserList(self,[v]))
66  else:
67  if not value in values:
68  values.extend(MyUserList(self,[value]))
69  def __delitem__(self, key):
70  del self.dict[key]
71  self.list.remove(key)
72 
73  def append(self,key,value):
74  self.__setitem__(key,value)
75  def __setslice__(self,start,stop,newdict):
76  start = max(start,0); stop = max(stop,0)
77  delindexes = []
78  for key in newdict.keys():
79  if self.dict.has_key(key):
80  index = self.list.index(key)
81  delindexes.append(index)
82  if index < start:
83  start = start - 1
84  stop = stop - 1
85  elif index >= stop:
86  pass
87  else:
88  stop = stop - 1
89  else:
90  self.dict[key]=UserList(self)
91  delindexes.sort()
92  delindexes.reverse()
93  for index in delindexes:
94  key = self.list[index]
95  #del self.dict[key]
96  del self.list[index]
97  self.list[start:stop] = newdict.list[:]
98  self.update(newdict.dict)
99  def copy(self):
100  values = map(lambda x:x[:],self.values())
101  return self.__class__(self.list,values)
102  def count(self,value):
103  vallist = self.dict.values()
104  return map(lambda x,y=value:x.count(y),vallist).count(1)
105  def filter(self,function,filtervalues=0):
106  if filtervalues == 1: #consider key and all keyvalues at once
107  dict = self.__class__()
108  for key,values in self.items():
109  if function(key,values):
110  dict[key]=values
111  return dict
112  elif filtervalues == 2: #consider key and every keyvalue for itself
113  dict = self.__class__()
114  for key,values in self.items():
115  for value in values:
116  if function(key,value):
117  dict[key]=value
118  return dict
119  else: #consider key only
120  liste=filter(function,self.list)
121  dict = {}
122  for i in liste:
123  dict[i]=self.dict[i]
124  return self.__class__(liste,dict)
125  def map(self,function,mapvalues=2):
126  if mapvalues == 1: #consider key and all keyvalues at once
127  dict = self.__class__()
128  for key,values in self.items():
129  k,v = function(key,values)
130  dict[k]=v
131  return dict
132  else: #if mapvalues!=1: #consider key and every keyvalue for itself
133  dict = self.__class__()
134  for key,values in self.items():
135  for value in values:
136  k,v = function(key,value)
137  dict[k]=v
138  return dict
139  def pop(self,key='...None',value='...None'):
140  if value=='...None':
141  if key=='...None':
142  pos = -1
143  key = self.list[pos]
144  else:
145  pos = self.list.index(key)
146  tmp = self.dict[key]
147  del self.dict[key]
148  return {self.list.pop(pos):tmp}
149  else:
150  val = self.dict[key]
151  index = val.index(value)
152  tmp = val[index]
153  del val[index]
154  return {key:tmp}
155  def remove(self,key,value='...None'):
156  if value=='...None':
157  del self[key]
158  else:
159  index = self[key].index(value)
160  del self[key][index]
161  def sort(self,func1=None,func2=None):
162  if not func1:
163  self.list.sort()
164  else:
165  apply(self.list.sort,[func1])
166  if func2:
167  for value in self.values():
168  apply(value.sort,[func2])
169 
170  def swap(self):
171  tmp = self.__class__()
172  for key,values in self.items():
173  for value in values:
174  tmp[value]=key
175  self.list,self.dict = tmp.list,tmp.dict
176  del tmp
177 
178  def __repr__(self):return 'mseqdict(\n%s,\n%s)'%(self.list,self.dict)
179 
Definition: vlib.h:256
Vec apply(Vec v, F f)
Definition: ExtVec.h:95