comparison lookup_list.py @ 19:57f4015e2e09

Iterators extend LookupList
author bergstrj@iro.umontreal.ca
date Thu, 27 Mar 2008 01:59:44 -0400
parents 759d17112b23
children 266c68cb6136
comparison
equal deleted inserted replaced
18:60b164a0d84a 19:57f4015e2e09
45 self._values[key]=value 45 self._values[key]=value
46 else: # if not an int, key must be a name 46 else: # if not an int, key must be a name
47 if key in self._name2index: 47 if key in self._name2index:
48 self._values[self._name2index[key]]=value 48 self._values[self._name2index[key]]=value
49 else: 49 else:
50 self._name2index[key]=len(self) 50 raise KeyError(key)
51 self._values.append(value)
52 self._names.append(key)
53 51
54 def __getattr__(self,name): 52 def __getattr__(self,name):
55 return self._values[self._name2index[name]] 53 try:
54 return self._values[self._name2index[name]]
55 except KeyError, e:
56 raise AttributeError(name)
56 57
57 def __setattr__(self,name,value): 58 if 0:
58 if name in self._name2index: 59 # This makes subclassing horrible, just call append_keyval if it's
59 self._values[self._name2index[name]]=value 60 # really what you want to do.
60 else: 61 # -JB
61 self._name2index[name]=len(self) 62 def __setattr__(self,name,value):
62 self._values.append(value) 63 if name in self._name2index:
63 self._names.append(name) 64 self._values[self._name2index[name]]=value
65 else:
66 raise AttributeError(name)
67
68 def append_keyval(self, key, value):
69 self._name2index[key]=len(self)
70 self._values.append(value)
71 self._names.append(key)
64 72
65 def __len__(self): 73 def __len__(self):
66 return len(self._values) 74 return len(self._values)
67 75
68 def __repr__(self): 76 def __repr__(self):