Commits

Stefan Scherfke  committed 85c0c2f

Made PyTables storage work and fixed the bugs introduced by Ontje. :-P

  • Participants
  • Parent commits 98309b1

Comments (0)

Files changed (4)

File collectors/examples.py

         obj.value_a, obj.value_b = a, b
         c()
 
+def pytables_example():
+    import tables
+
+    from collectors import storage
+
+    class ObserveMe(object):
+        a = 0
+        b = 0
+
+    obj = ObserveMe()
+    h5file = tables.openFile('example.h5', mode='w')
+    collector = Collector(get(obj, 'a', 'b'),
+            backend=storage.PyTables(h5file, 'groupname', ('int', 'int'))
+    )
+
+    for values in zip(range(10), reversed(range(10))):
+        obj.a, obj.b = values
+        collector()
+
+    print collector.a.read(), collector.b.read()
+    h5file.close()
+
 def excel_example():
     print '- Excel example'
 
     w.save('example.xls')
 
 if __name__ == '__main__':
+    simpy_example()
     storage_release_example()
-    simpy_example()
+    pytables_example()
     excel_example()

File collectors/storage/__init__.py

 # coding=utf-8
 
 try:
-    from excel import Excel, ExcelSeries
+    from collectors.storage.excel import Excel, ExcelSeries
 except ImportError:
     pass
 
 try:
-    from pytables import PyTables
-except ImportError:
+    from collectors.storage.pytables import PyTables
+except ImportError as e:
     pass

File collectors/storage/excel.py

         self.book = book
         self.sheet = sheet
 
-    def create_series(self, name, index, dtype=None):
+    def create_series(self, name, index):
         return ExcelSeries(self.sheet, name, index)

File collectors/storage/pytables.py

 from collectors.core import BaseStorage
 
 
-class PyTablesBackend(BaseStorage):
-    def __init__(self, hd5file, group):
+class PyTables(BaseStorage):
+    def __init__(self, hd5file, group, dtypes):
         self.hd5file = hd5file
         self.group = group \
-                if isinstance(group, tables.HDF5Group) \
+                if isinstance(group, tables.Group) \
                 else self.hd5file.createGroup('/', group)
+        self.dtypes = dtypes
 
-        self.append_func = PyTablesBackend.append
+        self.append = lambda series, value: series.append([value])
 
-    def create_series(self, name, index, dtype):
+    def create_series(self, name, index):
         return self.hd5file.createEArray(self.group, name,
-                tables.Atom.from_kind(dtype), (0,))
-
-    @staticmethod
-    def append(series, value):
-        series.append([value])
+                tables.Atom.from_kind(self.dtypes[index]), (0,))