pickle

How does Python 3 know how to pickle extension types, especially Numpy arrays?

梦想与她 提交于 2019-12-04 13:02:41
Numpy arrays, being extension types (aka defined using in extensions the C API), declare additional fields outside the scope of the Python interpreter (for example the data attribute, which is a Buffer Structure , as documented in Numpy's array interface . To be able to serialize it, Python 2 used to use the __reduce__ function as part of the pickle protocol, as stated in the doc , and explained here . But, even if __reduce__ still exists in Python 3, the Pickle protocol section (and Pickling and unpickling extension types a fortiori) was removed from the doc, so it is unclear what does what.

How to get unpickling to work with iPython?

此生再无相见时 提交于 2019-12-04 12:25:20
问题 I'm trying to load pickled objects in iPython. The error I'm getting is: AttributeError: 'FakeModule' object has no attribute 'World' Anybody know how to get it to work, or at least a workaround for loading objects in iPython in order to interactively browse them? Thanks edited to add: I have a script called world.py that basically does: import pickle class World: "" if __name__ == '__main__': w = World() pickle.dump(w, open("file", "wb")) Than in a REPL I do: import pickle from world import

Pickling decorated callable class wrapper

有些话、适合烂在心里 提交于 2019-12-04 11:37:13
I'm struggling to pickle a wrapped function when I use a custom callable class as a wrapper. I have a callable class "Dependee" that keeps track of dependencies for a wrapped function with a member variable "depends_on". I'd like to use a decorator to wrap functions and also be able to pickle the resulting wrapped function. So I define my dependee class. Note the use of functools.update_wrapper. >>> class Dependee: ... ... def __init__(self, func, depends_on=None): ... self.func = func ... self.depends_on = depends_on or [] ... functools.update_wrapper(self, func) ... ... def __call__(self,

How does the automatic full qualification of class names work, in Python? [relevant to object pickling]

ぃ、小莉子 提交于 2019-12-04 11:31:34
(It is possible to directly jump to the question, further down, and to skip the introduction.) There is a common difficulty with pickling Python objects from user-defined classes: # This is program dumper.py import pickle class C(object): pass with open('obj.pickle', 'wb') as f: pickle.dump(C(), f) In fact, trying to get the object back from another program loader.py with # This is program loader.py with open('obj.pickle', 'rb') as f: obj = pickle.load(f) results in AttributeError: 'module' object has no attribute 'C' In fact, the class is pickled by name ("C"), and the loader.py program does

python pickle UnicodeDecodeError

徘徊边缘 提交于 2019-12-04 10:26:38
问题 I'm trying to load the mnist character dataset (following the tutorial outlined here: http://neuralnetworksanddeeplearning.com/chap1.html ) when I run the load_data_wrapper function I get the error. UnicodeDecodeError: 'ascii' codec can't decode byte 0x90 in position 614: ordinal not in range(128) The code run is: import numpy as np import gzip def load_data(): f = gzip.open('../data/mnist.pkl.gz', 'rb') training_data, validation_data, test_data = pickle.load(f) f.close() return (training

AttributeError when reading a pickle file

那年仲夏 提交于 2019-12-04 10:04:50
I get the following error when I'm reading my .pkl files on spyder (python 3.6.5): IN: with open(file, "rb") as f: data = pickle.load(f) Traceback (most recent call last): File "<ipython-input-5-d9796b902b88>", line 2, in <module> data = pickle.load(f) AttributeError: Can't get attribute 'Signal' on <module '__main__' from 'C:\\Python36\\lib\\site-packages\\spyder\\utils\\ipython\\start_kernel.py'> The context: My program is made of one file: program.py In the program, a class Signal is defined as well as many functions. A simplified overview of the program is provided below: import numpy as

Cython Pickling in Package “not found as” Error

匆匆过客 提交于 2019-12-04 09:45:01
I'm having trouble pickling a Cython class, but only when it's defined inside a package. This problem was noted previously online , but they didn't state how it was resolved. There are two components here: the Cython pickling using a __reduce__ method and a package error. Cython Pickling Success I'll first show how it works without the package part. This example works correctly. Cython File My Cython file is reudce.pyx : cdef class Foo(object): cdef int n def __init__(self, n): self.n = n def __reduce__(self): return Foo, (self.n,) Setup File This may be compiled with a setup.py : from

Handling the classmethod pickling issue with copy_reg

六眼飞鱼酱① 提交于 2019-12-04 08:37:05
问题 I met a pickling error when dealing with multiprocessing: from multiprocessing import Pool def test_func(x): return x**2 class Test: @classmethod def func(cls, x): return x**2 def mp_run(n, func, args): return Pool(n).map(func, args) if __name__ == '__main__': args = range(1,6) print mp_run(5, test_func, args) # [1, 4, 9, 16, 25] print mp_run(5, Test.func, args) """ Exception in thread Thread-3: Traceback (most recent call last): File "/usr/lib64/python2.6/threading.py", line 532, in _

Pickling an enum exposed by Boost.Python

主宰稳场 提交于 2019-12-04 08:01:57
Is it possible to pickle (using cPickle) an enum that has been exposed with Boost.Python? I have successfully pickled other objects using the first method described here , but none of that seems to apply for an enum type, and the objects don't seem to be pickleable by default. Not as they are in the module. I am given to understand that this is SUPPOSED to be possible, but the way the enum_ statement works prevents this. You can work around this on the python side. Somewhere (probably in a __init__.py file) do something like this: import yourmodule def isEnumType(o): return isinstance(o, type)

Pickling objects

一个人想着一个人 提交于 2019-12-04 06:39:41
问题 I need to pickle object [wxpython frame object] and send it as a prameter to this function apply_async on multiproccessing pool module could someone provide me an example how can I do it I tried the following and get an error message : myfile = file(r"C:\binary.dat", "w") pickle.dump(self, myfile) myfile.close() self.my_pool.apply_async(fun,[i,myfile]) def fun(i,self_object): window = pickle.load(self_oject) wx.CallAfter(window.LogData, msg) could someone tell me what could be the problem If