test/data/vclamp_jpk/README: Document sample versions
[hooke.git] / hooke / command_stack.py
index c1164732f687ebad51374afb0be99d97fbaa6836..1307a01f8a011bae83020ab2fe4e66f4c21176fc 100644 (file)
@@ -1,23 +1,32 @@
-# Copyright (C) 2010 W. Trevor King <wking@drexel.edu>
+# Copyright (C) 2010-2012 W. Trevor King <wking@tremily.us>
 #
 # This file is part of Hooke.
 #
-# Hooke is free software: you can redistribute it and/or modify it
-# under the terms of the GNU Lesser General Public License as
-# published by the Free Software Foundation, either version 3 of the
-# License, or (at your option) any later version.
+# Hooke is free software: you can redistribute it and/or modify it under the
+# terms of the GNU Lesser General Public License as published by the Free
+# Software Foundation, either version 3 of the License, or (at your option) any
+# later version.
 #
-# Hooke is distributed in the hope that it will be useful, but WITHOUT
-# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
-# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General
-# Public License for more details.
+# Hooke is distributed in the hope that it will be useful, but WITHOUT ANY
+# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
+# A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
+# details.
 #
-# You should have received a copy of the GNU Lesser General Public
-# License along with Hooke.  If not, see
-# <http://www.gnu.org/licenses/>.
+# You should have received a copy of the GNU Lesser General Public License
+# along with Hooke.  If not, see <http://www.gnu.org/licenses/>.
 
 """The ``command_stack`` module provides tools for managing and
 executing stacks of :class:`~hooke.engine.CommandMessage`\s.
+
+In experiment analysis, the goal is to construct a
+:class:`~hooke.command_stack.CommandStack` that starts with your raw
+experiment data and ends with your analyzed results.  These
+:class:`~hooke.command_stack.CommandStack`\s are stored in your
+:class:`~hooke.playlist.FilePlaylist`, so they are saved to disk with
+the analysis results.  This means you will always have a record of
+exactly how you processed the raw data to produce your analysis
+results, which makes it easy to audit your approach or go back and
+reanalyze older data.
 """
 
 import os
@@ -39,10 +48,10 @@ class CommandStack (list):
     >>> c.append(CommandMessage('CommandB', {'param':'D'}))
 
     Implement a dummy :meth:`execute_command` for testing.
-    
-    >>> def execute_cmd(hooke, command_message):
+
+    >>> def execute_cmd(hooke, command_message, stack=None):
     ...     cm = command_message
-    ...     print 'EXECUTE', cm.command, cm.arguments
+    ...     print('EXECUTE {} {}'.format(cm.command, cm.arguments))
     >>> c.execute_command = execute_cmd
 
     >>> c.execute(hooke=None)  # doctest: +ELLIPSIS
@@ -59,11 +68,10 @@ class CommandStack (list):
     
     >>> def filter(hooke, command_message):
     ...     return command_message.command == 'CommandB'
-    >>> c.filter = filter
 
     Apply the stack to the current curve.
 
-    >>> c.execute(hooke=None)  # doctest: +ELLIPSIS
+    >>> c.execute(hooke=None, filter=filter)  # doctest: +ELLIPSIS
     EXECUTE CommandB {'param': 'B'}
     EXECUTE CommandB {'param': 'D'}
 
@@ -73,30 +81,136 @@ class CommandStack (list):
     >>> c.execute_command(hooke=None, command_message=cm)
     EXECUTE CommandC {'param': 'E'}
     >>> c.append(cm)
-    >>> print [repr(cm) for cm in c]  # doctest: +NORMALIZE_WHITESPACE
+    >>> print([repr(cm) for cm in c])  # doctest: +NORMALIZE_WHITESPACE
     ['<CommandMessage CommandA {param: A}>',
      '<CommandMessage CommandB {param: B}>',
      '<CommandMessage CommandA {param: C}>',
      '<CommandMessage CommandB {param: D}>',
      '<CommandMessage CommandC {param: E}>']
 
