pickle

Loading files into variables

江枫思渺然 提交于 2019-12-11 01:07:01
问题 I am trying to write a small function that gets a variable name, check if it exists, and if not loads it from a file (using pickle) to the global namespace. I tried using this in a file: import cPickle # # Load if neccesary # def loadfile(variable, filename): if variable not in globals(): cmd = "%s = cPickle.load(file('%s','r'))" % (variable, filename) print cmd exec(cmd) in globals() But it doesn't work - the variable don't get defined. What am I doing wrong? 回答1: Using 'globals' has the

How do I get cucumber and pickle working with mongo_mapper, machinist, and machinist_mongo?

爱⌒轻易说出口 提交于 2019-12-10 22:23:31
问题 I would like to get machinist, machinist_mongo, mongo_mapper, cucumber and pickle to play nice together. Currently I have machinist with all my blueprints configured and am using cucumber to do BDD. So far so good. My problem is I am having to write custom cucumber steps for all of my machinist blueprints. It is not really a problem per se, since it is not stopping me in my tracks, but as a .NET dev checking out rails, it feels really dirty to have to write a step for each blueprint whereas

Is it possible to pickle instances of structs in Golang

↘锁芯ラ 提交于 2019-12-10 21:37:02
问题 I am doing some machine learning in Golang. I am now hitting a wall, my trained classifier takes almost half a minute to train and want to save that instance of the classifier so that I do not have to train in from scratch every time. How should someone go about doing this is Golang? FYI my classifier is a struct When I do this type of stuff with python, it's very easy with pickle. Is there an equivalent? 回答1: Try gob or encoding/json to marshal your objects. After that, you can store the

Unable to load a previously dumped pickle file in Python

感情迁移 提交于 2019-12-10 21:17:28
问题 The implemented algorithm which I use is quite heavy and has three parts. Thus, I used pickle to dump everything in between various stages in order to do testing on each stage separately. Although the first dump always works fine, the second one behaves as if it is size dependent. It will work for a smaller dataset but not for a somewhat larger one. (The same actually also happens with a heatmap I try to create but that's a different question) The dumped file is about 10MB so it's nothing

How to pickle numpy's Inf objects?

冷暖自知 提交于 2019-12-10 20:43:45
问题 When trying to pickle the object Inf as defined in numpy (I think), the dumping goes Ok but the loading fails: >>> cPickle.dump(Inf, file("c:/temp/a.pcl",'wb')) >>> cPickle.load(file("c:/temp/a.pcl",'rb')) Traceback (most recent call last): File "<pyshell#257>", line 1, in <module> cPickle.load(file("c:/temp/a.pcl",'rb')) ValueError: could not convert string to float >>> type(Inf) <type 'float'> Why is that? And moreover - is there a way to fix that? I want to pickle something that has Inf in

Pickling objects imported with importlib.util

孤街浪徒 提交于 2019-12-10 19:26:23
问题 I ran into a problem while using Python's pickle. I need to load some Python modules by giving their file paths to importlib.util, like so: import importlib.util spec = importlib.util.spec_from_file_location('custom', 'C:\path\to\.py\file.py') module = importlib.util.module_from_spec(spec) spec.loader.exec_module(module) I would like to instantiate some objects from the loaded module and serialize them for later use, but when I try: pickle.dump(module.ObjectFromModule(), open('C:\object

pickle load error “__init__() takes exactly 2 arguments (1 given)”

我的未来我决定 提交于 2019-12-10 19:18:26
问题 My issue is that a custom class has been saved with pickle.dump, since these files were saved the custom class has been changed and now when I use pickle.load I am getting this error. Is it a problem with the saved file? The error: File "/cprprod/extern/lib/python2.7/pickle.py", line 1378, in load return Unpickler(file).load() File "/cprprod/extern/lib/python2.7/pickle.py", line 858, in load dispatch[key](self) file "/cprprod/extern/lib/python2.7/pickle.py", line 1070, in load_inst self.

Python pickling error when using sessions

人走茶凉 提交于 2019-12-10 19:14:35
问题 In my django app I was creating an extended user profile using session vars. But when registration form was saved and user was about to create, I got following error : Traceback (most recent call last): File "\Python26\Lib\site-packages\django\core\servers\basehttp.py", line 279, in run self.result = application(self.environ, self.start_response) File "\Python26\Lib\site-packages\django\core\servers\basehttp.py", line 651, in __call__ return self.application(environ, start_response) File "

python pickle size limit

怎甘沉沦 提交于 2019-12-10 17:19:54
问题 i want to pickle a large (1810392*255) numpy array. However when pickling i get an error: [...]error: 'i' format requires -2147483648 <= number <= 2147483647 Code: import numpy import pickle l=numpy.zeros((1810392,255)) f=open('file.pkl','wb') pickle.dump(l,f,2) Is there a size limit? Is there a workaround? If not necessary I do not want to use hdf5 or something not build into python. I also tried numpy.savez and numpy.savez_compressed . Code: import numpy l=numpy.zeros((1810392,255)) numpy

Why does pickle.dumps call __getattr__?

送分小仙女□ 提交于 2019-12-10 17:05:52
问题 import cPickle class Foo(object): def __init__(self): self._data = {'bar': 'baz'} def __getattr__(self, name): assert hasattr(self, '_data') return self._data[name] # I even had to define this just to stop KeyError: '__getstate__' def __getstate__(self): return self.__dict__ foo = Foo() bar = cPickle.dumps(foo) cPickle.loads(bar) This raises an assertion error. I thought pickle / cPickle just turns __dict__ into a string when dumping and then uses that string to set the __dict__ of the new