Mercurial > pylearn
diff lookup_list.py @ 17:759d17112b23
more comments, looping ArrayDataSet iterator, bugfixes to lookup_list, more tests
author | bergstrj@iro.umontreal.ca |
---|---|
date | Wed, 26 Mar 2008 21:05:14 -0400 |
parents | ff4e551490f1 |
children | 57f4015e2e09 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lookup_list.py Wed Mar 26 21:05:14 2008 -0400 @@ -0,0 +1,69 @@ + +class LookupList(object): + """ + A LookupList is a sequence whose elements can be named (and unlike + a dictionary the order of the elements depends not on their key but + on the order given by the user through construction) so that + following syntactic constructions work as one would expect: + example = Example(['x','y','z'],[1,2,3]) + example.x = [1, 2, 3] # set or change a field + x, y, z = example + x = example[0] + x = example["x"] + print example.keys() # returns ['x','y','z'] + print example.values() # returns [[1,2,3],2,3] + """ + def __init__(self,names=[],values=[]): + assert len(values)==len(names) + self.__dict__['_values']=values + self.__dict__['_name2index']={} + self.__dict__['_names']=names + for i in xrange(len(values)): + self._name2index[names[i]]=i + + def keys(self): + return self._names + + def values(self): + return self._values + + def items(self): + return zip(self._names,self._values) + + def __getitem__(self,key): + """ + The key in example[key] can either be an integer to index the fields + or the name of the field. + """ + if isinstance(key,int): + return self._values[key] + else: # if not an int, key must be a name + return self._values[self._name2index[key]] + + def __setitem__(self,key,value): + if isinstance(key,int): + self._values[key]=value + else: # if not an int, key must be a name + if key in self._name2index: + self._values[self._name2index[key]]=value + else: + self._name2index[key]=len(self) + self._values.append(value) + self._names.append(key) + + def __getattr__(self,name): + return self._values[self._name2index[name]] + + def __setattr__(self,name,value): + if name in self._name2index: + self._values[self._name2index[name]]=value + else: + self._name2index[name]=len(self) + self._values.append(value) + self._names.append(name) + + def __len__(self): + return len(self._values) + + def __repr__(self): + return "{%s}" % ", ".join([str(k) + "=" + repr(v) for k,v in self.items()])