Best way to represent an outline?
Dave Kuhlman
dkuhlman at rexx.com
Mon May 13 16:08:16 EDT 2002
holger krekel wrote:
> 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:
>
> could you give an example/definition what you mean
> by 'outline'?
>
> holger
I'm going to guess a bit about what you mean by an outline.
An outline is a proper tree, i.e. a set of nodes, each of which has
some content, possibly some attributes, and a set of (possibly
empty) children. Every note has exactly one parent node except of
one (special) node, the root node.
So, one thing to notice is that every XML document satisfies this
definition. (And, *no*, I do *not* think that every problem cries
out for an XML solution. 2%, possibly more, of the world's problems
can be solved in some other, admittedly less attractive, way.)
Therefore, (#1) you might try encoding a sample outline as an XML
document, then read it in using PyXML and minidom, then do your
processing on the minidom tree.
Or, (#2) you might read the XML document document into minidom,
then walk that tree and create a tree of nodes, where each node is
defined by a Python class. Here is a sample class definition to
get you started:
# ===========================================
class Node:
def __init__(self, label='', text='', children=None):
self.label = label
self.text = text
if children is None:
self.children = []
else:
self.children = children
def show(self, outfile, level):
self.showLevel(outfile, level)
outfile.write('%s. %s\n' % (self.label, self.text))
for child in self.children:
child.show(outfile, level + 1)
def showLevel(self, outfile, level):
for idx in range(level):
outfile.write(' ')
# ===========================================
And (#3), if you describe your XML representation of an outline in
XML Schema, then I have a Python script that will generate the code
that represents the classes and will parse the XML representation
and create the tree structure of instances of those classes. You
can find generateDS.py along with an outline example at my Web site
(http://www.rexx.com/~dkuhlman/#generateDS). You will have to
install PyXML in order to use it.
Note, that XML is only one way to represent an outline/tree in
text. Using XML has the advantage that there are parsers available
and so part of you work has been done for you. If we were Lisp
people, we'd use S-expressions and lots of parentheses. (Argh! I
knew I should not have said "Lisp". Now, we've started another "Re:
Is Python better than X, Was: ..."-war.)
- Dave
--
Dave Kuhlman
dkuhlman at rexx.com
http://www.rexx.com/~dkuhlman
More information about the Python-list
mailing list