Re: Nested Menus
This is probably more appropriate for the MySQL list, but since this is Dennis' pseudo-code... Dennis wrote the following: cursor.execute(create table if not exists Relationship (ID integer auto_increment primary key, Parent integer not null, foreign key (Categories.ID), Child integer not null, foreign key (Categories.ID), check (Parent Child) );) This code throws an error in MySQL around Categories.ID. I have never worked with foreign keys. It seems there should be a references statement after that...but what does it reference? Also, why is it repeated twice? TIA, V On Sat, Oct 10, 2009 at 1:58 AM, Dennis Lee Bieber wlfr...@ix.netcom.comwrote: On Fri, 9 Oct 2009 00:04:02 -0700 (PDT), r rt8...@gmail.com declaimed the following in comp.lang.python: This is not a personal attack on you but your writing style is so horrendous i must at least notify you of it. Go to this link and view your post in GG's and let me know what you think of it. This is the first time, in over a decade of Usenet posting, I've been accused of having a horrendous style... Occasional over-detailed technical harangues, perhaps -- often declared as such by me as a preface to the post itself. http://groups.google.com/group/comp.lang.python/browse_thread/thread/6301372f8e581a74?hl=en# What the web-based Google Groups does to a post is beyond my control. It is not a proper Usenet client. Though if it means anything, I did look at the post. As you can see the post is virtually impossible to read from the large and seemingly random indenting and tabbing going on.(notwitstanding Really? ~90% of my response was pseudo-Python code. If you hadn't noticed, Python uses indentation for structural elements -- so of course I'd use indentation. No randomness there. Did you want me to put long SQL statements as single line strings, exceeding common Usenet practice of ~76 characters per line, and letting the lines be wrapped by clients at truly indeterminate points? Other than Google using a very large tab width on the indentation, the post looked perfectly fine to me; no confusing line wraps breaking otherwise syntactically valid lines -- one could cutpaste to a Python file and probably get very few syntax errors (since I wasn't trying to fully working code, just the conceptual pieces). the bombastic verbosity and unbridled quoting). I have seen many posts Out of ~176 lines (as reported by Agent's thread list) I count only 35 lines of quoted material. That is less than 20% of the total line count. That's a great improvement over the many influenced by M$ Outlook, which promotes the unhealthy habits of top-posting and quoting of the entire message chain with no trimming of irrelevant text. -- Wulfraed Dennis Lee Bieber KD6MOG wlfr...@ix.netcom.com HTTP://wlfraed.home.netcom.com/ -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: Nested Menus
in line responses... On Thu, Oct 8, 2009 at 5:58 PM, Dennis Lee Bieber wlfr...@ix.netcom.comwrote: On Thu, 8 Oct 2009 10:38:02 -0500, Victor Subervi victorsube...@gmail.com declaimed the following in gmane.comp.python.general: First suggestion... Get rid of the confusing dat[0] and dat[1] and use names that make sense... You can do this via tuple unpacking in the for statement... for (flda, fldb) in list_of_tuples: Nice! cursor.execute( select Category from categories order by Parent, ID) Categories = [itm[0] for itm in cursor] #untuple single column cursor.execute( select Parent from categories order by Parent, ID) Parents = [itm[0] for itm in cursor] The above will create the equivalent of your primary two lists -- while also keeping them linked positionally. Also nice! But doesn't the following achieve the same goal even more cleanly? cursor.execute( select Category, Parent from categories order by Parent, ID) Categories, Parents = [itm[0], itm[1] for itm in cursor] MAXLEVEL = 15 cursor.execute(create table if not exists Categories ( ID integer auto_increment primary key, Name varchar(40) not null, unique (Name) )) cursor.execute(create table if not exists Relationship ( ID integer auto_increment primary key, Parent integer not null foreign key (Categories.ID), Child integer not null foreign key (Categories.ID), check (Parent Child) ) ) The MySQL manual states The CHECK clause is parsed but ignored by all storage engines. So, how does this help? # Note that in this schema you # 1) never duplicate NAMES # 2) separate the NAMES from the parentchild linkage def expand(fetched): aDict = {} for (name, ) in fetched: aDict[name] = {} return aDict Does this not simply make an empty dictionary for every item? I see where you call it here: levelDict[nm] = expand(cursor.fetchall()) but don't understand its purpose. def getChildren(levelDict, level = 0): if level MAXLEVEL: return #possibly the data has a cycle/loop for (nm, dt) in levelDict: cursor.execute(select c.name from Categories as c inner join Relationship as r on c.ID = r.Child inner join Categories as p on r.Parent = p.ID where p.Name = %s order by c.name, (nm,) ) levelDict[nm] = expand(cursor.fetchall()) #recursive call to do next level getChildren(levelDict[nm], level + 1) # no data return as we are mutating dictionaries in place nm must refer to name. What is dt? It appears it's never used. What is its purpose? Thanks, Dennis! A really good lesson here! Look forward to learning exactly how to do this. Thanks for your help! V -- http://mail.python.org/mailman/listinfo/python-list
Re: Nested Menus
Hmm. I didn't bother to look at the comparison post. The indenting looks right to me. I reread my post and I believe my question is straight-forward. The crux of the issue is my sentence at the bottom. I believe that details what my objective is. Not sure what I should do here. I hope you can clarify what it is you want me to clarify. V On Fri, Oct 9, 2009 at 2:04 AM, r rt8...@gmail.com wrote: On Oct 8, 5:58 pm, Dennis Lee Bieber wlfr...@ix.netcom.com wrote: (snip: atrocious post formatting!) Dear Dennis, This is not a personal attack on you but your writing style is so horrendous i must at least notify you of it. Go to this link and view your post in GG's and let me know what you think of it. http://groups.google.com/group/comp.lang.python/browse_thread/thread/6301372f8e581a74?hl=en# As you can see the post is virtually impossible to read from the large and seemingly random indenting and tabbing going on.(notwitstanding the bombastic verbosity and unbridled quoting). I have seen many posts like this in the past so maybe you are just oblivious...? Are you writing like this on purpose or is your newsreader bugging out? Sincerely, concerned Pythonista -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: Nested Menus
On Fri, Oct 9, 2009 at 7:13 AM, Victor Subervi victorsube...@gmail.comwrote: Hmm. I didn't bother to look at the comparison post. The indenting looks right to me. I reread my post and I believe my question is straight-forward. The crux of the issue is my sentence at the bottom. I believe that details what my objective is. Not sure what I should do here. I hope you can clarify what it is you want me to clarify. He was complaining not about your post but a rather extended response Dennis made to you while trying to help. Which is a particularly asshatish thing to do, but hey. Dennis's response isn't really my style of using SQL in Python, but its perfectly legible and understandable -- both in GMail where I usually read, and via the link r provided to prove his point, which it doesn't at all. It looks fine in both. Either way, I basically do it just the same way Dennis did-- so did that solve your question? A little bit of table redesign and then recursive calls between the items and relationships tables. --S -- http://mail.python.org/mailman/listinfo/python-list
Re: Nested Menus
Well, as sometimes happens, the response to Dennis' response caught my attention (out of context) and I didn't notice Dennis' response! Thanks for bringing it to my attention. I will look at it tonight, and follow-up tomorrow after I've had a chance to digest it and work with it. (And thank you Dennis!) V On Fri, Oct 9, 2009 at 11:13 AM, Stephen Hansen apt.shan...@gmail.comwrote: On Fri, Oct 9, 2009 at 7:13 AM, Victor Subervi victorsube...@gmail.comwrote: Hmm. I didn't bother to look at the comparison post. The indenting looks right to me. I reread my post and I believe my question is straight-forward. The crux of the issue is my sentence at the bottom. I believe that details what my objective is. Not sure what I should do here. I hope you can clarify what it is you want me to clarify. He was complaining not about your post but a rather extended response Dennis made to you while trying to help. Which is a particularly asshatish thing to do, but hey. Dennis's response isn't really my style of using SQL in Python, but its perfectly legible and understandable -- both in GMail where I usually read, and via the link r provided to prove his point, which it doesn't at all. It looks fine in both. Either way, I basically do it just the same way Dennis did-- so did that solve your question? A little bit of table redesign and then recursive calls between the items and relationships tables. --S -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Nested Menus
Hi; I have the following code: sql = 'create table if not exists categories (ID int(3) unsigned primary key, Category varchar(40), Parent varchar(40))' cursor.execute(sql) cursor.execute('select Category, Parent from categories;') data = cursor.fetchall() parents = [] Parents = [] for dat in data: if dat[1] not in parents: parents.append(dat[1]) Categories.append(dat[0]) Parents.append(dat[1]) Parents.sort() families = [] for parent in Parents: children = [] for dat in data: if dat[0] == parent: children.append(dat[1]) families.append([parent, children]) Now, what I want to do is to capture all nested categories, such that if x is the parent of y who is the parent of z, etc., I can print out the entire family tree. I'm at a loss as to how to do it! Please advise. TIA, Victor -- http://mail.python.org/mailman/listinfo/python-list