pickle

Why is Python giving me “an integer is required” when it shouldn't be?

ぐ巨炮叔叔 提交于 2019-12-29 07:42:26
问题 I have a save function within my Python program which looks like this: def Save(n): print("S3") global BF global WF global PBList global PWList print(n) File = open("C:\KingsCapture\Saves\\" + n + "\BF.txt", "w") pickle.dump(BF, File) File = open("C:\KingsCapture\Saves\\" + n + "\WF.txt", "w") pickle.dump(WF, File) File = open("C:\KingsCapture\Saves\\" + n + "\PBList.txt", "w") pickle.dump(PBList, File) File = open("C:\KingsCapture\Saves\\" + n + "\PWList.txt", "w") pickle.dump(PWList, File)

Why is Python giving me “an integer is required” when it shouldn't be?

穿精又带淫゛_ 提交于 2019-12-29 07:42:09
问题 I have a save function within my Python program which looks like this: def Save(n): print("S3") global BF global WF global PBList global PWList print(n) File = open("C:\KingsCapture\Saves\\" + n + "\BF.txt", "w") pickle.dump(BF, File) File = open("C:\KingsCapture\Saves\\" + n + "\WF.txt", "w") pickle.dump(WF, File) File = open("C:\KingsCapture\Saves\\" + n + "\PBList.txt", "w") pickle.dump(PBList, File) File = open("C:\KingsCapture\Saves\\" + n + "\PWList.txt", "w") pickle.dump(PWList, File)

debugging pickle

流过昼夜 提交于 2019-12-29 01:39:09
问题 I'm trying to pickle quite an involved object hierarchy and getting the exception: pickle.PicklingError: Can't pickle <class 'function'>: attribute lookup builtins.function failed Are there any reasonable methods one can use to test the pickleablility of an object hierarchy? My aim would be to find the location of the offending function 回答1: To do this, I'd use dill, which can serialize almost anything in python. Dill also has some good tools for helping you understand what is causing your

Is there an easy way to pickle a python function (or otherwise serialize its code)?

这一生的挚爱 提交于 2019-12-27 10:43:27
问题 I'm trying to transfer a function across a network connection (using asyncore). Is there an easy way to serialize a python function (one that, in this case at least, will have no side affects) for transfer like this? I would ideally like to have a pair of functions similar to these: def transmit(func): obj = pickle.dumps(func) [send obj across the network] def receive(): [receive obj from the network] func = pickle.loads(s) func() 回答1: You could serialise the function bytecode and then

How to replicate C# 'byte' and 'Write' in Python

三世轮回 提交于 2019-12-25 16:56:54
问题 For context I am trying to consume a streamed response from a soap API, which should output a CSV file. The response outputs a string coded in base 64, which I must write into the CSV file. The api documentation says that the response must be read to a destination buffer-by-buffer, but I am unfamiliar with c# so I am unsure on how to replicate byte and write in the correct context to do so. Here is the the code I am trying to replicate. The code was provided by the api's documentation: byte[]

pickle.PicklingError: Can't pickle '_subprocess_handle' object: <_subprocess_han dle object at 0x00AAAAAAA>

馋奶兔 提交于 2019-12-25 16:24:13
问题 I'm trying to write a test to compare methods for inter process communication in python, and OSX and windows are giving me problem with my first sample. it is pretty much the manual sample for using Queues. i wrote all the code on linux with python 2.7.9 and 3.4.1. And it works fine. #!/usr/bin/env python import multiprocessing as MP ... self.mpq_main = MP.Queue() self.mpq_net = MP.Queue() self.mpt_net = MP.Process( target=self.start_t_net, args=(self.mpq_main, self.mpq_net) ) ... def start_t

Pickle load: ImportError: No module named doc2vec_ext

北城余情 提交于 2019-12-25 08:49:59
问题 This is the structure I'm dealing with: src/ processing/ station_level/ train_paragraph_vectors.py doc2vec_ext.py word_embeddings_station_level.py I have trained and stored a model in word_embeddings_station_level.py like this: from src.doc2vec_ext import WeightedDoc2Vec # ... model = WeightedDoc2Vec( # ... ) train(model, vocab, station_sentences, num_epochs) # Saving the model -> pickles it model.save(open(model_file, "w")) This is working fine so far. However, I want to load that model in

Read list of lists of tuples in Python from file

夙愿已清 提交于 2019-12-25 08:49:59
问题 I'd like to read and write a list of lists of tuples from and to files. g_faces = [[(3,2)(3,5)],[(2,4)(1,3)(1,3)],[(1,2),(3,4),(6,7)]] I used pickle.dump(g_faces, fp) pickle.load(fp) But the file is not human readable. Is there an easy way to do it? 回答1: Try the json module. import json g_faces = [[(3,2), (3,5)],[(2,4), (1,3), (1,3)],[(1,2), (3,4), (6,7)]] json.dump(g_faces, open('test.json', 'w')) g_faces = json.load(open('test.json')) # cast back to tuples g_faces = [[tuple(l) for l in L]

unpickle sklearn.tree.DescisionTreeRegressor in python 2 from python3

孤街浪徒 提交于 2019-12-25 08:43:12
问题 I wanna fit model in python 3.5 (numpy 1.11.2, sklearn 0.18.1) import pickle from sklearn.tree import DecisionTreeRegressor clf = DecisionTreeRegressor() X = np.array([[1,2,3,4],[1,1,2,2],[1,2,1,2]]).T y = [1,1,0,0] clf.fit(X,y) with open(join(path_to_data, 'models', 'debug.model'), 'wb') as f: pickle.dump(clf, f, protocol=2) After pickling I try to unpickle model in python 2.7 (numpy 1.11.2, sklearn 0.18.1) import pickle with open(join(path_to_data, 'models', 'debug.model'), 'rb') as f: clf

How to load a pickle object and resolve certain references

。_饼干妹妹 提交于 2019-12-25 07:15:14
问题 Say we have an object we write to binary file using pickle . Let's say the object graph looks like this: foo1 +--->bar | \--->context +--->baz | +--->context | \--->qux \--->context Now the context objects are large datastructures and all instances of qux are the same. Therefore we decided to leave these contexxt objects out of the pickle process with: def __getstate__(self): my_dict = dict(self.__dict__) my_dict['context'] = None # We don't save the context return my_dict for the classes to