- 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
- else:
- # 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)
+ self._setup_default_attrs()
+ self.__dict__.update(self._default_attrs)
+ if state == True:
+ return
+ self.__dict__.update(state)
+ self.set_path(getattr(self, 'path', None))
+ if self.info in [None, {}]:
+ self.info = {}
+ if type(self.command_stack) == list:
+ self.command_stack = CommandStack()
+
+ def __copy__(self):
+ """Set copy to preserve :attr:`_hooke`.
+
+ :meth:`getstate` drops :attr:`_hooke` for :mod:`pickle` and
+ :mod:`yaml` output, but it should be preserved (but not
+ duplicated) during copies.
+
+ >>> import copy
+ >>> class Hooke (object):
+ ... pass
+ >>> h = Hooke()
+ >>> d = Data(shape=(3,2), info={'columns':['distance (m)', 'force (N)']})
+ >>> for i in range(3): # initialize d
+ ... for j in range(2):
+ ... d[i,j] = i*10 + j
+ >>> c = Curve(None)
+ >>> c.data = [d]
+ >>> c.set_hooke(h)
+ >>> c._hooke # doctest: +ELLIPSIS
+ <hooke.curve.Hooke object at 0x...>
+ >>> c._hooke == h
+ True
+ >>> c2 = copy.copy(c)
+ >>> c2._hooke # doctest: +ELLIPSIS
+ <hooke.curve.Hooke object at 0x...>
+ >>> c2._hooke == h
+ True
+ >>> c2.data
+ [Data([[ 0., 1.],
+ [ 10., 11.],
+ [ 20., 21.]])]
+ >>> d.info
+ {'columns': ['distance (m)', 'force (N)']}
+ >>> id(c2.data[0]) == id(d)
+ True
+ """
+ copier = _copy_dispatch.get(type(self))
+ if copier:
+ return copier(self)
+ reductor = dispatch_table.get(type(self))
+ if reductor:
+ rv = reductor(self)
+ else:
+ # :class:`object` implements __reduce_ex__, see :pep:`307`.
+ rv = self.__reduce_ex__(2)
+ y = _reconstruct(self, rv, 0)
+ y.set_hooke(self._hooke)
+ return y
+
+ def __deepcopy__(self, memo):
+ """Set deepcopy to preserve :attr:`_hooke`.
+
+ :meth:`getstate` drops :attr:`_hooke` for :mod:`pickle` and
+ :mod:`yaml` output, but it should be preserved (but not
+ duplicated) during copies.
+
+ >>> import copy
+ >>> class Hooke (object):
+ ... pass
+ >>> h = Hooke()
+ >>> d = Data(shape=(3,2), info={'columns':['distance (m)', 'force (N)']})
+ >>> for i in range(3): # initialize d
+ ... for j in range(2):
+ ... d[i,j] = i*10 + j
+ >>> c = Curve(None)
+ >>> c.data = [d]
+ >>> c.set_hooke(h)
+ >>> c._hooke # doctest: +ELLIPSIS
+ <hooke.curve.Hooke object at 0x...>
+ >>> c._hooke == h
+ True
+ >>> c2 = copy.deepcopy(c)
+ >>> c2._hooke # doctest: +ELLIPSIS
+ <hooke.curve.Hooke object at 0x...>
+ >>> c2._hooke == h
+ True
+ >>> c2.data
+ [Data([[ 0., 1.],
+ [ 10., 11.],
+ [ 20., 21.]])]
+ >>> d.info
+ {'columns': ['distance (m)', 'force (N)']}
+ >>> id(c2.data[0]) == id(d)
+ False
+ """
+ reductor = dispatch_table.get(type(self))
+ if reductor:
+ rv = reductor(self)
+ else:
+ # :class:`object` implements __reduce_ex__, see :pep:`307`.
+ rv = self.__reduce_ex__(2)
+ y = _reconstruct(self, rv, 1, memo)
+ y.set_hooke(self._hooke)
+ return y