pickle

Python pickling error: TypeError: object pickle not returning list. Issue with numpy?

▼魔方 西西 提交于 2019-12-08 02:15:32
问题 I wrote a script that does a bit of data processing, then pickles the results before doing some more processing so I can fiddle with final parameters for plotting. I am pickling lists of two classes that I've created. Pickling is fine for each list, but unpickling one list returns the following error: Traceback (most recent call last): File "script.py", line 980, in <module> main() File "script.py", line 956, in promoter_main open(os.path.join(pickle_dir, 'doublep.pickle'), 'rb')) File "/usr

How can I pickle a python object into a csv file?

不问归期 提交于 2019-12-07 15:29:30
I am trying to pickle a python object into a csv file. I want to write the pickle of an object as the third column in my file. I want to use pickle to avoid writing serialization for my complex objects. Code to write to csv : with open(self.file_path, 'a') as csv_file: wr = csv.writer(csv_file, delimiter='|') row = ['klines', symbol] row.extend(pickle.dumps(object)) wr.writerow(row) Code to read csv : with open(self.simulation_file_name, 'r') as csv_file: line = csv_file.readline() while line != '': line = line.strip('\n') columns = line.split('|') event_type = line.pop(0) symbol = line.pop(0)

Python: Ensuring my class gets pickled only with the latest protocol

本秂侑毒 提交于 2019-12-07 13:42:57
问题 I'm developing a class which can be meaningfully pickled only using protocol 2 (and upwards on Python 3.) When an older protocol is used, hard-to-trace bugs happen. I want to save the users of the class some debugging pain, so I want the class to immediately raise an exception if it's being pickled with a protocol of less than 2. How can I do that? 回答1: You can implement the __reduce_ex__() method on your class. It receives one parameter, which is the protocol version. Simply raise an

pickle saving pygame Surface (python)

喜欢而已 提交于 2019-12-07 13:40:06
问题 I tried to save a pygame.Surface but it doesn't let me, error TypeError: can't pickle Surface objects I can make it save surfaces? Or maybe there is another module that can save it ? EXPLANATION: a = pygame.Surface( (5,5) ) file = open("hello", "w") pickle.dump(a, file) I have classes which saves in them Surfaces. 回答1: As monkey said: You don't want to pickle a surface. But if you really need to save that surfaces' content than use the pygame.image.save() function. If you prefer your surface

Serializing an object in __main__ with pickle or dill

僤鯓⒐⒋嵵緔 提交于 2019-12-07 11:14:21
问题 I have a pickling problem. I want to serialize a function in my main script, then load it and run it in another script. To demonstrate this, I've made 2 scripts: Attempt 1: The naive way: dill_pickle_script_1.py import pickle import time def my_func(a, b): time.sleep(0.1) # The purpose of this will become evident at the end return a+b if __name__ == '__main__': with open('testfile.pkl', 'wb') as f: pickle.dump(my_func, f) dill_pickle_script_2.py import pickle if __name__ == '__main__': with

When pickling a class I get different behavior in python that in cython

房东的猫 提交于 2019-12-07 11:13:15
问题 I have the following file hierarchy: python/apps/A.py /geometrylib/__init__.py /geometrylib/B.py /geometrylib/geometry.py /geometrylib/goemetry.pyx /geometrylib/goemetry.pyd geometry.pyx and geometry.py contain the same class Camera (the cython version defines the class with cdef ). Both A.py and B.py import the geometry module. If I import the cython version(compiled to geometry.pyd), I can correctly pickle Camera from within B.py in the python/geometrylib folder. But I can't pickle Camera

pickle.dump meet RuntimeError: maximum recursion depth exceeded in cmp

左心房为你撑大大i 提交于 2019-12-07 10:08:58
问题 I have noticed that it may be caused by beautifulsoup or recursive data structure. however, the data structure that cause error seems no problem: class Movie: def __init__(self, name="", dscore=0, mscore=0, durl="", murl=""): self.name = name self.dscore = float(dscore) self.mscore = float(mscore) self.durl = durl self.murl = murl def __str__(self): return unicode(self.name) + u' / ' + unicode(self.dscore) + u' / ' + unicode(self.mscore) \ + u' / ' + unicode(self.durl) + u' / ' + unicode(self

Custom sklearn pipeline transformer giving “pickle.PicklingError”

时光毁灭记忆、已成空白 提交于 2019-12-07 09:30:48
问题 I am trying to create a custom transformer for a Python sklearn pipeline based on guidance from this tutorial: http://danielhnyk.cz/creating-your-own-estimator-scikit-learn/ Right now my custom class/transformer looks like this: class SelectBestPercFeats(BaseEstimator, TransformerMixin): def __init__(self, model=RandomForestRegressor(), percent=0.8, random_state=52): self.model = model self.percent = percent self.random_state = random_state def fit(self, X, y, **fit_params): """ Find features

Pickle cross platform __dict__ attribute error

自古美人都是妖i 提交于 2019-12-07 08:00:49
问题 I'm having an issue with pickle. Things work fine between OSX and Linux, but not Windows and Linux. All pickled strings are stored in memory and sent via an SSL socket. To be 100% clear I have replaced all '\n's with ":::" and all '\r's with "===" (there were none). Scenario: Client-Win: Small Business Server 2011 running Python 2.7 Client-Lin: Fedora Linux running Python 2.7 Server: Fedora Linux running Python 2.7 Client-Lin sends a pickled object to Server: ccopy_reg:::_reconstructor:::p0::

Pickling a trained NLTK Model

跟風遠走 提交于 2019-12-07 07:56:30
问题 So I am currently training a Hidden Markov Model on a set of surgical data like so: nltkTrainer = nltk.tag.hmm.HiddenMarkovModelTrainer(range(15),range(90)) model = nltkTrainer.train_unsupervised(data, max_iterations=3) If it's helpful, 'model' is given as 'HiddenMarkovModelTagger 15 states and 90 output symbols' However, it takes nearly an hour to run this full training on my machine. I want to be able to serialize the nltk model output 'model' to load and save between sessions. I've read