473,388 Members | 1,496 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 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 1443
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: LoserInYourFaceEngineer | last post by:
Hello All: I'm having trouble with a recursive function. The function is supposed to identify nested folders in a hierarchical folder structure. The function "searchForFolders()" is...
7
by: Jon Slaughter | last post by:
#pragma once #include <vector> class empty_class { }; template <int _I, int _J, class _element, class _property> class RDES_T {
3
by: samuelberthelot | last post by:
Hi, I'm trying to write a recursive fucntion that takes as parameters a html div and an id. I have to recurse through all the children and sub-children of the div and find the one that matches the...
4
by: Mike | last post by:
Hello, I'm trying to use a recursive CTE to find some data but I am having trouble crafting the CTE; maybe what I want isn't possible. I have the following tables: EMPLOYEE (int ID, int...
10
by: AsheeG87 | last post by:
Hello Everyone! I have a linked list and am trying to include a recursive search. However, I am having trouble understanding how I would go about that. I don't quite understand a recursive...
6
by: Shigun | last post by:
On a website I am working on I am trying to load another page into a div on the the page the user does his work from. What I have works correctly in FireFox, but not in IE. I've rummaged Google for...
3
by: MartinRinehart | last post by:
From the manual: "code objects are immutable and contain no references (directly or indirectly) to mutable objects" (3.2) I thought my code worked with both mutable and immutable objects....
3
by: AliRezaGoogle | last post by:
Dear Members, I have written a recursive function. It calls itself recursively. It is placed inside a thread. So I can easily suspend and resume the thread to suspend or resume the function as...
3
by: -Lost | last post by:
Runtime dependency loading, worth the trouble? I've been researching methods of "dynamically" loading and unloading dependencies at runtime (trying my best to think of a nice solution of loading...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.