[Numpy-discussion] xml-rpc with numpy arrays

Sebastien Binet hep.sebastien.binet@gmail....
Tue Sep 30 15:18:36 CDT 2008


Hi,

[..snip..]
On Tuesday 30 September 2008 12:05:27 Robert Kern wrote:
> > doing something wrong.  Is there a way to fix this?  Is there another
> > approach that I should be using?
>
> The marshal module *only* handles builtin Python types. It explicitly
> does not handle anything from third parties like numpy. 

but you can use the copy_reg module ('copyreg' in py2.6) to register 
pickling/unpickling functions to make 'pickle' able to handle user-types:

## test_pickling_numpy.py ##
import numpy
import copy_reg
import cPickle as pickle

def from_string(s):
    return numpy.fromstring(s)

def to_string(o):
    return from_string, (o.tostring(),)

a = numpy.arange (10,dtype=float)
copy_reg.pickle (numpy.ndarray, to_string, from_string)
s = pickle.dumps (a)
b = pickle.loads (s)
assert (a==b, "pickling failed !")
## EOF ##

see for more informations:
http://effbot.org/librarybook/copy-reg.htm

cheers,
sebastien.
-- 
###################################
# Dr. Sebastien Binet
# Lawrence Berkeley National Lab.
# 1 Cyclotron Road
# Berkeley, CA 94720
###################################



More information about the Numpy-discussion mailing list