Recursive loading trouble for immutables

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Sat Nov 24 00:19:20 CET 2007


On Fri, 23 Nov 2007 16:43:28 -0600, rekkufa wrote:

> I am currently building a system for serializing python objects to a
> readable file-format, as well as creating python objects by parsing the
> same format.

You mean like pickle? (Pardon me for telling you something you may 
already know, but then you may not already know it...)


>>> import pickle
>>> 
>>> # Create a recursive tuple.
... alist = [1, 2, 3]
>>> atuple = (4, 5, alist)
>>> alist.append(atuple)
>>> 
>>> atuple
(4, 5, [1, 2, 3, (4, 5, [...])])
>>> pickle.dumps(atuple)
'(I4\nI5\n(lp0\nI1\naI2\naI3\na(I4\nI5\ng0\ntp1\na0000g1\n.'


pickle can dump to files, using either text or binary protocols, and load 
objects back from either files or strings. I won't pretend the text 
protocol is exactly human readable, but perhaps the way forward is to 
write a post-processor to convert the output of pickle to something more 
human-readable, rather than writing a completely new serializer. 



-- 
Steven.



More information about the Python-list mailing list