From: W. Trevor King Date: Thu, 15 Mar 2012 02:55:45 +0000 (-0400) Subject: Add __reduce__() method to BitwiseOperator and _NamedInt. X-Git-Tag: 0.4~1 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=520f82497804b26df7624f44dfedad9303ebe6d9;p=pycomedi.git Add __reduce__() method to BitwiseOperator and _NamedInt. This makes it possible to successfully pickle and deepcopy instances of these types [1,2]. This gets the TRIG_SRC.now == copy.deepcopy(TRIG_SRC.now) doctest working, which I needed to successfully dump h5config ChoiceConfig objects that use _Enum choices [3]. For example, the newly fixed pypiezo.config.ChannelConfig [4] uses the AREF enum values as possible choices for it's `analog-reference` setting. h5config makes deepcopies of mutable objects to avoid confusion if the mutable is changed externally. It then compares the deepcopies with uncopied values. Now that deepcopying works for _NamedInts, the h5config behavior will work. [1] http://docs.python.org/library/pickle.html#pickling-and-unpickling-extension-types [2] http://docs.python.org/library/copy.html [3] http://pypi.python.org/pypi/h5config/ [4] http://pypi.python.org/pypi/pypiezo/ --- diff --git a/pycomedi/constant.pyx b/pycomedi/constant.pyx index 1f89219..ae981d7 100644 --- a/pycomedi/constant.pyx +++ b/pycomedi/constant.pyx @@ -191,6 +191,9 @@ cdef class BitwiseOperator (object): else: raise ValueError(op) + def __reduce__(self): + return (BitwiseOperator, (self.value,)) + class _NamedInt (BitwiseOperator): "A flag or enum item." @@ -205,6 +208,9 @@ class _NamedInt (BitwiseOperator): def __repr__(self): return '<%s %s>' % (self.__class__.__name__, self.name) + def __reduce__(self): + return (_NamedInt, (self.name, self.value, self.doc)) + class _Enum (list): "An enumerated list" @@ -256,7 +262,6 @@ class _Enum (list): self._value_keys[item.value] = item setattr(self, item.name, item) - def index_by_name(self, name): return self._name_keys[name]