Broke config section of test/tutorial.py out into test/config.py
[hooke.git] / hooke / curve.py
index 34fb34e0135c53f58704fa04f4f889c4c833ae22..a87a7fd52b776d72d397cf2ed13f48461673d173 100644 (file)
@@ -20,7 +20,9 @@
 storing force curves.
 """
 
+import logging
 import os.path
+
 import numpy
 
 from .command_stack import CommandStack
@@ -39,6 +41,7 @@ class NotRecognized (ValueError):
             super(NotRecognized, self).__init__(msg)
             self.curve = data
 
+
 class Data (numpy.ndarray):
     """Stores a single, continuous data set.
 
@@ -172,6 +175,25 @@ class Curve (object):
         self.command_stack = CommandStack()
         self._hooke = None  # Hooke instance for Curve.load()
 
+    def __str__(self):
+        return str(self.__unicode__())
+
+    def __unicode__(self):
+        return u'<%s %s>' % (self.__class__.__name__, self.name)
+
+    def __repr__(self):
+        return self.__str__()
+
+    def __getstate__(self):
+        data = dict(self.__dict__)
+        del(data['_hooke'])
+        return data
+
+    def __setstate__(self, data):
+        self._hooke = None
+        for key,value in data.items():
+            setattr(self, key, value)
+
     def set_hooke(self, hooke=None):
         if hooke != None:
             self._hooke = hooke
@@ -201,15 +223,22 @@ class Curve (object):
         :meth:`hooke.command_stack.CommandStack.execute`.
         """
         self.set_hooke(hooke)
+        log = logging.getLogger('hooke')
+        log.debug('loading curve %s with driver %s' % (self.name, self.driver))
         data,info = self.driver.read(self.path, self.info)
         self.data = data
         for key,value in info.items():
             self.info[key] = value
-        if self.hooke != None:
-            # TODO: set 'curve' argument explicitly for CurveCommands
-            self.command_stack.execute(self.hooke)
+        if self._hooke != None:
+            self.command_stack.execute(self._hooke)
+        elif len(self.command_stack) > 0:
+            log.warn(
+                'could not execute command stack for %s without Hooke instance'
+                % self.name)
 
     def unload(self):
         """Release memory intensive :attr:`.data`.
         """
+        log = logging.getLogger('hooke')
+        log.debug('unloading curve %s' % self.name)
         self.data = None