471,347 Members | 1,683 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,347 software developers and data experts.

Recursive loading trouble for immutables

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
1 1302
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)

(4, 5, [1, 2, 3, (4, 5, [...])])
'(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.

Nov 23 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by LoserInYourFaceEngineer | last post: by
7 posts views Thread by Jon Slaughter | last post: by
3 posts views Thread by samuelberthelot | last post: by
4 posts views Thread by Mike | last post: by
10 posts views Thread by AsheeG87 | last post: by
3 posts views Thread by MartinRinehart | last post: by
3 posts views Thread by AliRezaGoogle | last post: by
reply views Thread by Ronak mishra | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.