# HG changeset patch # User Frederic Bastien # Date 1209997738 14400 # Node ID 427e02ef0629bdfe2049e2fde20e8cf4d8ec45e9 # Parent dde1fb1b63ba516c2d48768603ab140f63c90049 -bugfix. We need to make deep copy otherwise we modify this instance event if we should not do it. -added function __eq__ and __ne__ as otherwise we do pointor comparison diff -r dde1fb1b63ba -r 427e02ef0629 lookup_list.py --- a/lookup_list.py Fri May 02 11:24:17 2008 -0400 +++ b/lookup_list.py Mon May 05 10:28:58 2008 -0400 @@ -1,5 +1,5 @@ -from copy import copy +from copy import deepcopy class LookupList(object): """ @@ -17,7 +17,9 @@ print example.items() # prints [('x',[1,2,3]),('y',2),('z',3)] example.append_keyval('u',0) # adds item with name 'u' and value 0 print len(example) # number of items = 4 here - print example+example # addition is like for lists, a concatenation of the items. + example2 = LookupList(['v', 'w'], ['a','b']) + print example+example2 # addition is like for lists, a concatenation of the items. + example + example # throw an error as we can't have duplicate name. Note that the element names should be unique. """ def __init__(self,names=[],values=[]): @@ -75,15 +77,23 @@ return "{%s}" % ", ".join([str(k) + "=" + repr(v) for k,v in self.items()]) def __add__(self,rhs): - new_example = copy(self) + new_example = deepcopy(self) for item in rhs.items(): new_example.append_keyval(item[0],item[1]) return new_example def __radd__(self,lhs): - new_example = copy(lhs) + new_example = deepcopy(lhs) for item in self.items(): new_example.append_keyval(item[0],item[1]) return new_example + def __eq__(self, other): + return self._values==other._values and self._name2index==other._name2index and self._names==other._names + + def __ne__(self, other): + return not self.__eq__(other) + + def __hash__(): + raise NotImplementedError()