Devin Jeanpierre
Pickle is usable in situations few other things are, because it can handle cyclic references and virtually any python object. The only stdlib alternative I'm aware of is json, which can do neither of those things. (Or at least, not without significant extra serialization code.) I would imagine that any alternative supplied should be easy enough to use that pickle users would seriously consider switching, and include at least those features.
What I'm about to mention may or may not meet your use case, but there is a way in which JSON can support cyclic references and virtually any Python object. Not without *some* work and *some* limitations, obviously, but then, TANSTAAFL :-) In logging.dictConfig, we use a dict to configure logging, but the underlying mechanism is more general. It allows you to construct objects outside the dict using a syntax such as 'ext://sys.stderr', and objects inside the dict using syntax such as 'cfg://xyz.abc[1]'. To illustrate, consider the following JSON: { "v1": "ext://sys.stderr", "v2": "cfg://v3.some_list[1]", "v3": { "some_list": [1, 2, 3], "some_value": "cfg://v4.key" }, "v4": { "key": "value", "some_value": "cfg://v3.some_value" } } You can see that sys.stderr is the external object, and that there are various cyclic references. Now, using Python 2.7 (where dictConfig was introduced), you can do this:
import json with open("example.json") as f: ... d = json.load(f) ... d {u'v1': u'ext://sys.stderr', u'v2': u'cfg://v3.some_list[1]', u'v3': {u'some_lis t': [1, 2, 3], u'some_value': u'cfg://v4.key'}, u'v4': {u'some_value': u'cfg://v 3.some_value', u'key': u'value'}} import logging.config bc = logging.config.BaseConfigurator(d) bc.config['v1']
bc.config['v2'] 2 bc.config['v3'] {u'some_list': [1, 2, 3], u'some_value': u'cfg://v4.key'} bc.config['v3']['some_value'] u'value' bc.config['v4']['some_value'] u'value'
The values are lazily evaluated (as you can see from the above example for bc.config['v3'], but that isn't necessarily a problem. Of course, one can build on this using keys to define classes, kwargs, etc., which is how logging's DictConfigurator (derived from BaseConfigurator) works. The handlers for ext://, cfg:// are customisable, and additional schemes can be added; it allows for fairly flexible configuration, but within bounds you can set. I probably should write a post about the BaseConfigurator, it is useful outside the context of logging and I have used it for other things, but it's hidden away under the covers a bit. Regards, Vinay Sajip