dynamic naming for hierarchical data problem

Jeremy Jones dnjjones at yahoo.com
Thu Aug 9 02:02:23 CEST 2001


I apologize if this is a dupe.  I tried to post earlier and
it appeared not to send out.

I want to be able to dynamically build a hierarchical
set of data.  (Forgive me if I am not using the proper
terminology or if what I mean by a term is completely
different from what the rest of you mean by the same
term.  I am still a very new newbie and am just
converting to Python after a couple of years of perl.
I will try to clarify what I mean.)  By hierarchical,
I mean that there will be parent-child relationships
and that the parents and children should be aware of
one another in their respective relationships (i.e.
the parent should be aware of its children and
children should be aware of their parents).  By
dynamic, I mean that the names in the set of data will
very likely be extracted from a text file or entered
in by a user at run time.  I also mean that the number
of levels in the hierarchy will potentially be
variable and will be defined at run time.  I would
prefer this set of data to be contained in one object.
 Right now, I have the following, which is a three
tier set of dictionaries that are associated with each
other:

################################
cs1_cmd1 = {'OPEN': 'RESULTS'}
cs1_cmd2 = {'CLOSE': 'RESULTS'}
cs2_cmd1 = {'OPEN': 'RESULTS'}
cs2_cmd2 = {'CLOSE': 'RESULTS'}
cs2_cmd3 = {'CLOSE2': 'RESULTS'}
cs3_cmd1 = {'OPEN': 'RESULTS'}
cs3_cmd2 = {'CLOSE': 'RESULTS'}

command_set1 = {'cmd1': cs1_cmd1, 'cmd2': cs1_cmd2}
command_set2 = {'cmd1': cs2_cmd1, 'cmd2': cs2_cmd2,
'cmd3': cs2_cmd3}
command_set3 = {'cmd1': cs3_cmd1, 'cmd2': cs3_cmd2}

test_set = {'TEST1': command_set1,'TEST2':
command_set2,'TEST3': command_set3}
#################################

which I can manipulate with much weeping gnashing of
teeth.  A couple of problems with this (other than the
fact that it is just plain hideous to look at):

1) A child doesn't really know who its parent is.
2) The data is not dynamic: it has to be built from
within the script at design time and not run time.
3) On the dynamic topic, I can't just add a child.  I
have to create kind of a reference in the parent to
another dictionary.
4) It is scattered across 11 dictionaries (even though
it has one base dictionary that acts as the root
parent).

I want to do the above more elegantly, more concisely
and less painfully.  I have read the python FAQ,
searched usenet, searched Google, scoured
www.python.org and have only come up with a few
possibilities for doing what I want to do (such as
metaclasses and acquisition and some virtual file
systems), but I am not sure that they will address the
solution for the problem that I have.

Does anyone have any ideas on how I can begin to
attack my problem?  Does anyone have any ideas on
specific methodologies for dynamically named
hierarchical data?  Can anyone point me in the
direction of documentation that might aid me in my
quest?  Does anyone know of similar projects that I
may be able to look at?

Any assistance would be most appreciated.  Thanks in advance.


Jeremy Jones







More information about the Python-list mailing list