pickle

Python pickle crash when trying to return default value in __getattr__

ぐ巨炮叔叔 提交于 2019-12-05 06:58:18
I have a dictionary like class that I use to store some values as attributes. I recently added some logic( __getattr__ ) to return None if an attribute doesn't exist. As soon as I did this pickle crashed, and I wanted some insight into why? Test Code: import cPickle class DictionaryLike(object): def __init__(self, **kwargs): self.__dict__.update(kwargs) def __iter__(self): return iter(self.__dict__) def __getitem__(self, key): if(self.__dict__.has_key(key)): return self.__dict__[key] else: return None ''' This is the culprit...''' def __getattr__(self, key): print 'Retreiving Value ' , key

How do I know which versions of pickle a particular version of Python supports?

☆樱花仙子☆ 提交于 2019-12-05 06:34:46
I have a script that requires Python 2.6. I will be adding a large-ish pickled database to the next version, and I want to use the fastest version of pickling. How can I tell which versions of pickling are available in every version of Python 2.6 and later? Like so: >>> import pickle >>> pickle.compatible_formats ['1.0', '1.1', '1.2', '1.3', '2.0'] Edit I think it's safe to rely on the latest documentation. For example the pickle documentation for Python 3.2.1 states: There are currently 4 different protocols which can be used for pickling. Protocol version 0 is the original human-readable

Python常用模块之pickle——对象序列化

时间秒杀一切 提交于 2019-12-05 06:33:29
作用 对Python对象进行序列化,便于存储和传输 Python对象序列化成bytes类型 pickle.dumps(obj) 将Python对象转化为bytes类型 pickle.loads(str) 将转化成的bytes类型数据还原成对象 Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit (AMD64)] on win32 Type "copyright", "credits" or "license()" for more information. >>> import pickle >>> mydict = {'id':123, 'name':'abc'} >>> pickle_str = pickle.dumps(mydict) >>> pickle_str b'\x80\x03}q\x00(X\x02\x00\x00\x00idq\x01K{X\x04\x00\x00\x00nameq\x02X\x03\x00\x00\x00abcq\x03u.' >>> newdict = pickle.loads(pickle_str) >>> newdict {'id': 123, 'name': 'abc'} Python对象序列化写入文件 pickle.dump(obj,

load a pickle file from a zipfile

纵然是瞬间 提交于 2019-12-05 06:18:41
For some reason I cannot get cPickle.load to work on the file-type object returned by ZipFile.open(). If I call read() on the file-type object returned by ZipFile.open() I can use cPickle.loads though. Example .... import zipfile import cPickle # the data we want to store some_data = {1: 'one', 2: 'two', 3: 'three'} # # create a zipped pickle file # zf = zipfile.ZipFile('zipped_pickle.zip', 'w', zipfile.ZIP_DEFLATED) zf.writestr('data.pkl', cPickle.dumps(some_data)) zf.close() # # cPickle.loads works # zf = zipfile.ZipFile('zipped_pickle.zip', 'r') sd1 = cPickle.loads(zf.open('data.pkl').read(

Python Distributed Computing (works)

时光怂恿深爱的人放手 提交于 2019-12-05 04:31:56
I'm using an old thread to post new code which attempts to solve the same problem. What constitutes a secure pickle? this? sock.py from socket import socket from socket import AF_INET from socket import SOCK_STREAM from socket import gethostbyname from socket import gethostname class SocketServer: def __init__(self, port): self.sock = socket(AF_INET, SOCK_STREAM) self.port = port def listen(self, data): self.sock.bind(("127.0.0.1", self.port)) self.sock.listen(len(data)) while data: s = self.sock.accept()[0] siz, dat = data.pop() s.send(siz) s.send(dat) s.close() class Socket: def __init__

Pandas Dataframe AttributeError: 'DataFrame' object has no attribute 'design_info'

╄→尐↘猪︶ㄣ 提交于 2019-12-05 02:36:48
I am trying to use the predict() function of the statsmodels.formula.api OLS implementation. When I pass a new data frame to the function to get predicted values for an out-of-sample dataset result.predict(newdf) returns the following error: 'DataFrame' object has no attribute 'design_info' . What does this mean and how do I fix it? The full traceback is: p = result.predict(newdf) File "C:\Python27\lib\site-packages\statsmodels\base\model.py", line 878, in predict exog = dmatrix(self.model.data.orig_exog.design_info.builder, File "C:\Python27\lib\site-packages\pandas\core\frame.py", line 2088,

TypeError: can't pickle generator objects

北城余情 提交于 2019-12-05 00:36:17
I am trying to write some result on to pickle file as below: raw_X = (self.token_ques(text) for text in training_data) with open('/root/Desktop/classifier_result.pkl', 'wb') as handle: pickle.dump(raw_X, handle) Error: raise TypeError, "can't pickle %s objects" % base.__name__ TypeError: can't pickle generator objects Any help would be much appreciable. Don't use a generator expression when you want to pickle data. Use a list comprehension instead, or call list() on the generator to capture all generated elements for pickling. For example, the following works just fine: raw_X = [self.token

How to pickle a scapy packet?

爱⌒轻易说出口 提交于 2019-12-05 00:17:52
I need to pickle a scapy packet. Most of the time this works, but sometimes the pickler complains about a function object. As a rule of thumb: ARP packets pickle fine. Some UDP packets are problematic. My solution (as inspired by the scapy mailing list) is as follows: class PicklablePacket: """A container for scapy packets that can be pickled (in contrast to scapy packets themselves).""" def __init__(self, pkt): self.contents = bytes(pkt) self.time = pkt.time def __call__(self): """Get the original scapy packet.""" pkt = scapy.Ether(self.contents) pkt.time = self.time return pkt Anywhere I

What's the fastest way to pickle a pandas DataFrame?

做~自己de王妃 提交于 2019-12-05 00:17:42
Which is better, using Pandas built-in method or pickle.dump ? The standard pickle method looks like this: pickle.dump(my_dataframe, open('test_pickle.p', 'wb')) The Pandas built-in method looks like this: my_dataframe.to_pickle('test_pickle.p') tegan Thanks to @qwwqwwq I discovered that pandas has a built-in to_pickle method for dataframes. I did a quick time test: In [1]: %timeit pickle.dump(df, open('test_pickle.p', 'wb')) 10 loops, best of 3: 91.8 ms per loop In [2]: %timeit df.to_pickle('testpickle.p') 10 loops, best of 3: 88 ms per loop So it seems that the built-in is only narrowly

python script to pickle entire environment

时光总嘲笑我的痴心妄想 提交于 2019-12-05 00:09:24
问题 I'm working inside the Python REPL, and I want to save my work periodically. Does anybody have a script to dump all the variables I have defined? I'm looking for something like this: for o in dir(): f=open(o) pickle(o_as_object, f) This seems like something other people have done, so I wanted to ask before re-inventing the wheel. 回答1: I think you want Dill: In addition to pickling python objects, dill provides the ability to save the state of an interpreter session in a single command. Hence,