Commits

luensdorf committed 98309b1

- Renamed some methods (which might get Stefan upset ;).
- Added documentation for storage.
- Added storage release example.

Comments (0)

Files changed (3)

collectors/core.py

 """
 
 class BaseStorage(object):
-    def __init__(self):
-        self.append_func = None
+    """
+    Base class for storages into which the data should be collected. During its
+    initialization a :class:`Collector` will call :meth:`create_series` for
+    each monitored variable. Every time the :class:`Collector` is asked to
+    collect the current values :meth:`store` invoked with the appropiate series
+    and value.
+    """
 
-    def create_series(self, name, index, dtype=None):
+    def append(self, series, value):
+        """
+        Append ``value`` into ``series``. This method can be used to perform
+        storage specific conversion (like for example in
+        :class:`collectors.core.backends.pytables.PyTables`).
+
+        If this method directly maps to a method of the series, you can directly
+        forward the call to that method by overriding the attribute. This is
+        done in :class:`DefaultStorage` for example.
+        """
         raise NotImplementedError(self)
 
+    def create_series(self, name, index):
+        """
+        Creates a new series for the variable ``name`` at ``index`` of the
+        collector.
+        """
+        raise NotImplementedError(self)
+
 
 class DefaultStorage(BaseStorage):
+    """
+    The default storage used by :class:`Collector`. The values of each
+    monitored variable is stored in a simple list.
+    """
+
     def __init__(self):
-        self.append_func = list.append
+        # Because the collector will always invoke store(series, value) we can
+        # use the list.append function directly.
+        self.append = list.append
 
-    def create_series(self, name, index, dtype=None):
+    def create_series(self, name, index):
+        """
+        Creates a new empty list for the variable ``name``.
+        """
         return []
 
 
         instance = tuple.__new__(cls, series)
 
         # Set the names and collectors of each series.
-        instance.__append = backend.append_func
+        instance.__append = backend.append
         instance.__names = tuple(names)
         instance.__collectors = tuple(collectors)
 

collectors/examples.py

     plt.plot(np_mon[0], np_mon[2], 'r')
     plt.show()
 
+def storage_release_example():
+    print 'Storage release example (this is really more of a test)'
+
+    from collectors.core import DefaultStorage
+
+    class YellingStorage(DefaultStorage):
+        def __del__(self):
+            print 'Oh noez, I wuz deleted'
+
+    class ObserveMe(object):
+        pass
+
+    obj = ObserveMe()
+    c = Collector(get(obj, 'value_a', 'value_b'), backend=YellingStorage())
+
+    import gc
+    gc.collect()
+
+    print 'Collector initialized'
+
+    for a, b in zip(range(10), reversed(range(10))):
+        obj.value_a, obj.value_b = a, b
+        c()
 
 def excel_example():
     print '- Excel example'
     w.save('example.xls')
 
 if __name__ == '__main__':
+    storage_release_example()
     simpy_example()
     excel_example()

collectors/storage/excel.py

 
 class Excel(BaseStorage):
     def __init__(self, book, sheet):
-        self.append_func = lambda series, value: series.append(value)
+        self.append = lambda series, value: series.append(value)
 
         self.book = book
         self.sheet = sheet