test
CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
seqvaluedict.py
Go to the documentation of this file.
1 # Sequential Dictionary Class #
2 '''
3 The sequential dictionary is a combination of a list and a dictionary so you can do most operations defined with lists .
4 seqdict - single value dictionary , keeps one value for one key
5 '''
6 
7 class seqdict:
8  def __init__(self,List=[],Dict={}):
9  if type(List)==type({}):
10  self.list = List.keys()
11  self.dict = List.copy()
12  elif List and not Dict:
13  self.list=[]
14  self.dict={}
15  for i,j in List:
16  self.list.append(i)
17  self.dict[i]=j
18  elif type(List)==type(Dict)==type([]):
19  self.list = List
20  self.dict = {}
21  for key,value in map(None,List,Dict):
22  self.dict[key] = value
23  else:
24  self.list,self.dict = List[:],Dict.copy()
25 
26  def append(self,key,value):
27  if self.dict.has_key(key):
28  self.list.remove(key)
29  self.list.append(key)
30  self.dict[key]=value
31  def check(self):
32  if len(self.dict)==len(self.list):
33  l1=self.list[:];l1.sort()
34  l2=self.dict.keys();l2.sort()
35  return l1==l2
36  return -1
37  def clear(self):
38  self.list=[];self.dict={}
39  def copy(self):
40  if self.__class__ is seqdict:
41  return self.__class__(self.list,self.dict)
42  import copy
43  return copy.copy(self)
44  def __cmp__(self,other):
45  return cmp(self.dict,other.dict) or cmp(self.list,other.list)
46  def __getitem__(self,key):
47  if type(key)==type([]):
48  newdict={}
49  for i in key:
50  newdict[i]=self.dict[i]
51  return self.__class__(key,newdict)
52  return self.dict[key]
53  def __setitem__(self,key,value):
54  if not self.dict.has_key(key):
55  self.list.append(key)
56  self.dict[key]=value
57  def __delitem__(self, key):
58  del self.dict[key]
59  self.list.remove(key)
60  def __getslice__(self,start,stop):
61  start = max(start,0); stop = max(stop,0)
62  newdict = self.__class__()
63  for key in self.list[start:stop]:
64  newdict.dict[key]=self.dict[key]
65  newdict.list[:]=self.list[start:stop]
66  return newdict
67  def __setslice__(self,start,stop,newdict):
68  start = max(start,0); stop = max(stop,0)
69  delindexes = []
70  for key in newdict.keys():
71  if self.dict.has_key(key):
72  index = self.list.index(key)
73  delindexes.append(index)
74  if index < start:
75  start = start - 1
76  stop = stop - 1
77  elif index >= stop:
78  pass
79  else:
80  stop = stop - 1
81  delindexes.sort()
82  delindexes.reverse()
83  for index in delindexes:
84  key = self.list[index]
85  del self.dict[key]
86  del self.list[index]
87  for key in self.list[start:stop]:
88  del self.dict[key]
89  self.list[start:stop] = newdict.list[:]
90  self.update(newdict.dict)
91  def __delslice__(self, start, stop):
92  start = max(start, 0); stop = max(stop, 0)
93  for key in self.list[start:stop]:
94  del self.dict[key]
95  del self.list[start:stop]
96  def __add__(self,other):
97  newdict = self.__class__()
98  for key,value in self.items()+other.items():
99  newdict.append(key,value)
100  return newdict
101  def __radd__(self,other):
102  newdict = self.__class__()
103  for key,value in other.items()+self.items():
104  newdict.append(key,value)
105  return newdict
106  def count(self,value):
107  vallist = self.dict.values()
108  return vallist.count(value)
109  def extend(self,other):
110  self.update(other)
111  def filter(self,function):
112  liste=filter(function,self.list)
113  dict = {}
114  for i in liste:
115  dict[i]=self.dict[i]
116  return self.__class__(liste,dict)
117  def get(self, key, failobj=None):
118  return self.dict.get(key, failobj)
119  def index(self,key):return self.list.index(key)
120  def insert(self,i,x):self.__setslice__(i,i,x)
121  def items(self):return map(None,self.list,self.values())
122  def has_key(self,key):return self.dict.has_key(key)
123  def keys(self):return self.list
124  def map(self,function):
125  return self.__class__(map(function,self.items()))
126  def values(self):
127  nlist = []
128  for key in self.list:
129  nlist.append(self.dict[key])
130  return nlist
131  def __len__(self):return len(self.list)
132  def pop(self,key=None):
133  if key==None:
134  pos = -1
135  key = self.list[pos]
136  else:
137  pos = self.list.index(key)
138  tmp = self.dict[key]
139  del self.dict[key]
140  return {self.list.pop(pos):tmp}
141  def push(self,key,value):
142  self.append(key,value)
143  def reduce(self,function,start=None):
144  return reduce(function,self.items(),start)
145  def remove(self,key):
146  del self.dict[key]
147  self.list.remove(key)
148  def reverse(self):self.list.reverse()
149  def sort(self,*args):apply(self.list.sort,args)
150  def split(self,function,Ignore=None):
151  splitdict = seqdict() #self.__class__()
152  for key in self.list:
153  skey = function(key)
154  if skey != Ignore:
155  if not splitdict.has_key(skey):
156  splitdict[skey] = self.__class__()
157  splitdict[skey][key] = self.dict[key]
158  return splitdict
159  def swap(self):
160  tmp = self.__class__(map(lambda (x,y):(y,x),self.items()))
161  self.list,self.dict = tmp.list,tmp.dict
162  def update(self,newdict):
163  for key,value in newdict.items():
164  self.__setitem__(key,value)
165  def slice(self,From,To=None,Step=1):
166  From = self.list.index(From)
167  if To:To = self.list.index(To)
168  else :
169  To = From + 1
170  List = range(From,To,Step)
171  def getitem(pos,self=self):return self.list[pos]
172  return self.__getitem__(map(getitem,List))
173  def __repr__(self):return 'seqdict(\n%s,\n%s)'%(self.list,self.dict)
174 
Definition: vlib.h:256