pickle

Python pickling keep object identity

烈酒焚心 提交于 2019-12-24 00:18:39
问题 Is there any way to preserve the identity of a pickled object, i.e. have the below print True : import pickle class Foo: pass x = Foo() print(x is pickle.loads(pickle.dumps(x))) #False I am using cPickle and cpython 3.x on a Linux box, don't need something that's portable. 回答1: yes, it is possible; You'll need to include the "identity" in the pickled result some how; the most natural being to use __getnewargs__ and have a __new__ method return the existing, cached instance in that case.

How do I unpickle a series of objects from a file in Python?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-24 00:07:37
问题 I have pickled objects to a file in append mode but it only reads a single object. Here's the code. I don't know what I am doing wrong. with open('notes.pkl', 'ab') as fileObject: #append pickle.dump(obj, fileObject, pickle.HIGHEST_PROTOCOL) with open('notes.pkl', 'rb') as input: #read obj= pickle.load(input) //perform tasks for each obj unpickled from the file 回答1: You need to call pickle.load repeatedly until you hit end-of-file. 来源: https://stackoverflow.com/questions/36504887/how-do-i

Is it possible to pickle itertools.product in python?

僤鯓⒐⒋嵵緔 提交于 2019-12-23 22:20:30
问题 I would like to save the state of itertools.product() after my program quits. Is it possible to do this with pickling? What I am planning to do is to generate permutations and if the process is interrupted (KeyboardInterrupt), I can resume the process the next time I run the program. def trywith(itr): try: for word in itr: time.sleep(1) print("".join(word)) except KeyboardInterrupt: f=open("/root/pickle.dat","wb") pickle.dump((itr),f) f.close() if os.path.exists("/root/pickle.dat"): f=open("

Pickle will not work with tkinter

冷暖自知 提交于 2019-12-23 22:05:55
问题 I'm making a little game with Tkinter, and it has a save function using pickle. However, when I try to save, it throws up the following message; Exception in Tkinter callback Traceback (most recent call last): File "C:\Python34\lib\tkinter\__init__.py", line 1533, in __call__ return self.func(*args) File "C:\Users\Benedict\Documents\Python\Migrant Simulator\MigSim 2016.10\migrant-stimulator.py", line 260, in save pickle.dump(self.game,file) _pickle.PicklingError: Can't pickle <class 'tkapp'>:

Storing unpicklabe pygame.Surface objects in external files

倖福魔咒の 提交于 2019-12-23 19:05:32
问题 So I've got a problem - I'm writing a game prototype in Python, using Pygame, and I want to save my games. All of the game-related data is in three instances of certain classes, and I want to save these three instances to a file. However, I've tried pickling these instances, and it doesn't work. Instead, I get "TypeError: can't pickle Surface objects". This is a problem, because I want to store Surface objects. I'm open to any alternatives to pickling that there may be, using any other kind

Python: Checking if an object is atomically pickleable

风格不统一 提交于 2019-12-23 17:56:50
问题 What's an accurate way of checking whether an object can be atomically pickled? When I say "atomically pickled", I mean without considering other objects it may refer to. For example, this list: l = [threading.Lock()] is not a a pickleable object, because it refers to a Lock which is not pickleable. But atomically, this list itself is pickleable. So how do you check whether an object is atomically pickleable? (I'm guessing the check should be done on the class, but I'm not sure.) I want it to

How to receive pickle via subprocess.Popen

强颜欢笑 提交于 2019-12-23 17:26:12
问题 getPickle.py import pickle import subprocess cmd = ['rsh', 'host1', 'sendPickle.py'] p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = p.communicate() results = pickle.load(stdout) print results sendPickle.py import pickle import sys to_return = {'a':1, 'b': 2} pickle.dump(to_return, sys.stdout) OUTPUT: File "getPickle" line 10, in <module> results = pickle.load(stdout) AttributeError: 'str' object has no attribute 'readline' What can I do to get

Pickle a QPolygon in python 3.6+ and PyQt5

一曲冷凌霜 提交于 2019-12-23 16:28:06
问题 I try to pickle a QPolygon and load it afterwards, but I get an error. I have done this on Python2 with PyQt4 but want to use it now on Python3 with PyQt5. I do not want to read/load data generated with Python 2! The pickle file is simply used to temporarily store Qt-elements like QPolygons from Python3 to Python3. I have tested different protocol options from 1-4 for pickle.dump() and tried to use the "fix_imports=True" option which should not make a difference in Python3. Here is my

can't unpickle class that inherits from pandas DataFrame

泪湿孤枕 提交于 2019-12-23 16:17:20
问题 I'm trying to pickle objects that inherit from pandas.DataFrame. The attribute I add to the dataframe disappears during the pickling/unpickling process. There are some obvious workarounds, but... am I doing something wrong, or is this a bug? import pandas as pd import pickle class Foo(pd.DataFrame): def __init__(self,tag,df): super().__init__(df) self._tag = tag foo = Foo('mytag', pd.DataFrame({'a':[1,2,3],'b':[4,5,6]})) print(foo) print(foo._tag) print("-------------------------------------"

Can a cookiejar object be pickled?

房东的猫 提交于 2019-12-23 13:24:10
问题 I tried pickling a CookieJar object like this: import cookielib import pickle dumpFile = open('cookie.dump','w') cj = cookielib.CookieJar() pickle.dump(cj, dumpFile) It raised the following exception: raise TypeError, "can't pickle %s objects" % base.__name__ TypeError: can't pickle lock objects Can a CookieJar be pickled? 回答1: The answer to the question as asked is "no": the jar itself is not pickle-able. However, the cookies contained in the jar, are : pickle.dump([c for c in cj], dumpFile)