CMS 3D CMS Logo

/data/doxygen/doxygen-1.7.3/gen/CMSSW_4_2_8/src/CondCore/TagCollection/python/seqvaluedict.py

Go to the documentation of this file.
00001 #  Sequential Dictionary Class                                                 #
00002 '''
00003 The sequential dictionary is a combination of a list and a dictionary so you can do most operations defined with lists . 
00004 seqdict - single value dictionary , keeps one value for one key 
00005 '''
00006 
00007 class seqdict:
00008   def __init__(self,List=[],Dict={}):
00009     if type(List)==type({}):
00010       self.list = List.keys()
00011       self.dict = List.copy()
00012     elif List and not Dict:
00013       self.list=[]
00014       self.dict={}
00015       for i,j in List:
00016         self.list.append(i)
00017         self.dict[i]=j
00018     elif type(List)==type(Dict)==type([]):
00019       self.list = List
00020       self.dict = {}
00021       for key,value in map(None,List,Dict):
00022         self.dict[key] = value
00023     else:
00024       self.list,self.dict = List[:],Dict.copy()
00025       
00026   def append(self,key,value):
00027     if self.dict.has_key(key):
00028       self.list.remove(key)
00029     self.list.append(key)
00030     self.dict[key]=value
00031   def check(self):
00032     if len(self.dict)==len(self.list):
00033       l1=self.list[:];l1.sort()
00034       l2=self.dict.keys();l2.sort()
00035       return l1==l2
00036     return -1
00037   def clear(self):
00038     self.list=[];self.dict={}
00039   def copy(self):
00040     if self.__class__ is seqdict:
00041       return self.__class__(self.list,self.dict)
00042     import copy
00043     return copy.copy(self)
00044   def __cmp__(self,other):
00045     return cmp(self.dict,other.dict) or cmp(self.list,other.list)
00046   def __getitem__(self,key):
00047     if type(key)==type([]):
00048       newdict={}
00049       for i in key:
00050         newdict[i]=self.dict[i]
00051       return self.__class__(key,newdict)
00052     return self.dict[key]
00053   def __setitem__(self,key,value):
00054     if not self.dict.has_key(key):
00055       self.list.append(key)
00056     self.dict[key]=value
00057   def __delitem__(self, key):
00058     del self.dict[key]
00059     self.list.remove(key)      
00060   def __getslice__(self,start,stop):
00061     start = max(start,0); stop = max(stop,0)
00062     newdict = self.__class__()
00063     for key in self.list[start:stop]:
00064       newdict.dict[key]=self.dict[key]
00065     newdict.list[:]=self.list[start:stop]
00066     return newdict
00067   def __setslice__(self,start,stop,newdict):
00068     start = max(start,0); stop = max(stop,0)
00069     delindexes = []
00070     for key in newdict.keys():
00071       if self.dict.has_key(key):
00072         index = self.list.index(key)
00073         delindexes.append(index)
00074         if index < start:
00075           start = start - 1
00076           stop  = stop  - 1
00077         elif index >= stop:
00078           pass
00079         else:
00080           stop  = stop - 1
00081     delindexes.sort()
00082     delindexes.reverse()
00083     for index in delindexes:
00084       key = self.list[index]
00085       del   self.dict[key]
00086       del  self.list[index]
00087     for key in self.list[start:stop]:
00088       del self.dict[key]
00089     self.list[start:stop] = newdict.list[:]
00090     self.update(newdict.dict)
00091   def __delslice__(self, start, stop):
00092       start = max(start, 0); stop = max(stop, 0)
00093       for key in self.list[start:stop]:
00094         del self.dict[key]
00095       del self.list[start:stop]
00096   def __add__(self,other):
00097     newdict = self.__class__()
00098     for key,value in self.items()+other.items():
00099       newdict.append(key,value)
00100     return newdict    
00101   def __radd__(self,other):
00102     newdict = self.__class__()
00103     for key,value in other.items()+self.items():
00104       newdict.append(key,value)
00105     return newdict   
00106   def count(self,value):
00107     vallist = self.dict.values()
00108     return vallist.count(value)
00109   def extend(self,other):
00110     self.update(other)
00111   def filter(self,function):
00112     liste=filter(function,self.list)
00113     dict = {}
00114     for i in liste:
00115       dict[i]=self.dict[i]
00116     return self.__class__(liste,dict)
00117   def get(self, key, failobj=None):
00118         return self.dict.get(key, failobj)
00119   def index(self,key):return self.list.index(key)
00120   def insert(self,i,x):self.__setslice__(i,i,x)
00121   def items(self):return map(None,self.list,self.values())
00122   def has_key(self,key):return self.dict.has_key(key)
00123   def keys(self):return self.list
00124   def map(self,function):
00125     return self.__class__(map(function,self.items()))
00126   def values(self):
00127     nlist = []
00128     for key in self.list:
00129       nlist.append(self.dict[key])
00130     return nlist
00131   def __len__(self):return len(self.list)
00132   def pop(self,key=None):
00133     if key==None:
00134       pos = -1
00135       key = self.list[pos]
00136     else:
00137       pos = self.list.index(key)
00138     tmp = self.dict[key]
00139     del self.dict[key]
00140     return {self.list.pop(pos):tmp}
00141   def push(self,key,value):
00142     self.append(key,value)
00143   def reduce(self,function,start=None):
00144     return reduce(function,self.items(),start)
00145   def remove(self,key):
00146     del self.dict[key]
00147     self.list.remove(key)
00148   def reverse(self):self.list.reverse()
00149   def sort(self,*args):apply(self.list.sort,args)
00150   def split(self,function,Ignore=None):
00151     splitdict = seqdict() #self.__class__()
00152     for key in self.list:
00153       skey = function(key)
00154       if skey != Ignore:
00155         if not splitdict.has_key(skey):
00156           splitdict[skey] = self.__class__()
00157         splitdict[skey][key] = self.dict[key]
00158     return splitdict
00159   def swap(self):
00160     tmp = self.__class__(map(lambda (x,y):(y,x),self.items()))
00161     self.list,self.dict = tmp.list,tmp.dict
00162   def update(self,newdict):
00163     for key,value in newdict.items():
00164       self.__setitem__(key,value)
00165   def slice(self,From,To=None,Step=1):
00166     From       = self.list.index(From)
00167     if To:To   = self.list.index(To)
00168     else :
00169       To = From + 1
00170     List = range(From,To,Step)
00171     def getitem(pos,self=self):return self.list[pos]
00172     return self.__getitem__(map(getitem,List))
00173   def __repr__(self):return 'seqdict(\n%s,\n%s)'%(self.list,self.dict)
00174