dynamic naming for hierarchical data problem

Quinn Dunkan quinn at retch.ugcs.caltech.edu
Wed Aug 8 21:47:05 EDT 2001


On Thu, 09 Aug 2001 00:02:23 GMT, Jeremy Jones <dnjjones at yahoo.com> wrote:
>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:

Is this close?

class Node:
    def __init__(self, val, parent=None):
        self.parent = parent
        self.children = {}
        for k in val.keys():
            self[k] = val[k]
    def __getitem__(self, k):
        return self.children[k]
    def __setitem__(self, k, v):
        if hasattr(v, 'keys'):
            self.children[k] = Node(v, self)
        else:
            self.children[k] = Leaf(v, self)
    def keys(self):
        return self.children.keys()
    def clear(self):
        for v in self.children.values():
            v.clear()
        self.children.clear()
    def __repr__(self):
        return 'Node(%s)' % repr(self.children)

class Leaf:
    def __init__(self, val, parent):
        self.parent = parent
        self.val = val
    def clear(self):
        self.parent = None
    def __repr__(self):
        return 'Leaf(%s)' % repr(self.val)

>>> n = t.Node({'foo': 1, 'bar': {1: 10, 2: 20}})
>>> n
Node({'foo': Leaf(1), 'bar': Node({2: Leaf(20), 1: Leaf(10)})})
>>> n['bar'][1]
Leaf(10)
>>> n['bar'][1].parent.parent
Node({'foo': Leaf(1), 'bar': Node({2: Leaf(20), 1: Leaf(10)})})
>>> 


Notice that this has cycles all over the place.  I put in 'clear' to try to
break them, but I've never built a directly cyclic structure before, so maybe
I'm doing it wrong.

>################################
>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}
>#################################

test_set = Node({
    'TEST1': {
        'cmd1': {'OPEN': 'RESULTS'},
        'cmd2': {'CLOSE': 'RESULTS'},
    },
    'TEST2': {
        etc.
    }
})

test_set['TEST4'] = { blah blah }



Editorial:
I don't really know what you're doing here, but it seems likely that you're
choosing some tortuous weird difficult solution to a problem which could be
solved in a much easier way.  Complicated cyclic data structures, repetitive
code, and lots of tiny dictionaries are all warning signs to me that I've
chosen the wrong approach.



More information about the Python-list mailing list