I want to have a string representation of a python object. I used to do it with the famous pickle, but I'm wondering if it exists something better and less expensive. I don't like to use repr and eval cause they are not secure.
3 Answers
YAML is a fairly decent format for serializing datastructures. It's like JSON, but more so. The PyYAML library works fairly well. In addition to letting it guess how to serialize datastructures, you can get fairly specific. See their full documentation for examples.
2 Comments
import yaml fails.Use repr() to get the string representation, and ast.literal_eval() to recover the object.
Or just use pickle. If you are using Python 2.x you can import cPickle to get a faster pickle, but in Python 3.x there is just the one pickle and it is the C one.
Personally, I like to use JSON for simple objects; you might try import json and see if it works for you.
3 Comments
ast.literal_eval() will not reconstruct an object from its repr() except in a few limited cases. In fact, in the general case where __repr__ is not overloaded, and object cannot be reconstructed from its repr(), because that just looks like <module.name_of_class instance at 0xdeadbeef>.__repr__ to produce a string that looks like a constructor call, ast.literal_eval() still won't handle it because that's an expression, not a literal. You'd need to move up to eval, which is insecure.By object representation, if you mean serialization then pickle is an option. If you want a faster implementation try cPickle
pickledocs