From: Jean-Michel Pichavant on 7 Dec 2009 07:14 Victor Subervi wrote: > > global printTree = <function printTree>, allTrees = [{'prodCat1': {}, > 'prodCat2': {}}, {'presCat1': {}, 'presCat2': {}}] > /var/www/html/angrynates.com/cart/catTree.py > <http://angrynates.com/cart/catTree.py> in > printTree(allTrees=[{'prodCat1': {}, 'prodCat2': {}}, {'presCat1': {}, > 'presCat2': {}}], level=0) > 12 for name in sorted(aTree.keys()): > 13 print '\t' * level, name > 14 tree.append("%s%s") % ("\t" * level, name) > 15 printTree(aTree[name], level + 1) > 16 > tree = ['%s%s'], tree.append = <built-in method append of list > object>, level = 0, name = 'prodCat1' > > TypeError: unsupported operand type(s) for %: 'NoneType' and 'tuple' > args = ("unsupported operand type(s) for %: 'NoneType' and > 'tuple'",) > > But according to the same error, level = 0 [the NoneType, I presume] > and name = 'prodCat1', which is most certainly not a tuple! Why the error? > TIA, > Victor Come on Victor, Given the error, you can easily see that tree.append("%s%s") % ("\t" * level, name) is involved in the error you get. The correct thing may be tree.append("%s%s" % ("\t" * level, name)) It should be easy for you to spot. FYI, tree.append('%s%s') returns None, then ("\t" * level, name) is the tuple applied to None through the % operator. That is why you get the above error. Cheers, JM
From: Jean-Michel Pichavant on 7 Dec 2009 09:36 Victor Subervi wrote: > On Mon, Dec 7, 2009 at 7:14 AM, Jean-Michel Pichavant > <jeanmichel(a)sequans.com <mailto:jeanmichel(a)sequans.com>> wrote: > > Victor Subervi wrote: > > > global printTree = <function printTree>, allTrees = > [{'prodCat1': {}, 'prodCat2': {}}, {'presCat1': {}, > 'presCat2': {}}] > /var/www/html/angrynates.com/cart/catTree.py > <http://angrynates.com/cart/catTree.py> > <http://angrynates.com/cart/catTree.py> in > printTree(allTrees=[{'prodCat1': {}, 'prodCat2': {}}, > {'presCat1': {}, 'presCat2': {}}], level=0) > > 12 for name in sorted(aTree.keys()): > 13 print '\t' * level, name > 14 tree.append("%s%s") % ("\t" * level, name) > 15 printTree(aTree[name], level + 1) > 16 > tree = ['%s%s'], tree.append = <built-in method append of list > object>, level = 0, name = 'prodCat1' > > TypeError: unsupported operand type(s) for %: 'NoneType' and > 'tuple' > args = ("unsupported operand type(s) for %: 'NoneType' > and 'tuple'",) > > But according to the same error, level = 0 [the NoneType, I > presume] and name = 'prodCat1', which is most certainly not a > tuple! Why the error? > TIA, > Victor > > > Come on Victor, > > Given the error, you can easily see that > > tree.append("%s%s") % ("\t" * level, name) > is involved in the error you get. > > The correct thing may be > tree.append("%s%s" % ("\t" * level, name)) > > It should be easy for you to spot. > FYI, tree.append('%s%s') returns None, then ("\t" * level, name) > is the tuple applied to None through the % operator. That is why > you get the above error. > > > You're absolutely right. This code was supplied to me by a lister. Had > I written it myself, I would have caught it. Reading his code, I > didn't. I will train myself from now on to write out the code when I > come across such errors in the future (and re-write my own as well) so > that I can spot such errors: > > >>> level = 0 > >>> name = 'one' > >>> tree = [] > >>> tree.append("%s%s" % ("\t" * level, name)) > >>> > > However, the code threw one of those blasted HTTP 500 errors and the > log had this terse comment: > > [Mon Dec 07 06:01:23 2009] [error] [client 208.84.198.58] Premature > end of script headers: catTree.py > > Thank you. How helpful. Here's the code: > > def printTree(allTrees, level=0): > tree = [] > for aTree in allTrees: > for name in sorted(aTree.keys()): > tree.append("%s%s" % ("\t" * level, name)) > printTree(aTree[name], level + 1) > > The problem must be occurring in the last line which creates a loop > since printTree is called only once at the very end of the entire > script (where it is returned to the referrer), but it seems perfectly > logical to me. Please advise. > V Is allTrees a dict or a list ? Did you try to define what could be a allTree value and test your code step by step in a python shell ? That's what is missing in your example: an allTrees values for us to execute your code and check what is going wrong. JM
From: Jean-Michel Pichavant on 7 Dec 2009 10:19 Victor Subervi wrote: > > > printTree(aTree[name], level + 1) > > > ... print aTree([name], level + 1) > ... > Traceback (most recent call last): > File "<stdin>", line 4, in ? > TypeError: 'dict' object is not callable > >>> > Be cautious, you are now executing the same code ! Again, read carefully the error message, you tried to call a dictionary : aTree([name], level + 1) the following code below works fine in a python 2.5 shell: allTrees = [{'prodCat1': {}, 'prodCat2': {}}, {'presCat1': {}, 'presCat2': {}}] level = 0 tree=[] def printTree(allTrees, level=0): for aTree in allTrees: for name in sorted(aTree.keys()): print '\t' * level, name tree.append("%s%s" % ("\t" * level, name)) printTree(aTree[name], level + 1) In [9]: run ~/test.py In [10]: printTree(allTrees) prodCat1 prodCat2 presCat1 presCat2 In [11]: print tree ['prodCat1', 'prodCat2', 'presCat1', 'presCat2'] Though I'm not sure this is exactly want you want. Anyway, there is a big probelm happening when using the following value : allTrees = [{'prodCat1': {'test':'success'}, 'prodCat2': {}}, {'presCat1': {}, 'presCat2': {}}] printTree take a list in the first place, but when recursively calling printTree(aTree[name], level + 1), you pass a dictionary. Your code will fail as soon as the dictionary is not empty. JM
From: Carsten Haese on 7 Dec 2009 10:29 Victor Subervi wrote: > I'll do my best to remember to do that from > now on to: > >>>> allTrees = [{'prodCat1': {}, 'prodCat2': {}}, {'presCat1': {}, > 'presCat2': {}}] >>>> level = 0 >>>> tree = [] >>>> for aTree in allTrees: > ... for name in sorted(aTree.keys()): > ... tree.append("%s%s" % ("\t" * level, name)) > ... print aTree([name], level + 1) > ... > Traceback (most recent call last): > File "<stdin>", line 4, in ? > TypeError: 'dict' object is not callable >>>> > > So It would seem I need to either figure a way to coerce the dicts into > being tuples a la: > http://code.activestate.com/recipes/361668/ > (which looks like a lot of work) or I need to supply tuples instead of > dicts. No. You need to test the actual code you want to test. The code you typed manually has some very significant differences from the code you first posted. For example, one uses <<printTree(aTree[name], level + 1)>>, and the other uses <<print aTree([name], level + 1)>>. The conclusions you are drawing from this test are meaningless guesses that have nothing to do with solving your actual problem. > The dicts come from here: > > cursor.execute('select category from categories%s order by Category;' % > (store[0].upper() + store[1:])) > theTree = expand(cursor.fetchall()) > > which is the magical code supplied by the lister that does all the heavy > lifting but that I don't understand :-} > Suggestions? Start by understanding the code you're using. -- Carsten Haese http://informixdb.sourceforge.net
From: Rami Chowdhury on 7 Dec 2009 11:40 On Mon, Dec 7, 2009 at 08:01, Victor Subervi <victorsubervi(a)gmail.com> wrote: > On Mon, Dec 7, 2009 at 11:29 AM, Carsten Haese <carsten.haese(a)gmail.com> > wrote: >> >> Victor Subervi wrote: >> > I'll do my best to remember to do that from >> > now on to: >> > >> >>>> allTrees = [{'prodCat1': {}, 'prodCat2': {}}, {'presCat1': {}, >> > 'presCat2': {}}] >> >>>> level = 0 >> >>>> tree = [] >> >>>> for aTree in allTrees: >> > ... Â for name in sorted(aTree.keys()): >> > ... Â Â tree.append("%s%s" % ("\t" * level, name)) >> > ... Â Â print aTree([name], level + 1) >> > ... >> > Traceback (most recent call last): >> > Â File "<stdin>", line 4, in ? >> > TypeError: 'dict' object is not callable >> >>>> >> > >> > So It would seem I need to either figure a way to coerce the dicts into >> > being tuples a la: >> > http://code.activestate.com/recipes/361668/ >> > (which looks like a lot of work) or I need to supply tuples instead of >> > dicts. >> >> No. You need to test the actual code you want to test. The code you >> typed manually has some very significant differences from the code you >> first posted. For example, one uses <<printTree(aTree[name], level + >> 1)>>, and the other uses <<print aTree([name], level + 1)>>. The >> conclusions you are drawing from this test are meaningless guesses that >> have nothing to do with solving your actual problem. > > Another screw-up. Now that I'm at a computer where I can right click to > paste the correctly copied code, it executed in the shell just fine. For > whatever reason, the page itself no longer throws an error, although it's > still not working properly. >> >> > The dicts come from here: >> > >> > cursor.execute('select category from categories%s order by Category;' % >> > (store[0].upper() + store[1:])) >> > theTree = expand(cursor.fetchall()) >> > >> > which is the magical code supplied by the lister that does all the heavy >> > lifting but that I don't understand :-} >> > Suggestions? >> >> Start by understanding the code you're using. > > Well, if you could point me in the right direction, it would be appreciated. > I've tried googling this with no luck. Apparently, "expand" is not a > well-documented term in python and, of course, it's an often-used term in > English, which further confuses the issue. Yes, I would like to understand > this code. Coming from PHP, I can see why you might be confused. Python is not PHP -- Python has namespaces and uses them, unlike PHP which IIRC shoves nearly everything into the default namespace. So you need to start by figuring out where the 'expand' function came from -- I'm pretty sure it's not a builtin. Look at the import statements in the file to see if the function has been specifically imported from somewhere, or if there's a statement of the form "from [module] import *". You will then know the module it came from, and be able to either look at the code or the documentation for that module, which should tell you more. -------- Rami Chowdhury "Never assume malice when stupidity will suffice." -- Hanlon's Razor 408-597-7068 (US) / 07875-841-046 (UK) / 0189-245544 (BD)
|
Next
|
Last
Pages: 1 2 Prev: Float precision and float equality Next: How do I Block Events in wxPython |