taschini / pyinterval

PyInterval — Interval arithmetic in Python
http://pyinterval.readthedocs.io
Other
83 stars 25 forks source link

pickle intervals #7

Closed croja closed 7 years ago

croja commented 7 years ago

Can I serialize intervals using pickle module? (pyinterval==1.1.1)

Following code:

import pickle from interval import interval, inf, imath

t = interval([500, 2000]) serialized = pickle.dumps(t)

Got error: pickle.PicklingError: Can't pickle <class 'interval.Component'>: it's not found as interval.Component

taschini commented 7 years ago

It should be possible, and if fact I have a test that should ensure that it is on Python 2.6, 2.7 and 3.5.

May I ask you what Python version you're running? If you are not sure, you can run the following few lines to get the Python version and the version of PyInterval that is in fact loaded by the interpreter:

import sys, pkg_resources
print(sys.version)
print(pkg_resources.get_distribution('pyinterval').version)
croja commented 7 years ago

I got following output:

2.7.6 (default, Jun 22 2015, 17:58:13) [GCC 4.8.2] 1.1.1

taschini commented 7 years ago

I found the problem: it is a matter of the version of the pickling protocol. You must use at least version 2 of the pickling protocol:

pickle.loads(pickle.dumps(t, protocol=2))

In fact, you should probably always use the latest protocol, denoted by the special value -1, which, aside from anything else, is much faster:

pickle.loads(pickle.dumps(t, protocol=-1))
croja commented 7 years ago

Yes, It works now! Thank you very much for help!