pickle

TypeError: can't pickle generator objects

删除回忆录丶 提交于 2019-12-22 03:21:43
问题 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. 回答1: Don't use a generator expression when you want to pickle data. Use a list comprehension instead, or call list() on the generator to

Python, how to handle the “ValueError: unsupported pickle protocol: 4” error?

拜拜、爱过 提交于 2019-12-22 01:33:11
问题 I'm new to Python. I've to run this TargetFinder script ("Custom Analyses"). I installed all the required python packages, and copied the code into a script I named main.py , and ran it. I got this error: [davide@laptop]$ python main.py Traceback (most recent call last): File "main.py", line 8, in <module> training_df = pd.read_hdf('./paper/targetfinder/K562/output-epw/training.h5', 'training').set_index(['enhancer_name', 'promoter_name']) File "/usr/lib64/python2.7/site-packages/pandas/io

Python class: Employee management system

感情迁移 提交于 2019-12-22 00:15:18
问题 This exercise assumes that you have created the Employee class for Programming Exercise 4. Create a program that stores Employee objects in a dictionary. Use the employee ID number as the key. The program should present a menu that lets the user perform the following actions: • Look up an employee in the dictionary • Add a new employee to the dictionary • Change an existing employee’s name, department, and job title in the dictionary • Delete an employee from the dictionary • Quit the program

How does the automatic full qualification of class names work, in Python? [relevant to object pickling]

守給你的承諾、 提交于 2019-12-21 18:56:43
问题 (It is possible to directly jump to the question, further down, and to skip the introduction.) There is a common difficulty with pickling Python objects from user-defined classes: # This is program dumper.py import pickle class C(object): pass with open('obj.pickle', 'wb') as f: pickle.dump(C(), f) In fact, trying to get the object back from another program loader.py with # This is program loader.py with open('obj.pickle', 'rb') as f: obj = pickle.load(f) results in AttributeError: 'module'

AttributeError when reading a pickle file

*爱你&永不变心* 提交于 2019-12-21 17:52:30
问题 I get the following error when I'm reading my .pkl files on spyder (python 3.6.5): IN: with open(file, "rb") as f: data = pickle.load(f) Traceback (most recent call last): File "<ipython-input-5-d9796b902b88>", line 2, in <module> data = pickle.load(f) AttributeError: Can't get attribute 'Signal' on <module '__main__' from 'C:\\Python36\\lib\\site-packages\\spyder\\utils\\ipython\\start_kernel.py'> The context: My program is made of one file: program.py In the program, a class Signal is

How to save ctypes objects containing pointers

匆匆过客 提交于 2019-12-21 17:52:13
问题 I use a 3rd party library which returns after a lot of computation a ctypes object containing pointers. How can I save the ctypes object and what the pointers are pointing to for later use? I tried scipy.io.savemat => TypeError: Could not convert object to array cPickle => ctypes objects containing pointers cannot be pickled 回答1: Python has no way of doing that automatically for you: You will have to build code to pick all the desired Data yourself, putting them in a suitable Python data

Class for pickle- and copy-persistent object?

最后都变了- 提交于 2019-12-21 17:49:17
问题 I'm trying to write a class for a read-only object which will not be really copied with the copy module, and when it will be pickled to be transferred between processes each process will maintain no more than one copy of it, no matter how many times it will be passed around as a "new" object. Is there already something like that? 回答1: I made an attempt to implement this. @Alex Martelli and anyone else, please give me comments/improvements. I think this will eventually end up on GitHub. """

Django caching a large list

妖精的绣舞 提交于 2019-12-21 17:18:29
问题 My django application deals with 25MB binary files. Each of them has about 100,000 "records" of 256 bytes each. It takes me about 7 seconds to read the binary file from disk and decode it using python's struct module. I turn the data into a list of about 100,000 items, where each item is a dictionary with values of various types (float, string, etc.). My django views need to search through this list. Clearly 7 seconds is too long. I've tried using django's low-level caching API to cache the

Pickling an enum exposed by Boost.Python

时光毁灭记忆、已成空白 提交于 2019-12-21 17:01:39
问题 Is it possible to pickle (using cPickle) an enum that has been exposed with Boost.Python? I have successfully pickled other objects using the first method described here, but none of that seems to apply for an enum type, and the objects don't seem to be pickleable by default. 回答1: Not as they are in the module. I am given to understand that this is SUPPOSED to be possible, but the way the enum_ statement works prevents this. You can work around this on the python side. Somewhere (probably in

python multiprocessing - OverflowError('cannot serialize a bytes object larger than 4GiB')

∥☆過路亽.° 提交于 2019-12-21 15:38:53
问题 We are running a script using the multiprocessing library ( python 3.6 ), where a big pd.DataFrames is passed as an argument to a function : from multiprocessing import Pool import time def my_function(big_df): # do something time consuming time.sleep(50) if __name__ == '__main__': with Pool(10) as p: res = {} output = {} for id, big_df in some_dict_of_big_dfs: res[id] = p.apply_async(my_function,(big_df ,)) output = {u : res[id].get() for id in id_list} The problem is that we are getting an