pickle

how to use 'pickle'

随声附和 提交于 2019-12-18 09:05:36
问题 my code(i was unable to use 'pickle'): class A(object): def __getstate__(self): print 'www' return 'sss' def __setstate__(self,d): print 'aaaa' import pickle a = A() s = pickle.dumps(a) e = pickle.loads(s) print s,e print : www aaaa ccopy_reg _reconstructor p0 (c__main__ A p1 c__builtin__ object p2 Ntp3 Rp4 S'sss' p5 b. <__main__.A object at 0x00B08CF0> who can tell me how to use. 回答1: What are you trying to do? It works for me: class A(object): def __init__(self): self.val = 100 def __str__

Python “IOError: [Errno 22] Invalid argument” when using cPickle to write large array to network drive

本秂侑毒 提交于 2019-12-18 08:16:12
问题 EDIT: At the suggestion of J. F. Sebastian, I can get the same error much more simply: Python 2.6.4 (r264:75708, Oct 26 2009, 08:23:19) [MSC v.1500 32 bit (Intel)] Type "copyright", "credits" or "license" for more information. IPython 0.10 -- An enhanced Interactive Python. ? -> Introduction and overview of IPython's features. %quickref -> Quick reference. help -> Python's own help system. object? -> Details about 'object'. ?object also works, ?? prints more. Welcome to pylab, a matplotlib

Python: pickling nested functions

妖精的绣舞 提交于 2019-12-18 07:44:34
问题 Using the example def foo(a): def bar(b): return a+b return bar d = {1:foo(1), 2:foo(2)} It appears that pickle module will not work with a function not defined at the module scope, so pickling 'd' will not work. Is there another pickling mechanism available that I should consider? 回答1: I'm afraid that you can't pickle nested functions. The pickle module serializes functions by name. That is, if you have a function myfunc in a module mymodule it simply saves the name mymodule.myfunc and looks

How to pickle a ssl.SSLContext object

浪尽此生 提交于 2019-12-18 07:16:32
问题 Python 3.5 on windows, try these: import ssl, pickle, multiprocessing context = ssl.create_default_context() foo = pickle.dumps(context) pickle.loads(foo) Throws an exception: TypeError: __new__() missing 1 required positional argument: 'protocol' subclass of multiprocessing.Process throws the same exception: class Foo(multiprocessing.Process): def __init__(self): super().__init__() self.context = ssl.create_default_context() def run(self): pass if __name__ == '__main__': foo = Foo() foo

fastest method to dump numpy array into string

♀尐吖头ヾ 提交于 2019-12-18 07:06:40
问题 I need to organized a data file with chunks of named data. Data is NUMPY arrays. But I don't want to use numpy.save or numpy.savez function, because in some cases, data have to be sent on a server over a pipe or other interface. So I want to dump numpy array into memory, zip it, and then, send it into a server. I've tried simple pickle, like this: try: import cPickle as pkl except: import pickle as pkl import ziplib import numpy as np def send_to_db(data, compress=5): send( zlib.compress(pkl

How to serialize sympy lambdified function?

南笙酒味 提交于 2019-12-18 04:50:11
问题 The title says it all. Is there any way to serialize a function generated by sympy.lambdify?: import sympy as sym import pickle import dill a, b = sym.symbols("a, b") expr = sym.sin(a) + sym.cos(b) lambdified_expr = sym.lambdify((a, b), expr, modules="numpy") pickle.dumps(lambdified_expr) # won't work dill.dumps(lambdified_expr) # won't work either ... The reason I want to do this is because my code generates so many lambdified functions but I found it takes too long every time. 回答1: You

JSON vs. Pickle security

梦想的初衷 提交于 2019-12-18 04:04:10
问题 I recently came across the security problems of the Python pickle and cPickle modules. Obviously, there are no real security measures implemented in pickle unless you overwrite the find_class method as a basic modification to get a bit more security. But I often heard that JSON is more secure. Can anyone elaborate a bit on this?`Why is JSON more secure than pickle? Thanks a lot! Mark 回答1: json is more secure because it's fundamentally more limited. The only python types that a json document

What is the difference between save a pandas dataframe to pickle and to csv?

心不动则不痛 提交于 2019-12-18 04:03:31
问题 I am learning python pandas. I see a tutorial which shows two ways to save a pandas dataframe. pd.to_csv('sub.csv') and to open pd.read_csv('sub.csv') pd.to_pickle('sub.pkl') and to open pd.read_pickle('sub.pkl') The tutorial says to_pickle is to save the dataframe to disk. I am confused about this. Because when I use to_csv , I did see a csv file appears in the folder, which I assume is also save to disk right? In general, why we want to save a dataframe using to_pickle rather than save it

Is there a way to really pickle compiled regular expressions in python?

柔情痞子 提交于 2019-12-17 22:43:12
问题 I have a python console application that contains 300+ regular expressions. The set of regular expressions is fixed for each release. When users run the app, the entire set of regular expressions will be applied anywhere from once (a very short job) to thousands of times (a long job). I would like to speed up the shorter jobs by compiling the regular expressions up front, pickle the compiled regular expressions to a file, and then load that file when the application is run. The python re

Using multiprocessing in a class

╄→гoц情女王★ 提交于 2019-12-17 22:28:21
问题 I was using multiprocessing perfectly in the messy configuration of my code. I decided to give some order to my code and re-write it as a class then I can easily change the inputs, my new code is as following: class LikelihoodTest: def __init__(self,Xgal,Ygal): self.x=Xgal self.y=Ygal self.objPosition=gal_pos self.beta_s=beta self.RhoCrit_SigmaC=rho_c_over_sigma_c self.AngularDiameter=DA self.RhoCrit=rho_crit self.Reducedshear=observed_g self.ShearError=g_err #The 2D function def like2d(self