+    The data-type is also pickleable, which ensures we can move it
+    between processes with :class:`multiprocessing.Queue`\s and easily
+    save it to disk.  We must remove the unpickleable dummy executor
+    before testing though.
+
+    >>> c.execute_command  # doctest: +ELLIPSIS
+    <function execute_cmd at 0x...>
+    >>> del(c.__dict__['execute_command'])
+    >>> c.execute_command  # doctest: +ELLIPSIS
+    <bound method CommandStack.execute_command of ...>
+    
+    Lets also attach a child command message to demonstrate recursive
+    serialization (we can't append `c` itself because of
+    `Python issue 1062277`_).
+
+    .. _Python issue 1062277: http://bugs.python.org/issue1062277
+
+    >>> import copy
+    >>> c.append(CommandMessage('CommandD', {'param': copy.deepcopy(c)}))
+
+    Run the pickle (and YAML) tests.
+
+    >>> import pickle
+    >>> s = pickle.dumps(c)
+    >>> z = pickle.loads(s)
+    >>> print('\\n'.join([repr(cm) for cm in c]))
+    ... # doctest: +NORMALIZE_WHITESPACE
+    <CommandMessage CommandA {param: A}>
+    <CommandMessage CommandB {param: B}>
+    <CommandMessage CommandA {param: C}>
+    <CommandMessage CommandB {param: D}>
+    <CommandMessage CommandC {param: E}>
+    <CommandMessage CommandD {param:
+      [<CommandMessage CommandA {param: A}>,
+       <CommandMessage CommandB {param: B}>,
+       <CommandMessage CommandA {param: C}>,
+       <CommandMessage CommandB {param: D}>,
+       <CommandMessage CommandC {param: E}>]}>
+    >>> import yaml
+    >>> print(yaml.dump(c))  # doctest: +REPORT_UDIFF
+    !!python/object/new:hooke.command_stack.CommandStack
+    listitems:
+    - !!python/object:hooke.engine.CommandMessage
+      arguments: {param: A}
+      command: CommandA
+      explicit_user_call: true
+    - !!python/object:hooke.engine.CommandMessage
+      arguments: {param: B}
+      command: CommandB
+      explicit_user_call: true
+    - !!python/object:hooke.engine.CommandMessage
+      arguments: {param: C}
+      command: CommandA
+      explicit_user_call: true
+    - !!python/object:hooke.engine.CommandMessage
+      arguments: {param: D}
+      command: CommandB
+      explicit_user_call: true
+    - !!python/object:hooke.engine.CommandMessage
+      arguments: {param: E}
+      command: CommandC
+      explicit_user_call: true
+    - !!python/object:hooke.engine.CommandMessage
+      arguments:
+        param: !!python/object/new:hooke.command_stack.CommandStack
+          listitems:
+          - !!python/object:hooke.engine.CommandMessage
+            arguments: {param: A}
+            command: CommandA
+            explicit_user_call: true
+          - !!python/object:hooke.engine.CommandMessage
+            arguments: {param: B}
+            command: CommandB
+            explicit_user_call: true
+          - !!python/object:hooke.engine.CommandMessage
+            arguments: {param: C}
+            command: CommandA
+            explicit_user_call: true
+          - !!python/object:hooke.engine.CommandMessage
+            arguments: {param: D}
+            command: CommandB
+            explicit_user_call: true
+          - !!python/object:hooke.engine.CommandMessage
+            arguments: {param: E}
+            command: CommandC
+            explicit_user_call: true
+      command: CommandD
+      explicit_user_call: true
+    <BLANKLINE>
+
     There is also a convenience function for clearing the stack.
 
     >>> c.clear()
-    >>> print [repr(cm) for cm in c]
+    >>> print([repr(cm) for cm in c])
     []
+
+    YAMLize a curve argument.
+
+    >>> from .curve import Curve
+    >>> c.append(CommandMessage('curve info', {'curve': Curve(path=None)}))
+    >>> print(yaml.dump(c))  # doctest: +REPORT_UDIFF
+    !!python/object/new:hooke.command_stack.CommandStack
+    listitems:
+    - !!python/object:hooke.engine.CommandMessage
+      arguments:
+        curve: !!python/object:hooke.curve.Curve {}
+      command: curve info
+      explicit_user_call: true
+    <BLANKLINE>
     """
-    def execute(self, hooke):
+    def execute(self, hooke, filter=None, stack=False):
         """Execute a stack of commands.
 
         See Also
         --------
-        _execute, filter
+        execute_command, filter
         """
+        if filter == None:
+            filter = self.filter
         for command_message in self:
-            if self.filter(hooke, command_message) == True:
+            if filter(hooke, command_message) == True:
                 self.execute_command(
-                    hooke=hooke, command_message=command_message)
+                    hooke=hooke, command_message=command_message, stack=stack)
 
     def filter(self, hooke, command_message):
         """Return `True` to execute `command_message`, `False` otherwise.
@@ -105,9 +219,11 @@ class CommandStack (list):
         """
         return True
 
-    def execute_command(self, hooke, command_message):
+    def execute_command(self, hooke, command_message, stack=False):
+        arguments = dict(command_message.arguments)
+        arguments['stack'] = stack
         hooke.run_command(command=command_message.command,
-                          arguments=command_message.arguments)
+                          arguments=arguments)
 
     def clear(self):
         while len(self) > 0:
@@ -121,8 +237,13 @@ class FileCommandStack (CommandStack):
 
     def __init__(self, *args, **kwargs):
         super(FileCommandStack, self).__init__(*args, **kwargs)
-        self.name = None
-        self.path = None
+        self.name = self.path = None
+
+    def __setstate__(self, state):
+        self.name = self.path = None
+        for key,value in state.items():
+            setattr(self, key, value)
+        self.set_path(state.get('path', None))
 
     def set_path(self, path):
         """Set the path (and possibly the name) of the command  stack.
@@ -185,7 +306,7 @@ class FileCommandStack (CommandStack):
         >>> c.append(CommandMessage('CommandB', {'param':'B'}))
         >>> c.append(CommandMessage('CommandA', {'param':'C'}))
         >>> c.append(CommandMessage('CommandB', {'param':'D'}))
-        >>> print c.flatten()
+        >>> print(c.flatten())
         - arguments: {param: A}
           command: CommandA
         - arguments: {param: B}
@@ -218,7 +339,7 @@ class FileCommandStack (CommandStack):
         ... '''
         >>> c = FileCommandStack()
         >>> c.from_string(string)
-        >>> print [repr(cm) for cm in c]  # doctest: +NORMALIZE_WHITESPACE
+        >>> print([repr(cm) for cm in c])  # doctest: +NORMALIZE_WHITESPACE
         ['<CommandMessage CommandA {param: A}>',
          '<CommandMessage CommandB {param: B}>',
          '<CommandMessage CommandA {param: C}>',