pickle

How to pickle several .txt files into one pickle

倾然丶 夕夏残阳落幕 提交于 2019-12-11 12:09:42
问题 I need to overcome some cPickle constrains, namely i need to open several files and pickle them to one file, like this: import cPickle file1=open('file1.txt','r') file2=open('file2.txt','r') obj=[file1,file2] or obj=[file1.read(), file2.read()] cPickle.dump(obj,open('result.i2','w'),2) so that later I can "repickle" them and get the data. Is a cPickle good way to do so?If yes how can I do it properly If not, what would be suitable? Thanks in advance Rafal 回答1: This is the correct way, it

Python - pickle.load() takes one positional argument (2 given)

冷暖自知 提交于 2019-12-11 12:05:54
问题 This produces an error: pickle.load() takes one positional argument (2 given) Here is my code: import pickle, os.path created = False phoneBook = {} name = input("Please enter a name(or press enter to end input): ") while name != '': number = input("Please enter number: ") phoneBook[name] = number name = input("Please enter a name(or press enter to end input): ") if name == '': print("Thank You!") print("Your phonebook contains the following entries:") for name, number in phoneBook.items():

PicklingError: Can't pickle <class …>: it's not the same object as … in GAE

六眼飞鱼酱① 提交于 2019-12-11 11:16:46
问题 I'm getting a PicklingError from this line of code in my GAE Python app: deferred.defer(email_voters_begin, ekey, voter_list) The three arguments are: email_voters_begin -- A Python function, eg., function email_voters_begin at 0x1035d4488 ekey -- A key to an entity I defined, eg., prints as agdvcGF2b3Rlcg4LEghFbGVjdGlvbhgCDA voter_list -- A list of objects I defined, eg., [models.Voter object at 0x103d3d310, ... ] When this line executes as part of my tests (with webtest and nosegae), I get

NotImplementedError: X is not picklable

北慕城南 提交于 2019-12-11 10:37:11
问题 Using Python 3.x, I am trying to iterate over a dictionary of datasets (NetCDF4 datasets). They are just files... I want to examine each dataset on a separate process: def DoProcessWork(datasetId, dataset): parameter = dataset.variables["so2"] print(parameter[0,0,0,0]) if __name__ == '__main__': mp.set_start_method('spawn') processes = [] for key, dataset in datasets.items(): p = mp.Process(target=DoProcessWork, args=(key, dataset,)) p.start() processes.append(p) When I run my program, I get

Unpickle binary file to text [duplicate]

China☆狼群 提交于 2019-12-11 09:55:31
问题 This question already has answers here : Is there a way to view cPickle or Pickle file contents without loading Python in Windows? (4 answers) Closed 11 months ago . I need to do some maintenance on a system that basically looks like: (Complicated legacy Python program) -> binary pickle file -> (Another complicated legacy Python program) Which requires figuring out exactly what is in the intermediate pickle file. I suspect the file format is much simpler than the codes that generate and

How to persist Python class with member variables that are also Python classes having large `numpy` array variables (so `pickle` no longer efficient)?

半城伤御伤魂 提交于 2019-12-11 09:44:05
问题 The use case: Python class stores large numpy arrays (large, but small enough that working with them in-memory is a breeze) in a useful structure. Here's a cartoon of the situation: main class: Environment ; stores useful information pertinent to all balls "child" class: Ball ; stores information pertinent to this particular ball Environment member variable: balls_in_environment (list of Ball s) Ball member variable: large_numpy_array (NxN numpy array that is large, but still easy to work

Textual reference of a method

橙三吉。 提交于 2019-12-11 09:04:37
问题 Say I have the following: def func(): print 'this is a function and not a method!!!' class Test: def TestFunc(self): print 'this is Test::TestFunc method' I have the following functions (which are taken from https://bitbucket.org/agronholm/apscheduler/src/d2f00d9ac019/apscheduler/util.py): def get_callable_name(func): """ Returns the best available display name for the given function/callable. """ f_self = getattr(func, '__self__', None) or getattr(func, 'im_self', None) if f_self and hasattr

Shove knowing about an object but unable to retrieve it

徘徊边缘 提交于 2019-12-11 08:26:08
问题 After a lot of playing around I found a workaround for some weird behaviour but would like to understand what's going on. Sorry if I am missing elementary stuff but I am pretty inexperienced in python. So... I am storing an object in Shove in one script and retrieving in another one - all works fine if I use my workaround shove_repro_class.y class MyClass(): def __init__(self, name ): self.name = name self.othername = "%s" % name ## <=== workaround for ## <=== self.othername = name def __repr

Cannot open pickled dictionary of DataFrames from a previous Python/pandas version

这一生的挚爱 提交于 2019-12-11 07:59:34
问题 I have a pickled item stored, created 2 years ago, under Python 2 and pandas version<0.17 , which I cannot figure how to open. The item contains a dictionary with some pd.DataFrames as values assigned to the keys. Back then, I was on Windows 7 and I can't remember if I used an encoding different than the predefined. In Python3, after trying different combinations of open modes ('r','rb') for the file handler, pickle's fix_import=True option and some more solutions I found here, I couldn't

How to make a class which has __getattr__ properly pickable?

自古美人都是妖i 提交于 2019-12-11 07:35:43
问题 I extended dict in a simple way to directly access it's values with the d.key notation instead of d['key'] : class ddict(dict): def __getattr__(self, item): return self[item] def __setattr__(self, key, value): self[key] = value Now when I try to pickle it, it will call __getattr__ to find __getstate__ , which is neither present nor necessary. The same will happen upon unpickling with __setstate__ : >>> import pickle >>> class ddict(dict): ... def __getattr__(self, item): ... return self[item]