pickle

Persisting hashlib state

I'd like to create a hashlib instance, update() it, then persist its state in some way. Later, I'd like to recreate the object using this state data, and continue to update() it. Finally, I'd like to get the hexdigest() o the total cumulative run of data. State persistence has to survive across multiple runs. Example: import hashlib m ...

Hitting Maximum Recursion Depth Using Python's Pickle / cPickle

The background: I'm building a trie to represent a dictionary, using a minimal construction algorithm. The input list is 4.3M utf-8 strings, sorted lexicographically. The resulting graph is acyclic and has a maximum depth of 638 nodes. The first line of my script sets the recursion limit to 1100 via sys.setrecursionlimit(). The prob...

How can I pickle suds results?

To avoid repeatedly accessing a SOAP server during development, I'm trying to cache the results so I can run the rest of my code without querying the server each time. With the code below I get a PicklingError: Can't pickle <class suds.sudsobject.AdvertiserSearchResponse at 0x03424060>: it's not found as suds.sudsobject.AdvertiserSearch...

python 2.6 cPickle.load results in EOFError

I use cPickle to pickle a list of integers, using HIGHEST_PROTOCOL, cPickle.dump(l, f, HIGHEST_PROTOCOL) When I try to unpickle this using the following code, I get an EOFError. I tried 'seeking' to offset 0 before unpickling, but the error persists. l = cPickle.load(f) Any ideas? ...

Using pickle with cucumber and factory_girl to create associated models and pass parameters through to the nested model

I have the following models: class User < ActiveRecord::Base has_one :profile, :dependent => :destroy def before_create self.profile ||= Profile.new end end class Profile < ActiveRecord::Base belongs_to :user validates_uniqueness_of :name end And I have the following factories: Factory.define :user do |user| ...

Why am I getting an error about my class defining __slots__ when trying to pickle an object?

I'm trying to pickle an object of a (new-style) class I defined. But I'm getting the following error: >>> with open('temp/connection.pickle','w') as f: ... pickle.dump(c,f) ... Traceback (most recent call last): File "<stdin>", line 2, in <module> File "/usr/lib/python2.5/pickle.py", line 1362, in dump Pickler(file, protocol...

Custom instance unpickling in Python: should the object dictionary be updated, or is replacing it OK?

When defining how objects of a certain class should be unpickled, via __setstate__, I gather that it is safe to do def __setstate__(self, dict_returned_by_pickle): self.__dict__.update(dict_returned_by_pickle) when the pickled state is a dictionary. This is what I have seen in an answer here on stackoverflow. However, is this a ...

Pickle or json?

I need to save to disk a little dict object which keys are strings and values are ints and then recover it. Something like this: {'juanjo': 2, 'pedro':99, 'other': 333} Which and why is the best option? Serialize it with pickle or with simplejson? I'm using Python 2.6 ...

Problem using cPickle

Could you helpme to make this exmaple work? I'd like to load a serialized dict if it exists, modify it and dump it again. I think I have a problem with the mode I'm using to open the file but I don't know the correct way. import os import cPickle as pickle if os.path.isfile('file.txt'): cache_file = open('file.txt', 'rwb') cac...

Trying to unpickle a string from a django model : insecure string pickle

I'm trying to store pickled objects in a django models.TextField. But when I try to unpickle my data (with loads) I get "insecure string pickle". What can I do about this? ...

Caching suds-object. Unicode problem

I'm using suds https://fedorahosted.org/suds/ to fetch data using SOAP. I'd like to cache the result (using memcached) to not overload the server from where I'm fetching the data. The problem is when fetching the cached data. Fetching it works fine but then django tries to decode the data (force_unicode) and it fails with the following: ...

Pickling array.array in 2.4 using cPickle

Hey All, I am working on a project built on python 2.4 (It is an embedded python project, so I don't have a choice on the version of python used). Throughout the application, we use array.array to store data. Support for pickling array.array objects was added to pickle (and cPickle) in 2.5. We have a viable workaround in 2.4 when usi...

Exclude object's field from pickling in python

I would like to avoid pickling of certain fields in an instance of a class. Currently, before pickling I just set those fields to None, but I wonder whether there's more elegant solution? ...

Pickled Object Versioning

I am working on a project where we have a large number of objects being serialized and stored to disk using pickle/cPickle. As the life of the project progresses (after release to customers in the field) it is likely that future features/fixes will require us to change the signature of some of our persisted objects. This could be the a...

Serializing a suds object in python

Ok I'm working on getting better with python, so I'm not sure this is the right way to go about what I'm doing to begin with, but here's my current problem... I need to get some information via a SOAP method, and only use part of the information now but store the entire result for future uses (we need to use the service as little as pos...

Any way to set or overwrite the __line__ and __file__ metadata?

I'm writing some code that needs to change function signatures. Right now, I'm using Simionato's FunctionMaker class, which uses the (hacky) inspect module, and does a compile. Unfortunately, this still loses the line and file metadata. Does anyone know: If it is possible to overwrite these values in some odd way? If hacking up a c...

How to customize pickle for django model objects

My app uses a "per-user session" to allow multiple sessions from the same user to share state. It operates very similarly to the django session by pickling objects. I need to pickle a complex object that refers to django model objects. The standard pickling process stores a denormalized object in the pickle. So if the object changes ...

Record id with cucumber and pickle [Rails]

I am using Cucumber, Webrat, and Pickle in conjunction. When I write a scenario, I can do something like this: Given a product exists with title: "Bread" When I go to the edit page for that product And I fill in "Title" with "Milk" And I press "Save changes" Then I should see "Successfully edited product." And I should be on that car's ...

Cucumber Record ID

Given the following in Cucumber: Given a car exists with title: "Toyota" And I go to path "/cars" And I follow "Toyota Page" And I should be on path "/cars/CAR_ID" Where CAR_ID is the ID of the car titled "Toyota". How do I figure out that ID? Thanks! ...

DjangoUnicodeDecodeError while storing pickle'd data.

I've got a simple dict object I'm trying to store in the database after it has been run through pickle. It seems that Django doesn't like trying to encode this error. I've checked with MySQL, and the query isn't even getting there before it is throwing the error, so I don't believe that is the problem. The dict I'm storing looks like ...