TypeError: int argument required

Discussion in 'Python' started by lucius, Jun 12, 2009.

  1. lucius

    lucius Guest

    I am trying to
    print some values to a file (using c's printf like method).
    TypeError: int argument required
    # this works, i see value on screen
    print w, h, absX, absY

    # where result is the return value of my regular expression.

    w, h, absX, absY = result.group(3), result.group(4), result.group
    (5), result.group(6)

    w = 100
    h = 200

    absX = 10.0
    absY = 20.0

    # this fails, I get "TypeError: int argument required"
    print >> fo, "<rect x=\"%f\" y=\"%f\" width=\"%d\" height=\"%d\"
    style=\"fill:blue;stroke:pink;stroke-width:5;fill-opacity:0.1;stroke-
    opacity:0.9\"/> " % (absX, absY, w, h)

    Thank you for any help.
     
    lucius, Jun 12, 2009
    #1
    1. Advertising

  2. lucius

    alex23 Guest

    On Jun 12, 1:56 pm, lucius <> wrote:
    >  w, h, absX, absY = result.group(3), result.group(4), result.group
    > (5), result.group(6)
    >
    > w = 100
    > h = 200
    >
    > absX = 10.0
    > absY = 20.0


    Are you sure those values are ints & floats? I would expect your
    regexp would be returning strings...

    Try replacing the %f & %d strsubs with %s and see if that works. (You
    shouldn't need to typecast the values if you're just reinserting them
    into a string...)
     
    alex23, Jun 12, 2009
    #2
    1. Advertising

  3. lucius

    Rhodri James Guest

    On Fri, 12 Jun 2009 04:56:24 +0100, lucius <> wrote:

    > I am trying to
    > print some values to a file (using c's printf like method).
    > TypeError: int argument required
    > # this works, i see value on screen
    > print w, h, absX, absY
    >
    > # where result is the return value of my regular expression.
    >
    > w, h, absX, absY = result.group(3), result.group(4), result.group
    > (5), result.group(6)
    >
    > w = 100
    > h = 200
    >
    > absX = 10.0
    > absY = 20.0
    >
    > # this fails, I get "TypeError: int argument required"
    > print >> fo, "<rect x=\"%f\" y=\"%f\" width=\"%d\" height=\"%d\"
    > style=\"fill:blue;stroke:pink;stroke-width:5;fill-opacity:0.1;stroke-
    > opacity:0.9\"/> " % (absX, absY, w, h)
    >
    > Thank you for any help.


    1. This has to be the most incoherent help request that I've seen that
    included actual information. Figuring out what you were actually doing
    was quite a challenge.

    2. That output string has severe "leaning toothpick" syndrome. Python
    accepts both single and double quotes to help avoid creating something
    so unreadable: use them.

    3. matchobject.group(n) returns a string, not an int or float.

    --
    Rhodri James *-* Wildebeest Herder to the Masses
     
    Rhodri James, Jun 12, 2009
    #3
  4. In message <>, Rhodri
    James wrote:

    > 2. That output string has severe "leaning toothpick" syndrome. Python
    > accepts both single and double quotes to help avoid creating something
    > so unreadable: use them.


    Backslashes are more scalable.
     
    Lawrence D'Oliveiro, Jun 14, 2009
    #4
  5. lucius

    Rhodri James Guest

    On Sun, 14 Jun 2009 10:43:30 +0100, Lawrence D'Oliveiro
    <_zealand> wrote:

    > In message <>, Rhodri
    > James wrote:
    >
    >> 2. That output string has severe "leaning toothpick" syndrome. Python
    >> accepts both single and double quotes to help avoid creating something
    >> so unreadable: use them.

    >
    > Backslashes are more scalable.


    Yes, yes, you can print them at any size. That doesn't excuse sprinkling
    several million backslashes through literal constants when there's a more
    readable alternative.

    --
    Rhodri James *-* Wildebeest Herder to the Masses
     
    Rhodri James, Jun 14, 2009
    #5
  6. In message <>, Rhodri
    James wrote:

    > On Sun, 14 Jun 2009 10:43:30 +0100, Lawrence D'Oliveiro
    > <_zealand> wrote:
    >
    >> In message <>, Rhodri
    >> James wrote:
    >>
    >>> 2. That output string has severe "leaning toothpick" syndrome. Python
    >>> accepts both single and double quotes to help avoid creating something
    >>> so unreadable: use them.

    >>
    >> Backslashes are more scalable.

    >
    > That doesn't excuse sprinkling several million backslashes through literal
    > constants when there's a more readable alternative.


    Perl allows just about any printable character as a quote. I tried
    alternative quotes for many years, and decided making that choice was a
    waste of brain cells.

    So no, using alternative quotes does not make things more readable.
     
    Lawrence D'Oliveiro, Jun 15, 2009
    #6
  7. lucius

    Rhodri James Guest

    On Mon, 15 Jun 2009 01:33:50 +0100, Lawrence D'Oliveiro
    <_zealand> wrote:

    > In message <>, Rhodri
    > James wrote:
    >
    >> On Sun, 14 Jun 2009 10:43:30 +0100, Lawrence D'Oliveiro
    >> <_zealand> wrote:
    >>
    >>> In message <>,
    >>> Rhodri
    >>> James wrote:
    >>>
    >>>> 2. That output string has severe "leaning toothpick" syndrome.
    >>>> Python
    >>>> accepts both single and double quotes to help avoid creating something
    >>>> so unreadable: use them.
    >>>
    >>> Backslashes are more scalable.

    >>
    >> That doesn't excuse sprinkling several million backslashes through
    >> literal
    >> constants when there's a more readable alternative.

    >
    > Perl allows just about any printable character as a quote. I tried
    > alternative quotes for many years, and decided making that choice was a
    > waste of brain cells.
    >
    > So no, using alternative quotes does not make things more readable.


    I find it odd that you consider qquoting less scalable than backslashes.
    I also find it odd that you dislike two visuals stutters (at the start
    and end of string) so much that you'll put up with a dozen visual
    stutters in the string to avoid them. Particular since my years of
    Perl-bashing lead me to the opposite conclusion.


    --
    Rhodri James *-* Wildebeest Herder to the Masses
     
    Rhodri James, Jun 15, 2009
    #7
  8. lucius

    Bearophile Guest

    Lawrence D'Oliveiro:
    >So no, using alternative quotes does not make things more readable.<


    You think that this:

    '<rect x="%f" y="%f" width="%d" height="%d"
    style="fill:blue;stroke:pink;stroke-width:5;fill-opacity:0.1;stroke-
    opacity:0.9"/> '

    Isn't a bit more readable and simpler to write than:

    "<rect x=\"%f\" y=\"%f\" width=\"%d\" height=\"%d\"
    style=\"fill:blue;stroke:pink;stroke-width:5;fill-opacity:0.1;stroke-
    opacity:0.9\"/> "

    I think lot of doesn't agree with you.


    In such situation it can also be positive to split such string in two
    or more parts, for example (untested):

    style = ("fill:blue; stroke:pink; stroke-width:5; "
    "fill-opacity:0.1; stroke-opacity:0.9")

    print >> fo, '<rect x="%f" y="%f" width="%d" height="%d" style="%s"/>
    ' % (abs_x, abs_y, w, h, style)

    Bye,
    bearophile
     
    Bearophile, Jun 15, 2009
    #8
  9. In message <>, Rhodri
    James wrote:

    > On Mon, 15 Jun 2009 01:33:50 +0100, Lawrence D'Oliveiro
    > <_zealand> wrote:
    >
    >> Perl allows just about any printable character as a quote. I tried
    >> alternative quotes for many years, and decided making that choice was a
    >> waste of brain cells.
    >>
    >> So no, using alternative quotes does not make things more readable.

    >
    > I find it odd that you consider qquoting less scalable than backslashes.


    Backslashes are scalable because they can be nested to any depth, without
    having to decide beforehand which quotes to use at which level. And yes, I
    do write things like this:

    out.write \
    (
    "function JSString(Str)\n"
    # /* returns a JavaScript string literal that evaluates to Str. */
    " {\n"
    " var Result = \"\\\"\"\n"
    " for (var i = 0; i < Str.length; ++i)\n"
    " {\n"
    " var ThisCh = Str.charAt(i)\n"
    " if (ThisCh == \"\\\\\")\n"
    " {\n"
    " ThisCh = \"\\\\\\\\\"\n"
    " }\n"
    " else if (ThisCh == \"\\\"\")\n"
    " {\n"
    " ThisCh = \"\\\\\\\"\"\n"
    " }\n"
    " else if (ThisCh == \"\\t\")\n"
    " {\n"
    " ThisCh = \"\\\\t\"\n"
    " }\n"
    " else if (ThisCh == \"\\n\")\n"
    " {\n"
    " ThisCh = \"\\\\n\"\n"
    " } /*if*/\n"
    " Result += ThisCh\n"
    " } /*for*/\n"
    " return Result + \"\\\"\"\n"
    "} /*JSString*/\n"
    )

    > I also find it odd that you dislike two visuals stutters (at the start
    > and end of string) so much that you'll put up with a dozen visual
    > stutters in the string to avoid them. Particular since my years of
    > Perl-bashing lead me to the opposite conclusion.


    I find it odd you should think so.
     
    Lawrence D'Oliveiro, Jun 16, 2009
    #9
  10. lucius

    Lie Ryan Guest

    Lawrence D'Oliveiro wrote:
    > In message <>, Rhodri
    > James wrote:
    >
    >> On Mon, 15 Jun 2009 01:33:50 +0100, Lawrence D'Oliveiro
    >> <_zealand> wrote:
    >>
    >>> Perl allows just about any printable character as a quote. I tried
    >>> alternative quotes for many years, and decided making that choice was a
    >>> waste of brain cells.
    >>>
    >>> So no, using alternative quotes does not make things more readable.

    >> I find it odd that you consider qquoting less scalable than backslashes.

    >
    > Backslashes are scalable because they can be nested to any depth, without
    > having to decide beforehand which quotes to use at which level. And yes, I
    > do write things like this:


    Scalable for the computers, not the eye...

    >> I also find it odd that you dislike two visuals stutters (at the start
    >> and end of string) so much that you'll put up with a dozen visual
    >> stutters in the string to avoid them. Particular since my years of
    >> Perl-bashing lead me to the opposite conclusion.

    >
    > I find it odd you should think so.
    >


    If found it odd that you think that is more readable and scalable than this:

    out.write (
    '''
    function JSString(Str)
    {
    var Result = '\"'
    for (var i = 0; i < Str.length; ++i)
    {
    var ThisCh = Str.charAt(i)
    if (ThisCh == '\\')
    {
    ThisCh = '\\\\'
    }
    else if (ThisCh == '\"')
    {
    ThisCh = '\\\"'
    }
    else if (ThisCh == '\t')
    {
    ThisCh = '\\t'
    }
    else if (ThisCh == '\n')
    {
    ThisCh = '\\n'
    } /*if*/
    Result += ThisCh
    } /*for*/
    return Result + '\"'
    } /*JSString*/
    '''
    )

    I might go even further:

    out.write (
    '''
    function JSString(Str)
    {
    const dq = '\"'
    const slash = '\\'

    var Result = dq
    for (var i = 0; i < Str.length; ++i)
    {
    var ThisCh = Str.charAt(i)
    if (ThisCh == slash)
    {
    ThisCh = slash + slash
    }
    else if (ThisCh == dq)
    {
    ThisCh = slash + dq
    }
    else if (ThisCh == '\t')
    {
    ThisCh = slash + 't'
    }
    else if (ThisCh == '\n')
    {
    ThisCh = slash + 'n'
    } /*if*/
    Result += ThisCh
    } /*for*/
    return Result + dq
    } /*JSString*/
    '''
    )
     
    Lie Ryan, Jun 16, 2009
    #10
  11. In message <N4QZl.19093$>, Lie Ryan
    wrote:

    > out.write (
    > '''
    > function JSString(Str)
    > {
    > var Result = '\"'
    > for (var i = 0; i < Str.length; ++i)
    > {
    > var ThisCh = Str.charAt(i)
    > if (ThisCh == '\\')
    > {
    > ThisCh = '\\\\'
    > }
    > else if (ThisCh == '\"')
    > {
    > ThisCh = '\\\"'
    > }
    > else if (ThisCh == '\t')
    > {
    > ThisCh = '\\t'
    > }
    > else if (ThisCh == '\n')
    > {
    > ThisCh = '\\n'
    > } /*if*/
    > Result += ThisCh
    > } /*for*/
    > return Result + '\"'
    > } /*JSString*/
    > '''
    > )


    You haven't managed to get rid of the backslashes.

    > I might go even further:
    >
    > out.write (
    > '''
    > function JSString(Str)
    > {
    > const dq = '\"'
    > const slash = '\\'
    >
    > var Result = dq
    > for (var i = 0; i < Str.length; ++i)
    > {
    > var ThisCh = Str.charAt(i)
    > if (ThisCh == slash)
    > {
    > ThisCh = slash + slash
    > }
    > else if (ThisCh == dq)
    > {
    > ThisCh = slash + dq
    > }
    > else if (ThisCh == '\t')
    > {
    > ThisCh = slash + 't'
    > }
    > else if (ThisCh == '\n')
    > {
    > ThisCh = slash + 'n'
    > } /*if*/
    > Result += ThisCh
    > } /*for*/
    > return Result + dq
    > } /*JSString*/
    > '''
    > )


    Now you've lost track of the original point of the discussion, which is
    about using alternate quotes to avoid backslashes.
     
    Lawrence D'Oliveiro, Jun 17, 2009
    #11
  12. lucius

    Rhodri James Guest

    On Wed, 17 Jun 2009 12:07:15 +0100, Lawrence D'Oliveiro
    <_zealand> wrote:

    [snip example code]

    > You haven't managed to get rid of the backslashes.


    [snip other example code]

    > Now you've lost track of the original point of the discussion, which is
    > about using alternate quotes to avoid backslashes.


    Ah, selective amnesia, how useful you are. The original point of the
    discussion was in fact about using alternative quotes to avoid alternate
    backslashes (or at least excessive ones). The first example showed this
    nicely, actually, within the confines of a language which doesn't give
    you much more help.

    Yes, I know from past conversations that you have a superhuman ability
    to recognise the code in apparent line noise. That still doesn't make
    it legible to anyone else.

    --
    Rhodri James *-* Wildebeest Herder to the Masses
     
    Rhodri James, Jun 18, 2009
    #12
  13. In message <>, Rhodri
    James wrote:

    > On Wed, 17 Jun 2009 12:07:15 +0100, Lawrence D'Oliveiro
    > <_zealand> wrote:
    >
    > [snip example code]
    >
    >> You haven't managed to get rid of the backslashes.

    >
    > [snip other example code]
    >
    >> Now you've lost track of the original point of the discussion, which is
    >> about using alternate quotes to avoid backslashes.

    >
    > Ah, selective amnesia, how useful you are. The original point of the
    > discussion was in fact about using alternative quotes to avoid alternate
    > backslashes (or at least excessive ones).


    No mention of avoiding "alternate backslashes (or at least excessive ones)".
    Here's what I said, in message <h144tf$gip$>:

    > In message <>, Rhodri
    > James wrote:
    >
    >> On Sun, 14 Jun 2009 10:43:30 +0100, Lawrence D'Oliveiro
    >> <_zealand> wrote:
    >>
    >>> In message <>, Rhodri
    >>> James wrote:
    >>>
    >>>> 2. That output string has severe "leaning toothpick" syndrome. Python
    >>>> accepts both single and double quotes to help avoid creating something
    >>>> so unreadable: use them.
    >>>
    >>> Backslashes are more scalable.

    >>
    >> That doesn't excuse sprinkling several million backslashes through
    >> literal constants when there's a more readable alternative.

    >
    > Perl allows just about any printable character as a quote. I tried
    > alternative quotes for many years, and decided making that choice was a
    > waste of brain cells.
    >
    > So no, using alternative quotes does not make things more readable.


    Now compare that with Lie Ryan's examples which, instead of using
    backslashes, instead used alternative quotes plus backslashes in one
    example, and in the other example, alternative quotes, alternatives to
    literal quotes, and backslashes. As opposed to my original routine, which
    managed three levels of quoting using just backslashes. Do you begin to
    understand what I mean by "scalable"?
     
    Lawrence D'Oliveiro, Jun 18, 2009
    #13
  14. lucius

    Rhodri James Guest

    On Thu, 18 Jun 2009 08:29:53 +0100, Lawrence D'Oliveiro
    <_zealand> wrote:

    > Now compare that with Lie Ryan's examples which, instead of using
    > backslashes, instead used alternative quotes plus backslashes in one
    > example, and in the other example, alternative quotes, alternatives to
    > literal quotes, and backslashes. As opposed to my original routine, which
    > managed three levels of quoting using just backslashes. Do you begin to
    > understand what I mean by "scalable"?


    I do, and I still disagree. More importantly, Lie Ryan's examples were
    much more readable, which is what I was complaining about.

    --
    Rhodri James *-* Wildebeest Herder to the Masses
     
    Rhodri James, Jun 18, 2009
    #14
  15. lucius

    Lie Ryan Guest

    Rhodri James wrote:
    > On Thu, 18 Jun 2009 08:29:53 +0100, Lawrence D'Oliveiro
    > <_zealand> wrote:
    >
    >> Now compare that with Lie Ryan's examples which, instead of using
    >> backslashes, instead used alternative quotes plus backslashes in one
    >> example, and in the other example, alternative quotes, alternatives to
    >> literal quotes, and backslashes. As opposed to my original routine, which
    >> managed three levels of quoting using just backslashes. Do you begin to
    >> understand what I mean by "scalable"?

    >
    > I do, and I still disagree. More importantly, Lie Ryan's examples were
    > much more readable, which is what I was complaining about.


    I still remember when I started programming, I wrote in QBASIC without
    indentations. I honestly saw no reason to indent because I still can see
    the program flow as clearly as the bottom of a bucket of crystalline
    water. No decisions to be made, everything is consistently justified left.

    I still remember asking a friend, "Why is your code jagged like that?"
    and him looking at me a bit confused at the question.
     
    Lie Ryan, Jun 18, 2009
    #15
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Schnoffos
    Replies:
    2
    Views:
    1,223
    Martien Verbruggen
    Jun 27, 2003
  2. Hal Styli
    Replies:
    14
    Views:
    1,650
    Old Wolf
    Jan 20, 2004
  3. Iain Adams
    Replies:
    2
    Views:
    373
    bukzor
    Jun 9, 2008
  4. Íéêüëáïò Êïýñáò

    TypeError: iterable argument required

    Íéêüëáïò Êïýñáò, Apr 2, 2011, in forum: Python
    Replies:
    3
    Views:
    310
    Íéêüëáïò Êïýñáò
    Apr 2, 2011
  5. eryksun ()
    Replies:
    3
    Views:
    502
    Íéêüëáïò Êïýñáò
    Apr 4, 2011
Loading...

Share This Page