changeset 269:fdce496c3b56

deprecating __getitem__[fieldname] syntax
author James Bergstra <bergstrj@iro.umontreal.ca>
date Wed, 04 Jun 2008 19:04:40 -0400
parents 3f1cd8897fda
children fa8abc813bd2
files dataset.py test_dataset.py
diffstat 2 files changed, 57 insertions(+), 49 deletions(-) [+]
line wrap: on
line diff
--- a/dataset.py	Wed Jun 04 18:48:50 2008 -0400
+++ b/dataset.py	Wed Jun 04 19:04:40 2008 -0400
@@ -109,10 +109,6 @@
 
      - dataset[[i1,i2,...in]] returns a dataset with examples i1,i2,...in.
 
-     - dataset[fieldname] an iterable over the values of the field fieldname across
-     the dataset (the iterable is obtained by default by calling valuesVStack
-     over the values for individual examples).
-
      - dataset.<property> returns the value of a property associated with
      the name <property>. The following properties should be supported:
           - 'description': a textual description or name for the dataset
@@ -151,9 +147,9 @@
        - __len__ if it is not a stream
        - fieldNames
        - minibatches_nowrap (called by DataSet.minibatches())
+    For efficiency of implementation, a sub-class might also want to redefine
        - valuesHStack
        - valuesVStack
-    For efficiency of implementation, a sub-class might also want to redefine
        - hasFields
        - __getitem__ may not be feasible with some streams
        - __iter__
@@ -412,6 +408,20 @@
         """
         return DataSetFields(self,fieldnames)
 
+    def getitem_key(self, fieldname):
+        """A not-so-well thought-out place to put code that used to be in
+        getitem.
+        """
+        #removing as per discussion June 4. --JSB
+
+        i = fieldname
+        # else check for a fieldname
+        if self.hasFields(i):
+            return self.minibatches(fieldnames=[i],minibatch_size=len(self),n_batches=1,offset=0).next()[0]
+        # else we are trying to access a property of the dataset
+        assert i in self.__dict__ # else it means we are trying to access a non-existing property
+        return self.__dict__[i]
+
     def __getitem__(self,i):
         """
         dataset[i] returns the (i+1)-th example of the dataset.
@@ -460,12 +470,7 @@
                                             for fieldname,field_values
                                             in zip(self.fieldNames(),fields_values)]),
                 self.valuesVStack,self.valuesHStack)
-        # else check for a fieldname
-        if self.hasFields(i):
-            return self.minibatches(fieldnames=[i],minibatch_size=len(self),n_batches=1,offset=0).next()[0]
-        # else we are trying to access a property of the dataset
-        assert i in self.__dict__ # else it means we are trying to access a non-existing property
-        return self.__dict__[i]
+        raise TypeError(i, type(i))
 
     def valuesHStack(self,fieldnames,fieldvalues):
         """
--- a/test_dataset.py	Wed Jun 04 18:48:50 2008 -0400
+++ b/test_dataset.py	Wed Jun 04 19:04:40 2008 -0400
@@ -305,49 +305,52 @@
 #ds[fieldname]# an iterable over the values of the field fieldname across
   #the ds (the iterable is obtained by default by calling valuesVStack
   #over the values for individual examples).
-    assert have_raised("ds['h']")  # h is not defined...
-    assert have_raised("ds[['x']]")  # bad syntax
-    assert not have_raised("var['ds']['x']",ds=ds)
-    isinstance(ds['x'],DataSetFields)
-    ds2=ds['x']
-    assert len(ds['x'])==10
-    assert len(ds['y'])==10
-    assert len(ds['z'])==10
-    i=0
-    for example in ds['x']:
-        assert (example==array[i][:3]).all()
-        i+=1
-    assert i==len(ds)
-    i=0
-    for example in ds['y']:
-        assert (example==array[i][3]).all()
-        i+=1
-    assert i==len(ds)
-    i=0
-    for example in ds['z']:
-        assert (example==array[i,0:3:2]).all()
-        i+=1
-    assert i==len(ds)
-    del ds2,i
+    if 0:
+        assert have_raised("ds['h']")  # h is not defined...
+        assert have_raised("ds[['x']]")  # bad syntax
+        assert not have_raised("var['ds']['x']",ds=ds)
+        isinstance(ds['x'],DataSetFields)
+        ds2=ds['x']
+        assert len(ds['x'])==10
+        assert len(ds['y'])==10
+        assert len(ds['z'])==10
+        i=0
+        for example in ds['x']:
+            assert (example==array[i][:3]).all()
+            i+=1
+        assert i==len(ds)
+        i=0
+        for example in ds['y']:
+            assert (example==array[i][3]).all()
+            i+=1
+        assert i==len(ds)
+        i=0
+        for example in ds['z']:
+            assert (example==array[i,0:3:2]).all()
+            i+=1
+        assert i==len(ds)
+        del ds2,i
+    else:
+        print 'warning: ds[fieldname] is deprecated... Fred could you fix this test?'
 
-#ds.<property># returns the value of a property associated with
-  #the name <property>. The following properties should be supported:
-  #    - 'description': a textual description or name for the ds
-  #    - 'fieldtypes': a list of types (one per field)
+    #ds.<property># returns the value of a property associated with
+      #the name <property>. The following properties should be supported:
+      #    - 'description': a textual description or name for the ds
+      #    - 'fieldtypes': a list of types (one per field)
 
-#* ds1 | ds2 | ds3 == ds.hstack([ds1,ds2,ds3])#????
-    #assert hstack([ds('x','y'),ds('z')])==ds
-    #hstack([ds('z','y'),ds('x')])==ds
+    #* ds1 | ds2 | ds3 == ds.hstack([ds1,ds2,ds3])#????
+        #assert hstack([ds('x','y'),ds('z')])==ds
+        #hstack([ds('z','y'),ds('x')])==ds
     assert have_raised2(hstack,[ds('x'),ds('x')])
     assert have_raised2(hstack,[ds('y','x'),ds('x')])
     assert not have_raised2(hstack,[ds('x'),ds('y')])
-    
-#        i=0
-#        for example in hstack([ds('x'),ds('y'),ds('z')]):
-#            example==ds[i]
-#            i+=1 
-#        del i,example
-#* ds1 & ds2 & ds3 == ds.vstack([ds1,ds2,ds3])#????
+        
+    #        i=0
+    #        for example in hstack([ds('x'),ds('y'),ds('z')]):
+    #            example==ds[i]
+    #            i+=1 
+    #        del i,example
+    #* ds1 & ds2 & ds3 == ds.vstack([ds1,ds2,ds3])#????
 
 def test_fields_fct(ds):
     #@todo, fill correctly