In general, there's 2 solutions to your "unflattening" problem. <br><br>A) The general solution: Keep track of the depth you're at, and add tabs/spaces ('\t' * depth) as necessary.<br clear="all">
<br>B) The xml solution: Use (for example) the dom.minidom module, and create a new "ul node" and populate it with children, at each level. Then, have the xml do the printing for you.<br><br><nitpick><br>
For what it's worth, I may have a slight nit-pick of your final output. I don't believe you can technically have a <ul> child of a <ul> node. I'm not absolutely sure on that, but my quick google searching has appeared to confirm. If that turns out to be true, you'll need to change the structure of your output.<br>
<br><ul><br> <li><br> 1,0,data<br> <ul><br> <li>555,1,image</li><br> ...<br> </ul><br> </li><br></ul><br><br>All of that is, of course, null and void if you define your own xml scheme. <br>
<br></nitpick><br><br><br>--<br><br>I enjoy haiku<br>but sometimes they don't make sense;<br>refrigerator?<br>
<br><br><div class="gmail_quote">On Fri, Jun 4, 2010 at 11:15 AM, <span dir="ltr"><<a href="mailto:jjcrump@uw.edu">jjcrump@uw.edu</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
All,<br>
<br>
any observations might be helpful. For the display of database contents I have the following problem:<br>
<br>
Database querys will return data to me in tuples of the following sort:<br>
each tuple has a unique id, a parent id, and a type.<br>
<br>
a parent id of 0 indicates that the element has no parent.<br>
not all data elements have children<br>
image elements have no children and must have a parent<br>
<br>
So the contents of the db is a tree of sorts:<br>
<br>
(1, 0, work)<br>
(555, 1, image)<br>
(556, 1, work)<br>
(557, 556, image)<br>
(558, 556, work)<br>
(559, 558, image)<br>
(560, 558, work)<br>
(561, 556, image)<br>
(562, 556, image)<br>
(563, 1, work)<br>
(564, 563, work)<br>
(565, 1, work)<br>
<br>
I have a function that will traverse this tree, summoning lists of tuples at each leaf. Well and good; it took a lot of trial and error, but it was a fine education in tree traversal.<br>
<br>
def makeHeirarchy(id):<br>
id = parentPath(id)[-1]<br>
rootimages = getImages(id[0])<br>
rootworks = getWorks(id[0])<br>
heirarchy = treeTraversal(rootworks, [id, rootimages])<br>
return heirarchy<br>
<br>
## which calls this recursive function:<br>
<br>
def treeTraversal(listIn,result):<br>
for x in listIn:<br>
if not getWorks(x[0]):<br>
result.append(x)<br>
result.append(getImages(x[0]))<br>
else:<br>
result.append(x)<br>
result.append(getImages(x[0]))<br>
treeTraversal(getWorks(x[0]), result)<br>
return result<br>
<br>
My problem is that this returns the tuples to me in a flat list.<br>
I've been trying to work out how I can get it to return some sort of nested structure: nested lists, dictionaries, or html thus:<br>
<br>
<ul><br>
<li>1,0,data</li><br>
<ul><br>
<li>555, 1, image</li><br>
<li>556, 1, data<li><br>
<ul><br>
<li>557, 556, image</li><br>
<li>561, 556, image</li><br>
<li>562, 556, image</li><br>
<li>558, 556, data</li><br>
<ul><br>
<li>559, 558, image</li><br>
<li>560, 558, image</li><br>
</ul><br>
</ul><br>
<li>563, 1, data</li><br>
<ul><br>
<li>564, 563, data</li><br>
</ul><br>
<li>565, 1, data</li><br>
</ul><br>
</ul><br>
<br>
_______________________________________________<br>
Tutor maillist - <a href="mailto:Tutor@python.org" target="_blank">Tutor@python.org</a><br>
To unsubscribe or change subscription options:<br>
<a href="http://mail.python.org/mailman/listinfo/tutor" target="_blank">http://mail.python.org/mailman/listinfo/tutor</a><br>
</blockquote></div><br>