comparison lookup_list.py @ 22:b6b36f65664f

Created virtual sub-classes of DataSet: {Finite{Length,Width},Sliceable}DataSet, removed .field ability from LookupList (because of setattr problems), removed fieldNames() from DataSet (but is in FiniteWidthDataSet, where it makes sense), and added hasFields() instead. Fixed problems in asarray, and tested previous functionality in _test_dataset.py, but not yet new functionality.
author bengioy@esprit.iro.umontreal.ca
date Mon, 07 Apr 2008 20:44:37 -0400
parents 266c68cb6136
children a5c70dc42972
comparison
equal deleted inserted replaced
21:fdf0abc490f7 22:b6b36f65664f
1
2 from copy import copy
1 3
2 class LookupList(object): 4 class LookupList(object):
3 """ 5 """
4 A LookupList is a sequence whose elements can be named (and unlike 6 A LookupList is a sequence whose elements can be named (and unlike
5 a dictionary the order of the elements depends not on their key but 7 a dictionary the order of the elements depends not on their key but
6 on the order given by the user through construction) so that 8 on the order given by the user through construction) so that
7 following syntactic constructions work as one would expect: 9 following syntactic constructions work as one would expect:
8 example = Example(['x','y','z'],[1,2,3]) 10 example = LookupList(['x','y','z'],[1,2,3])
9 example.x = [1, 2, 3] # set or change a field 11 example['x'] = [1, 2, 3] # set or change a field
10 x, y, z = example 12 x, y, z = example
11 x = example[0] 13 x = example[0]
12 x = example["x"] 14 x = example["x"]
13 print example.keys() # returns ['x','y','z'] 15 print example.keys() # prints ['x','y','z']
14 print example.values() # returns [[1,2,3],2,3] 16 print example.values() # prints [[1,2,3],2,3]
17 print example.items() # prints [('x',[1,2,3]),('y',2),('z',3)]
18 example.append_keyval('u',0) # adds item with name 'u' and value 0
19 print len(example) # number of items = 4 here
20 print example+example # addition is like for lists, a concatenation of the items.
21 Note that the element names should be unique.
15 """ 22 """
16 def __init__(self,names=[],values=[]): 23 def __init__(self,names=[],values=[]):
17 assert len(values)==len(names) 24 assert len(values)==len(names)
18 self.__dict__['_values']=values 25 self.__dict__['_values']=values
19 self.__dict__['_name2index']={} 26 self.__dict__['_name2index']={}
20 self.__dict__['_names']=names 27 self.__dict__['_names']=names
21 for i in xrange(len(values)): 28 for i in xrange(len(values)):
29 assert names[i] not in self._name2index
22 self._name2index[names[i]]=i 30 self._name2index[names[i]]=i
23 31
24 def keys(self): 32 def keys(self):
25 return self._names 33 return self._names
26 34
27 def values(self): 35 def values(self):
28 return self._values 36 return self._values
29 37
30 def items(self): 38 def items(self):
39 """
40 Return a list of (name,value) pairs of all the items in the look-up list.
41 """
31 return zip(self._names,self._values) 42 return zip(self._names,self._values)
32 43
33 def __getitem__(self,key): 44 def __getitem__(self,key):
34 """ 45 """
35 The key in example[key] can either be an integer to index the fields 46 The key in example[key] can either be an integer to index the fields
45 self._values[key]=value 56 self._values[key]=value
46 else: # if not an int, key must be a name 57 else: # if not an int, key must be a name
47 if key in self._name2index: 58 if key in self._name2index:
48 self._values[self._name2index[key]]=value 59 self._values[self._name2index[key]]=value
49 else: 60 else:
50 raise KeyError(key) 61 self.append_keyval(key,value)
51 62
52 def __getattr__(self,name):
53 try:
54 return self._values[self._name2index[name]]
55 except KeyError, e:
56 raise AttributeError(name)
57
58 def append_keyval(self, key, value): 63 def append_keyval(self, key, value):
64 assert key not in self._name2index
59 self._name2index[key]=len(self) 65 self._name2index[key]=len(self)
60 self._values.append(value) 66 self._values.append(value)
61 self._names.append(key) 67 self._names.append(key)
62 68
63 def __len__(self): 69 def __len__(self):
64 return len(self._values) 70 return len(self._values)
65 71
66 def __repr__(self): 72 def __repr__(self):
67 return "{%s}" % ", ".join([str(k) + "=" + repr(v) for k,v in self.items()]) 73 return "{%s}" % ", ".join([str(k) + "=" + repr(v) for k,v in self.items()])
74
75 def __add__(self,rhs):
76 new_example = copy(self)
77 for item in rhs.items():
78 new_example.append_keyval(item[0],item[1])
79 return new_example
80
81 def __radd__(self,lhs):
82 new_example = copy(lhs)
83 for item in self.items():
84 new_example.append_keyval(item[0],item[1])
85 return new_example
86
87