Commits

Joe Kington committed ab19880

Changes to the data.py wrapper around the hdf5 file. Added getitems, iters, etc

  • Participants
  • Parent commits 62b5479

Comments (0)

Files changed (1)

 
 import analysis
 
-basedir = os.path.dirname(__file__)
-data = h5py.File(os.path.join(basedir, 'data.hdf5'), 'r')
 
 class Once(object):
     """
         setattr(obj, self._calculate.func_name, value)
         return value
 
+class DogCollection(object):
+    def __init__(self, filename):
+        self.datafile = h5py.File(filename, 'r')
+
+    def __len__(self):
+        return len(self.datafile)
+
+    def __iter__(self):
+        for group in self.datafile.values():
+            yield Dog(group)
+
+    def __getitem__(self, key):
+        return Dog(self.datafile[key])
+
 class Dog(object):
     def __init__(self, group):
         self.group = group
         self.name = group.attrs['name']
 
+    def __getitem__(self, key):
+        return Measurement(self.group[key])
+
+    def __iter__(self):
+        for item in self.group.values():
+            yield Measurement(item)
+
     @property
     def measurements(self):
-        for measurement in self.group.values():
-            yield Measurement(measurement)
+        for item in self:
+            yield item
 
 class Measurement(object):
     def __init__(self, group):
     def paw(self):
         return analysis.classify(self.standardized_pawprint)
 
-dogs = (Dog(item) for item in data.values())
+basedir = os.path.dirname(__file__)
+
+dogs = DogCollection(os.path.join(basedir, 'data.hdf5'))
 measurements = (run for dog in dogs for run in dog.measurements)