By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,271 Members | 1,722 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,271 IT Pros & Developers. It's quick & easy.

Recursive loading trouble for immutables

P: n/a
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. It is more or less complete except for a single issue I just cannot figure out by myself: How to load data that specifies immutables that recursively reference themselves.

There are only a few solutions I can think of.

One: While loading recursive objects, I always create empty versions of objects (lists, dicts, classes) etc, and fill them in afterwards. This works fine for loading recursive lists and such, but as immutables are, well, immutable, this gets me nowhere with important datatypes like tuples.

Two: Global replacement. If I dont remember incorrectly, PyPy has a function for simply globally replacing all references to a given object with another. This would make the loading code a piece of cake, although I assume this functionality doesn't exist in CPython? This is the second time I've had good use for it.

Three: Create transparent proxies everywhere. Just kidding.

Four: Disallow immutable recursiveness. This is bad for two reasons. Firstly, it requires me to greatly increase the complexity of the loading code as I have to topsort all references to avoid recursiveness in immutables while at the SAME TIME allow mutables to be recursive. I can't imagine how unelegant the code will be. Secondly, there is nothing wrong with recursive tuples. To disallow them and work miles around them just because they can't be properly expressed in Python

In any case I am stumped. It's the last piece of a module I am otherwise very pleased with. There must be a way. I certainly know most people on this list can get around python much better than I do, so, any ideas?

----------------------------------------------------------------
Brought to you by Footard: http://www.footard.com
Please report abuse/spam/scams to reportabuse at footard dot com
Nov 23 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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\na 0000g1\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.
Nov 23 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.