in line responses...<br><br><div class="gmail_quote">On Thu, Oct 8, 2009 at 5:58 PM, Dennis Lee Bieber <span dir="ltr"><<a href="mailto:wlfraed@ix.netcom.com">wlfraed@ix.netcom.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
On Thu, 8 Oct 2009 10:38:02 -0500, Victor Subervi<br>
<<a href="mailto:victorsubervi@gmail.com">victorsubervi@gmail.com</a>> declaimed the following in<br>
gmane.comp.python.general:<br>
<br> First suggestion... Get rid of the confusing dat[0] and dat[1] and<br>
use names that make sense... You can do this via tuple unpacking in the<br>
"for" statement...<br>
<br>
for (flda, fldb) in list_of_tuples:<br></blockquote><div><br>Nice! <br></div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><br>
cursor.execute(<br>
"select Category from categories order by Parent, ID")<br>
Categories = [itm[0] for itm in cursor] #untuple single column<br>
cursor.execute(<br>
"select Parent from categories order by Parent, ID")<br>
Parents = [itm[0] for itm in cursor]<br>
<br>
The above will create the equivalent of your primary two lists --<br>
while also keeping them linked positionally.<br></blockquote><div><br>Also nice! But doesn't the following achieve the same goal even more cleanly?<br><br> cursor.execute(<br> "select Category, Parent from categories order by Parent, ID")<br>
Categories, Parents = [itm[0], itm[1] for itm in cursor]<br> <br></div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
MAXLEVEL = 15<br>
<br>
cursor.execute("""create table if not exists Categories<br>
( ID integer auto_increment primary key,<br>
Name varchar(40) not null,<br>
unique (Name)<br>
)""" )<br>
<br>
cursor.execute("""create table if not exists Relationship<br>
( ID integer auto_increment primary key,<br>
Parent integer not null<br>
foreign key (Categories.ID),<br>
Child integer not null<br>
foreign key (Categories.ID),<br>
check (Parent <> Child) )""" )<br></blockquote><div><br>The MySQL manual states "The CHECK clause is parsed but ignored by all storage<br>engines." So, how does this help?<br>
</div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<br>
# Note that in this schema you<br>
# 1) never duplicate NAMES<br>
# 2) separate the NAMES from the parent<>child linkage<br>
<br>
def expand(fetched):<br>
aDict = {}<br>
for (name, ) in fetched:<br>
aDict[name] = {}<br>
return aDict<br></blockquote><div><br>Does this not simply make an empty dictionary for every item? I see where you call it here:<br> levelDict[nm] = expand(cursor.fetchall())<br>but don't understand its purpose.<br>
</div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<br>
def getChildren(levelDict, level = 0):<br>
if level > MAXLEVEL:<br>
return #possibly the data has a cycle/loop<br>
for (nm, dt) in levelDict:<br>
cursor.execute("""select <a href="http://c.name" target="_blank">c.name</a> from Categories as c<br>
inner join Relationship as r<br>
on c.ID = r.Child<br>
inner join Categories as p<br>
on r.Parent = p.ID<br>
where p.Name = %s<br>
order by <a href="http://c.name" target="_blank">c.name</a>""",<br>
(nm,) )<br>
levelDict[nm] = expand(cursor.fetchall())<br>
#recursive call to do next level<br>
getChildren(levelDict[nm], level + 1)<br>
# no data return as we are mutating dictionaries in place<br></blockquote><div><br>"nm" must refer to "name". What is "dt"? It appears it's never used. What is its purpose?<br><br>
Thanks, Dennis! A really good lesson here! Look forward to learning exactly how to do this. Thanks for your help!<br>V<br></div></div><br>