how do i map this?

John Machin sjmachin at lexicon.net
Mon Nov 13 00:37:59 EST 2006


ronrsr wrote:
> I think I am passing the wrong argument to the Print_row routine:

Well, yes,  that is what the error message is telling you.
It is also what Fredrik Lundh told you only a couple of hours ago.
He also told you what to do to fix it, at a high level.
Below is my attempt to explain at a lower level.
If you don't understand, please *reply* to my message; *don't* start a
new topic as though it were a totally fresh problem.

Now read on ...

>
>
>
> Traceback (most recent call last):
>   File "index.py", line 91, in ?
>     zhtml.print_row(record)
>   File "/usr/www/users/homebase/realprogress/zingers/zhtml.py", line
> 154, in pri
> nt_row
>     print """<tr>
> TypeError: format requires a mapping
> homebase at upsilon%
>
>
>
> routine from zhtml.py:
>
> #row is a dictionary with keys: zid, keywords, citation, quotation
> def print_row(row):
>    print """<tr>
>       <td class="pad">%(keywords)s
>       </td>
>       <td class="pad">%(quotation)s
>       </td>
>       <td class="pad">%(citation)s
>       </td>
>       <td class="pad" align="center"><form action="update.py"
> name="updateform" enctype="application/x-www-form-urlencoded"
> method="GET"><input type="hidden" name="zid" value="%(zid)d"><input
> type="submit" value="Edit"></form>
>       </td>
>       </tr>
>       """

This cannot be the code that was executed -- it would need to be
followed by
    % row
for it to produce the error message that you received.

>
>
> code from index.py:
>
>
> cursor.execute(querystring);
> # get the resultset as a tuple
> result = cursor.fetchall()
> # iterate through resultset
> for record in result:
>       print record[0] , "-->", record[1]

"record" looks like a tuple, not a dictionary.

>
> #zq = zc.query(querystring).dictresult()
>
> #HTML
>
> import zhtml
> zhtml.print_start_html()
> zhtml.print_header("Frankie's Zingers")
>
> if form.has_key("printview"):
>    print ("xxxxthenxxxxxx")
>    zhtml.printp_start_table()
>    zhtml.printp_title_row()
>    for record in result:
>       zhtml.printp_row(record[row])
> else:
> #   print ("xxxxxelsexxxxx",record[1]);
>    zhtml.print_top_controls(k=k,c=c,q=q)
>    zhtml.print_start_table()
>    zhtml.print_title_row()
>    zhtml.print_search_row(k=k,c=c,q=q)
>    for record in result:
>
>
>       print(record);

print is a statement, not a function; you meant
    print record
Instead of that, do this
    print type(record)
    print repr(record)

You will find that "record" is a tuple, not a dictionary
Without changing the code in the zhtml module, you need to make a
dictionary, for example (untested):

adict = {}
for key, inx in zip("zid keywords citation quotation".split(), (0, 1,
2, 3)):
    adict[key] = record[inx]
zhtml.print_row(adict)
where (0, 1, 2, 3) are the positions of the 4 fields in record --
change the numbers as necessary to match reality.

Alternatively (sketch):

(1) unpack the tuple e.g.
 zid, keywords, citation, quotation = record
# Add names for any other fields that exist.
# Change the order as necessary to match reality.
(2) call a changed zhtml routine:
zhtml.print_row(zid, keywords, citation, quotation)
(3) re-jig the zhtml.print_row routine
 def print_row(zid, keywords, citation, quotation):
    print """<tr>
       <td class="pad">%s
       </td>
       <td class="pad">%s
etc etc
    """ % (keywords, quotation, citation, zid)

>       zhtml.print_row(record)
> 
> zhtml.print_end_table()
> zhtml.print_end_html()

HTH,
John




More information about the Python-list mailing list