Remove CommandStack.__getstate__ to avoid type ambiguity for command arguments.
authorW. Trevor King <wking@drexel.edu>
Sat, 21 Aug 2010 16:53:54 +0000 (12:53 -0400)
committerW. Trevor King <wking@drexel.edu>
Sat, 21 Aug 2010 16:53:54 +0000 (12:53 -0400)
hooke/command_stack.py
hooke/curve.py
hooke/playlist.py

index 234b3f3d85dfbd0ed7f42584c1d04f51517cf6be..6e84a913f1ff9731ad2da74927bb7694361b946d 100644 (file)
@@ -39,7 +39,7 @@ class CommandStack (list):
     >>> c.append(CommandMessage('CommandB', {'param':'D'}))
 
     Implement a dummy :meth:`execute_command` for testing.
     >>> c.append(CommandMessage('CommandB', {'param':'D'}))
 
     Implement a dummy :meth:`execute_command` for testing.
-    
+
     >>> def execute_cmd(hooke, command_message, stack=None):
     ...     cm = command_message
     ...     print 'EXECUTE', cm.command, cm.arguments
     >>> def execute_cmd(hooke, command_message, stack=None):
     ...     cm = command_message
     ...     print 'EXECUTE', cm.command, cm.arguments
@@ -59,11 +59,10 @@ class CommandStack (list):
     
     >>> def filter(hooke, command_message):
     ...     return command_message.command == 'CommandB'
     
     >>> def filter(hooke, command_message):
     ...     return command_message.command == 'CommandB'
-    >>> c.filter = filter
 
     Apply the stack to the current curve.
 
 
     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'}
 
     EXECUTE CommandB {'param': 'B'}
     EXECUTE CommandB {'param': 'D'}
 
@@ -80,19 +79,84 @@ class CommandStack (list):
      '<CommandMessage CommandB {param: D}>',
      '<CommandMessage CommandC {param: E}>']
 
      '<CommandMessage CommandB {param: D}>',
      '<CommandMessage CommandC {param: E}>']
 
-    The data-type is also pickleable, to ensure we can move it between
-    processes with :class:`multiprocessing.Queue`\s and easily save it
-    to disk.
+    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)
 
     >>> import pickle
     >>> s = pickle.dumps(c)
     >>> z = pickle.loads(s)
-    >>> 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}>']
+    >>> 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)
+    !!python/object/new:hooke.command_stack.CommandStack
+    listitems:
+    - !!python/object:hooke.engine.CommandMessage
+      arguments: {param: A}
+      command: CommandA
+    - !!python/object:hooke.engine.CommandMessage
+      arguments: {param: B}
+      command: CommandB
+    - !!python/object:hooke.engine.CommandMessage
+      arguments: {param: C}
+      command: CommandA
+    - !!python/object:hooke.engine.CommandMessage
+      arguments: {param: D}
+      command: CommandB
+    - !!python/object:hooke.engine.CommandMessage
+      arguments: {param: E}
+      command: CommandC
+    - !!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
+          - !!python/object:hooke.engine.CommandMessage
+            arguments: {param: B}
+            command: CommandB
+          - !!python/object:hooke.engine.CommandMessage
+            arguments: {param: C}
+            command: CommandA
+          - !!python/object:hooke.engine.CommandMessage
+            arguments: {param: D}
+            command: CommandB
+          - !!python/object:hooke.engine.CommandMessage
+            arguments: {param: E}
+            command: CommandC
+      command: CommandD
+    <BLANKLINE>
 
     There is also a convenience function for clearing the stack.
 
 
     There is also a convenience function for clearing the stack.
 
@@ -100,27 +164,17 @@ class CommandStack (list):
     >>> print [repr(cm) for cm in c]
     []
     """
     >>> print [repr(cm) for cm in c]
     []
     """
-    def __getstate__(self):
-        state = [{'command':cm.command, 'arguments':cm.arguments}
-                for cm in self]
-        return state
-
-    def __setstate__(self, state):
-        self.clear()
-        for cm_state in state:
-            self.append(CommandMessage(
-                    command=cm_state['command'],
-                    arguments=cm_state['arguments']))
-
-    def execute(self, hooke, stack=False):
+    def execute(self, hooke, filter=None, stack=False):
         """Execute a stack of commands.
 
         See Also
         --------
         """Execute a stack of commands.
 
         See Also
         --------
-        _execute, filter
+        execute_command, filter
         """
         """
+        if filter == None:
+            filter = self.filter
         for command_message in self:
         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, stack=stack)
 
                 self.execute_command(
                     hooke=hooke, command_message=command_message, stack=stack)
 
@@ -151,20 +205,10 @@ class FileCommandStack (CommandStack):
         super(FileCommandStack, self).__init__(*args, **kwargs)
         self.name = self.path = None
 
         super(FileCommandStack, self).__init__(*args, **kwargs)
         self.name = self.path = None
 
-    def __getstate__(self):
-        command_stack = super(FileCommandStack, self).__getstate__()
-        state = {
-            'command stack': command_stack,
-            'path': self.path,
-            'name': self.name,
-            }
-        return state
-
     def __setstate__(self, state):
     def __setstate__(self, state):
-        super(FileCommandStack, self).__setstate__(
-            state.get('command stack', []))
-        self.name = state.get('name', None)
-        self.path = None
+        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):
         self.set_path(state.get('path', None))
 
     def set_path(self, path):
