how do i map this?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ronrsr

    #1

    how do i map this?

    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@upsilo n%



    routine from zhtml.py:

    #row is a dictionary with keys: zid, keywords, citation, quotation
    def print_row(row):
    print """<tr>
    <td class="pad">%(k eywords)s
    </td>
    <td class="pad">%(q uotation)s
    </td>
    <td class="pad">%(c itation)s
    </td>
    <td class="pad" align="center"> <form action="update. py"
    name="updatefor m" enctype="applic ation/x-www-form-urlencoded"
    method="GET"><i nput 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(querys tring).dictresu lt()

    #HTML

    import zhtml
    zhtml.print_sta rt_html()
    zhtml.print_hea der("Frankie's Zingers")

    if form.has_key("p rintview"):
    print ("xxxxthenxxxxx x")
    zhtml.printp_st art_table()
    zhtml.printp_ti tle_row()
    for record in result:
    zhtml.printp_ro w(record[row])
    else:
    # print ("xxxxxelsexxxx x",record[1]);
    zhtml.print_top _controls(k=k,c =c,q=q)
    zhtml.print_sta rt_table()
    zhtml.print_tit le_row()
    zhtml.print_sea rch_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-

  • Ben Finney

    #2
    Re: how do i map this?

    "ronrsr" <ronrsr@gmail.c omwrites:
    #row is a dictionary with keys: zid, keywords, citation, quotation
    def print_row(row):
    print """<tr>
    <td class="pad">%(k eywords)s
    </td>
    <td class="pad">%(q uotation)s
    </td>
    <td class="pad">%(c itation)s
    </td>
    <td class="pad" align="center"> <form action="update. py"
    name="updatefor m" enctype="applic ation/x-www-form-urlencoded"
    method="GET"><i nput 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:
    >>print "spam %(foo)s eggs"
    spam %(foo)s eggs
    >>print "spam %(foo)s eggs" % {'foo': "Wortle"}
    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>

    --
    \ "When I was little, my grandfather used to make me stand in a |
    `\ closet for five minutes without moving. He said it was elevator |
    _o__) practice." -- Steven Wright |
    Ben Finney

    Comment

    • John Machin

      #3
      Re: how do i map this?


      Ben Finney wrote:
      "ronrsr" <ronrsr@gmail.c omwrites:
      >
      #row is a dictionary with keys: zid, keywords, citation, quotation
      def print_row(row):
      print """<tr>
      <td class="pad">%(k eywords)s
      </td>
      <td class="pad">%(q uotation)s
      </td>
      <td class="pad">%(c itation)s
      </td>
      <td class="pad" align="center"> <form action="update. py"
      name="updatefor m" enctype="applic ation/x-www-form-urlencoded"
      method="GET"><i nput 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.
      If that is so, why is he getting that message "TypeError: format
      requires a mapping"?

      Comment

      • Gabriel G

        #4
        Re: how do i map this?

        At Monday 13/11/2006 01:55, John Machin wrote:
        >Ben Finney wrote:
        "ronrsr" <ronrsr@gmail.c omwrites:
        #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

        Comment

        • John Machin

          #5
          Re: how do i map this?

          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@upsilo n%
          >
          >
          >
          routine from zhtml.py:
          >
          #row is a dictionary with keys: zid, keywords, citation, quotation
          def print_row(row):
          print """<tr>
          <td class="pad">%(k eywords)s
          </td>
          <td class="pad">%(q uotation)s
          </td>
          <td class="pad">%(c itation)s
          </td>
          <td class="pad" align="center"> <form action="update. py"
          name="updatefor m" enctype="applic ation/x-www-form-urlencoded"
          method="GET"><i nput 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(querys tring).dictresu lt()
          >
          #HTML
          >
          import zhtml
          zhtml.print_sta rt_html()
          zhtml.print_hea der("Frankie's Zingers")
          >
          if form.has_key("p rintview"):
          print ("xxxxthenxxxxx x")
          zhtml.printp_st art_table()
          zhtml.printp_ti tle_row()
          for record in result:
          zhtml.printp_ro w(record[row])
          else:
          # print ("xxxxxelsexxxx x",record[1]);
          zhtml.print_top _controls(k=k,c =c,q=q)
          zhtml.print_sta rt_table()
          zhtml.print_tit le_row()
          zhtml.print_sea rch_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".spli t(), (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

          Comment

          • Ben Finney

            #6
            Re: how do i map this?

            "John Machin" <sjmachin@lexic on.netwrites:
            Ben Finney wrote:
            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"?
            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.

            --
            \ "Room service? Send up a larger room." -- Groucho Marx |
            `\ |
            _o__) |
            Ben Finney

            Comment

            Working...