<div class="gmail_quote">On Mon, Dec 7, 2009 at 9:36 AM, Jean-Michel Pichavant <span dir="ltr"><<a href="mailto:jeanmichel@sequans.com">jeanmichel@sequans.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;">
Victor Subervi wrote:<div><div></div><div class="h5"><br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
On Mon, Dec 7, 2009 at 7:14 AM, Jean-Michel Pichavant <<a href="mailto:jeanmichel@sequans.com" target="_blank">jeanmichel@sequans.com</a> <mailto:<a href="mailto:jeanmichel@sequans.com" target="_blank">jeanmichel@sequans.com</a>>> wrote:<br>
<br>
Victor Subervi wrote:<br>
<br>
<br>
global printTree = <function printTree>, allTrees =<br>
[{'prodCat1': {}, 'prodCat2': {}}, {'presCat1': {},<br>
'presCat2': {}}]<br>
/var/www/html/<a href="http://angrynates.com/cart/catTree.py" target="_blank">angrynates.com/cart/catTree.py</a><br>
<<a href="http://angrynates.com/cart/catTree.py" target="_blank">http://angrynates.com/cart/catTree.py</a>><br>
<<a href="http://angrynates.com/cart/catTree.py" target="_blank">http://angrynates.com/cart/catTree.py</a>> in<br>
printTree(allTrees=[{'prodCat1': {}, 'prodCat2': {}},<br>
{'presCat1': {}, 'presCat2': {}}], level=0)<br>
<br>
12 for name in sorted(aTree.keys()):<br>
13 print '\t' * level, name<br>
14 tree.append("%s%s") % ("\t" * level, name)<br>
15 printTree(aTree[name], level + 1)<br>
16<br>
tree = ['%s%s'], tree.append = <built-in method append of list<br>
object>, level = 0, name = 'prodCat1'<br>
<br>
TypeError: unsupported operand type(s) for %: 'NoneType' and<br>
'tuple'<br>
args = ("unsupported operand type(s) for %: 'NoneType'<br>
and 'tuple'",)<br>
<br>
But according to the same error, level = 0 [the NoneType, I<br>
presume] and name = 'prodCat1', which is most certainly not a<br>
tuple! Why the error?<br>
TIA,<br>
Victor<br>
<br>
<br>
Come on Victor,<br>
<br>
Given the error, you can easily see that<br>
<br>
tree.append("%s%s") % ("\t" * level, name)<br>
is involved in the error you get.<br>
<br>
The correct thing may be<br>
tree.append("%s%s" % ("\t" * level, name))<br>
<br>
It should be easy for you to spot.<br>
FYI, tree.append('%s%s') returns None, then ("\t" * level, name)<br>
is the tuple applied to None through the % operator. That is why<br>
you get the above error.<br>
<br>
<br>
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:<br>
<br>
>>> level = 0<br>
>>> name = 'one'<br>
>>> tree = []<br>
>>> tree.append("%s%s" % ("\t" * level, name))<br>
>>><br>
<br>
However, the code threw one of those blasted HTTP 500 errors and the log had this terse comment:<br>
<br>
[Mon Dec 07 06:01:23 2009] [error] [client 208.84.198.58] Premature end of script headers: catTree.py<br>
<br>
Thank you. How helpful. Here's the code:<br>
<br>
def printTree(allTrees, level=0):<br>
tree = []<br>
for aTree in allTrees:<br>
for name in sorted(aTree.keys()):<br>
tree.append("%s%s" % ("\t" * level, name))<br>
printTree(aTree[name], level + 1)<br>
<br>
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.<br>
V<br>
</blockquote></div></div>
Is allTrees a dict or a list ?<br>
Did you try to define what could be a allTree value and test your code step by step in a python shell ?<br>
That's what is missing in your example: an allTrees values for us to execute your code and check what is going wrong.<br></blockquote><div><br>That was a great suggestion. I'll do my best to remember to do that from now on to:<br>
<br>>>> allTrees = [{'prodCat1': {}, 'prodCat2': {}}, {'presCat1': {}, 'presCat2': {}}]<br>>>> level = 0<br>>>> tree = []<br>>>> for aTree in allTrees:<br>
... for name in sorted(aTree.keys()):<br>... tree.append("%s%s" % ("\t" * level, name))<br>... print aTree([name], level + 1)<br>...<br>Traceback (most recent call last):<br> File "<stdin>", line 4, in ?<br>
TypeError: 'dict' object is not callable<br>>>><br><br>So It would seem I need to either figure a way to coerce the dicts into being tuples a la:<br><a href="http://code.activestate.com/recipes/361668/">http://code.activestate.com/recipes/361668/</a><br>
(which looks like a lot of work) or I need to supply tuples instead of dicts. The dicts come from here:<br><br>cursor.execute('select category from categories%s order by Category;' % (store[0].upper() + store[1:]))<br>
theTree = expand(cursor.fetchall())<br><br>which is the magical code supplied by the lister that does all the heavy lifting but that I don't understand :-}<br>Suggestions?<br>TIA,<br>V<br></div></div>