index 75fbf75ce2f394bb9d03dcf39e29ca69bf0419ef..64b4c61db56c13d5fd56b73b32a9fe6416db6267 100644 (file)
@@ -194,36 +194,13 @@ class Curve (object):
         state = dict(self.__dict__)      # make a copy of the attribute dict.
         state['info'] = dict(self.info)  # make a copy of the info dict too.
         del(state['_hooke'])
         state = dict(self.__dict__)      # make a copy of the attribute dict.
         state['info'] = dict(self.info)  # make a copy of the info dict too.
         del(state['_hooke'])
-        dc = state['command_stack']
-        if hasattr(dc, '__getstate__'):
-            state['command_stack'] = dc.__getstate__()
-        if state['info'].get('experiment', None) != None:
-            e = state['info']['experiment']
-            assert isinstance(e, experiment.Experiment), type(e)
-            # HACK? require Experiment classes to be defined in the
-            # experiment module.
-            state['info']['experiment'] = e.__class__.__name__
         return state
 
     def __setstate__(self, state):
         self.name = self._hooke = None
         for key,value in state.items():
         return state
 
     def __setstate__(self, state):
         self.name = self._hooke = None
         for key,value in state.items():
-            if key == 'path':
-                self.set_path(value)
-                continue
-            elif key == 'info':
-                if 'experiment' not in value:
-                    value['experiment'] = None
-                elif value['experiment'] != None:
-                    # HACK? require Experiment classes to be defined in the
-                    # experiment module.
-                    cls = getattr(experiment, value['experiment'])
-                    value['experiment'] = cls()
-            elif key == 'command_stack':
-                v = CommandStack()
-                v.__setstate__(value)
-                value = v
             setattr(self, key, value)
             setattr(self, key, value)
+        self.set_path(state.get('path', None))
 
     def set_hooke(self, hooke=None):
         if hooke != None:
 
     def set_hooke(self, hooke=None):
         if hooke != None:
index 473e7362c7b4e722ece4a4788d987e22fd764e44..9ee31259f780e633eb0a25afc0148bd46a244901 100644 (file)
@@ -102,10 +102,10 @@ class NoteIndexList (list):
             if k == 'drivers':  # HACK.  Need better driver serialization.
                 continue
             try:
             if k == 'drivers':  # HACK.  Need better driver serialization.
                 continue
             try:
-                yaml.safe_dump((k,v))
-            except RepresenterError, e:
+                yaml.dump((k,v))
+            except (TypeError, RepresenterError), e:
                 raise NotImplementedError(
                 raise NotImplementedError(
-                    'cannot convert %s.%s = %s (%s) to safe YAML'
+                    'cannot convert %s.%s = %s (%s) to YAML'
                     % (owner.__class__.__name__, k, v, type(v)))
 
     def _setup_item(self, item):
                     % (owner.__class__.__name__, k, v, type(v)))
 
     def _setup_item(self, item):
@@ -383,11 +383,14 @@ class FilePlaylist (Playlist):
         - info: {note: The first curve}
           name: one
           path: curve/one
         - info: {note: The first curve}
           name: one
           path: curve/one
-        - command_stack:
-          - arguments: {arg 0: 0, arg 1: X}
-            command: command A
-          - arguments: {arg 0: 1, arg 1: Y}
-            command: command B
+        - command_stack: !!python/object/new:hooke.command_stack.CommandStack
+            listitems:
+            - !!python/object:hooke.engine.CommandMessage
+              arguments: {arg 0: 0, arg 1: X}
+              command: command A
+            - !!python/object:hooke.engine.CommandMessage
+              arguments: {arg 0: 1, arg 1: Y}
+              command: command B
           info: {attr with spaces: 'The second curve
         <BLANKLINE>
               with endlines'}
           info: {attr with spaces: 'The second curve
         <BLANKLINE>
               with endlines'}
@@ -410,11 +413,14 @@ class FilePlaylist (Playlist):
         - info: {note: The first curve}
           name: one
           path: /path/to/curve/one
         - info: {note: The first curve}
           name: one
           path: /path/to/curve/one
-        - command_stack:
-          - arguments: {arg 0: 0, arg 1: X}
-            command: command A
-          - arguments: {arg 0: 1, arg 1: Y}
-            command: command B
+        - command_stack: !!python/object/new:hooke.command_stack.CommandStack
+            listitems:
+            - !!python/object:hooke.engine.CommandMessage
+              arguments: {arg 0: 0, arg 1: X}
+              command: command A
+            - !!python/object:hooke.engine.CommandMessage
+              arguments: {arg 0: 1, arg 1: Y}
+              command: command B
           info: {attr with spaces: 'The second curve
         <BLANKLINE>
               with endlines'}
           info: {attr with spaces: 'The second curve
         <BLANKLINE>
               with endlines'}
@@ -462,11 +468,14 @@ class FilePlaylist (Playlist):
         ... items:
         ... - info: {note: The first curve}
         ...   path: curve/one
         ... items:
         ... - info: {note: The first curve}
         ...   path: curve/one
-        ... - command_stack:
-        ...   - arguments: {arg 0: 0, arg 1: X}
-        ...     command: command A
-        ...   - arguments: {arg 0: 1, arg 1: Y}
-        ...     command: command B
+        ... - command_stack: !!python/object/new:hooke.command_stack.CommandStack
+        ...      listitems:
+        ...      - !!python/object:hooke.engine.CommandMessage
+        ...        arguments: {arg 0: 0, arg 1: X}
+        ...        command: command A
+        ...      - !!python/object:hooke.engine.CommandMessage
+        ...        arguments: {arg 0: 1, arg 1: Y}
+        ...        command: command B
         ...   info: {attr with spaces: 'The second curve
         ... 
         ...       with endlines'}
         ...   info: {attr with spaces: 'The second curve
         ... 
         ...       with endlines'}