Python: can’t pickle module objects error

Python’s inability to pickle module objects is the real problem. Is there a good reason? I don’t think so. Having module objects unpicklable contributes to the frailty of python as a parallel / asynchronous language. If you want to pickle module objects, or almost anything in python, then use dill. Python 3.2.5 (default, May 19 … Read more

How to find user id from session_data from django_session table?

I had trouble with Paulo’s method (see my comment on his answer), so I ended up using this method from a scottbarnham.com blog post: from django.contrib.sessions.models import Session from django.contrib.auth.models import User session_key = ‘8cae76c505f15432b48c8292a7dd0e54’ session = Session.objects.get(session_key=session_key) uid = session.get_decoded().get(‘_auth_user_id’) user = User.objects.get(pk=uid) print user.username, user.get_full_name(), user.email

Python: How do I write a list to file and then pull it back into memory (dict represented as a string convert to dict) later?

Why not use python pickle? Python has a great serializing module called pickle it is very easy to use. import cPickle cPickle.dump(obj, open(‘save.p’, ‘wb’)) obj = cPickle.load(open(‘save.p’, ‘rb’)) There are two disadvantages with pickle: It’s not secure against erroneous or maliciously constructed data. Never unpickle data received from an untrusted or unauthenticated source. The format … Read more

Attacking Python’s pickle

Yes and no… No – unless there’s a bug with the interpreter or the pickle module, you can’t run arbitrary code via pickled text, or something like that. unless the pickled text is evaled later, or you’re doing stuff like creating a new object with a type mentioned in this data. Yes – depending on … Read more

Relationship between pickle and deepcopy

You should not be confused by (1) and (2). In general, Python tries to include sensible fall-backs for missing methods. (For instance, it is enough to define __getitem__ in order to have an iterable class, but it may be more efficient to also implement __iter__. Similar for operations like __add__, with optional __iadd__ etc.) __deepcopy__ … Read more

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

The Pickle protocol is basically the file format. From the documentation, The higher the protocol used, the more recent the version of Python needed to read the pickle produced. … Pickle protocol version 4 was added in Python 3.4, your python version (2.7.5) does not support this. Either upgrade to Python 3.4 or later (current … Read more

copy.deepcopy vs pickle

Problem is, pickle+unpickle can be faster (in the C implementation) because it’s less general than deepcopy: many objects can be deepcopied but not pickled. Suppose for example that your class A were changed to…: class A(object): class B(object): pass def __init__(self): self.b = self.B() now, copy1 still works fine (A’s complexity slows it downs but … Read more

What are the pitfalls of using Dill to serialise scikit-learn/statsmodels models?

I’m the dill author. dill was built to do exactly what you are doing… (to persist numerical fits within class instances for statistics) where these objects can then be distributed to different resources and run in an embarrassingly parallel fashion. So, the answer is yes — I have run code like yours, using mystic and/or … Read more

What’s the most space-efficient way to compress serialized Python data?

I’ve done some test using a Pickled object, lzma gave the best compression. But your results can vary based on your data, I’d recommend testing them with some sample data of your own. Mode LastWriteTime Length Name —- ————- —— —- -a—- 9/17/2019 10:05 PM 23869925 no_compression.pickle -a—- 9/17/2019 10:06 PM 6050027 gzip_test.gz -a—- 9/17/2019 … Read more

tech