diff lookup_list.py @ 79:427e02ef0629

-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
author Frederic Bastien <bastienf@iro.umontreal.ca>
date Mon, 05 May 2008 10:28:58 -0400
parents a5c70dc42972
children 8fa1ef2411a0
line wrap: on
line diff
--- 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()