Best way to represent an outline?
Michael Gilfix
mgilfix at eecs.tufts.edu
Sun May 12 20:30:03 EDT 2002
This sounds like a great use for XML. But if you feel that's too
fancy, and it may very well be overkill, you just need to create
a simple tree structure. You could have a simple node class and
implement a simple tree that uses a list to store ordered children:
class SimpleNode:
def __init__ (self):
name = None
data = None
order = [ ] # List of child names
children = { } # Name - To node object mapping
class Tree:
def __init__ (self):
self.root = SimpleNode ()
self.root.name = "Root"
def add_node (self, path):
pass
def find_node (self, path):
pass
# etc...
Where a child can be a tuple of child name and object.
You can then search through the hierarchy. I used something
similar this to implement a simple file system cache. You
can also play tricks to increase speed. In my case, there
was no ordering involved, so I didn't need any ordering
and just used a child mapping. It's pretty straightforward
and you might feel more comfortable with it. Hope that helps.
-- Mike
On Thu, May 09 @ 08:02, VanL wrote:
> Hello,
>
> I'm trying to figure out the best way to represent an
> outline, but I'm not sure what to use for each node:
>
> A dict: This is probably the best fit, with the name serving
> as a key to another dict with different types of content.
> But the problem is that an outline is ordered, where dicts
> are inherently unordered.
>
> A class: This would probably work, but I don't want the
> types of items that I will include within each class
> instance to necessarily be pre-defined. So it seems like it
> would be difficult to loop over the instance and discover
> everything in a particular node. Plus, even if I used the
> __dict__, I still have the ordering problem described above.
>
> A list: This satisfies the ordering property needed for an
> outline, but I'm not sure where I would stick the variable
> content of each node.
>
> Any suggestions?
>
> TIA,
>
> Van
>
> --
> http://mail.python.org/mailman/listinfo/python-list
`-> (news)
--
Michael Gilfix
mgilfix at eecs.tufts.edu
For my gpg public key:
http://www.eecs.tufts.edu/~mgilfix/contact.html
More information about the Python-list
mailing list