Python 2.2.1 (#1, Sep 9 2002, 09:26:21)
[GCC 3.2 (Mandrake Linux 9.0 3.2-1mdk)] on linux-i386
Type "help", "copyright", "credits" or "license" for
more information.
>>> from fixedpoint import FixedPoint
>>> x = FixedPoint("1.234")
>>> print x
1.23
>>> import pickle
>>> f = open("foo", 'w')
>>> pickle.dump(f, x)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "/usr/lib/python2.2/pickle.py", line 973, in dump
Pickler(file, bin).dump(object)
File "/usr/lib/python2.2/pickle.py", line 110, in
__init__
self.write = file.write
AttributeError: 'FixedPoint' object has no attribute
'write'
>>> import cPickle
>>> cPickle.dump(f, x)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: argument must have 'write' attribute
>>>
Logged In: YES
user_id=87541
Augh! Never mind that above example - I got the args wrong
for dump(). Here is the correct test and error message:
Python 2.2.1 (#1, Sep 9 2002, 09:26:21)
[GCC 3.2 (Mandrake Linux 9.0 3.2-1mdk)] on linux-i386
Type "help", "copyright", "credits" or "license" for more
information.
>>> from fixedpoint import FixedPoint
>>> import cPickle
>>> f = open('foo', 'w')
>>> x = FixedPoint(1.234)
>>> print x
1.23
>>> cPickle.dump(x, f)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "/usr/lib/python2.2/copy_reg.py", line 68, in _reduce
dict = getstate()
TypeError: a class that defines __slots__ without defining
__getstate__ cannot be pickled
>>>
Logged In: YES
user_id=918868
To make pickle work, just add the following two methods
to the FixedPoint class:
def __getstate__(self):
return (self.n, self.p)
def __setstate__(self, v):
self.n, self.p = v