how do i map this?

R

ronrsr

I think I am passing the wrong argument to the Print_row routine:



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@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>
"""


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]

#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);
zhtml.print_row(record)

zhtml.print_end_table()
zhtml.print_end_html()






how do i go about mapping that, if tha'ts what I need to do.
thanks again for your help.

bests,

-rsr-
 
B

Ben Finney

ronrsr said:
#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>
"""

You're printing a string, and never using that 'row' parameter.

Perhaps this will help illustrate:
spam Wortle eggs

The 'print' statement doesn't care about the format specifiers; it
just wants a string expression. It's the formatting operator, '%',
that looks for them.

<URL:http://docs.python.org/lib/typesseq-strings.html>
 
J

John Machin

Ben said:
You're printing a string, and never using that 'row' parameter.

If that is so, why is he getting that message "TypeError: format
requires a mapping"?
 
G

Gabriel G

Ben said:
ronrsr said:
#row is a dictionary with keys: zid, keywords, citation, quotation
def print_row(row):
print """<tr>
[...]
"""

You're printing a string, and never using that 'row' parameter.

If that is so, why is he getting that message "TypeError: format
requires a mapping"?

Apparently the posted code doesn't match the actual code. If that
print_row() matched the stack trace, should say % row at the end.
Perhaps there is another function print_row?
Or you cut something from the end?


--
Gabriel Genellina
Softlab SRL

__________________________________________________
Correo Yahoo!
Espacio para todos tus mensajes, antivirus y antispam ¡gratis!
¡Abrí tu cuenta ya! - http://correo.yahoo.com.ar
 
J

John Machin

ronrsr said:
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@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
 
B

Ben Finney

John Machin said:
If that is so, why is he getting that message "TypeError: format
requires a mapping"?

No idea. Probably because what the poster showed us is not code that
shows the problem described.

Original poster (and everyone who wants help with their program
behaving unexpectedly): please ensure that you post a complete,
minimal example of code that exhibits the unexpected behaviour.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,222
Latest member
patricajohnson51

Latest Threads

Top