diff 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
line wrap: on
line diff
--- a/lookup_list.py	Mon Apr 07 19:32:52 2008 -0400
+++ b/lookup_list.py	Mon Apr 07 20:44:37 2008 -0400
@@ -1,3 +1,5 @@
+
+from copy import copy
 
 class LookupList(object):
     """
@@ -5,13 +7,18 @@
     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
+       example = LookupList(['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]
+       print example.keys() # prints ['x','y','z']
+       print example.values() # prints [[1,2,3],2,3]
+       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.
+    Note that the element names should be unique.
     """
     def __init__(self,names=[],values=[]):
         assert len(values)==len(names)
@@ -19,6 +26,7 @@
         self.__dict__['_name2index']={}
         self.__dict__['_names']=names
         for i in xrange(len(values)):
+            assert names[i] not in self._name2index
             self._name2index[names[i]]=i
 
     def keys(self):
@@ -28,6 +36,9 @@
         return self._values
 
     def items(self):
+        """
+        Return a list of (name,value) pairs of all the items in the look-up list.
+        """
         return zip(self._names,self._values)
     
     def __getitem__(self,key):
@@ -47,15 +58,10 @@
             if key in self._name2index:
                 self._values[self._name2index[key]]=value
             else:
-                raise KeyError(key)
-
-    def __getattr__(self,name):
-        try:
-            return self._values[self._name2index[name]]
-        except KeyError, e:
-            raise AttributeError(name)
-
+                self.append_keyval(key,value)
+            
     def append_keyval(self, key, value):
+        assert key not in self._name2index
         self._name2index[key]=len(self)
         self._values.append(value)
         self._names.append(key)
@@ -65,3 +71,17 @@
 
     def __repr__(self):
         return "{%s}" % ", ".join([str(k) + "=" + repr(v) for k,v in self.items()])
+
+    def __add__(self,rhs):
+        new_example = copy(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)
+        for item in self.items():
+            new_example.append_keyval(item[0],item[1])
+        return new_example
+
+