TypeError: int argument required

L

lucius

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.
 
A

alex23

 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...)
 
R

Rhodri James

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.
 
L

Lawrence D'Oliveiro

Rhodri said:
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.
 
R

Rhodri James

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.
 
L

Lawrence D'Oliveiro

Rhodri said:
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.
 
R

Rhodri James

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.
 
B

Bearophile

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
 
L

Lawrence D'Oliveiro

Rhodri said:
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.
 
L

Lie Ryan

Lawrence said:
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 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*/
'''
)
 
L

Lawrence D'Oliveiro

Lie Ryan said:
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.
 
R

Rhodri James

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

[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.
 
L

Lawrence D'Oliveiro

Rhodri said:
On Wed, 17 Jun 2009 12:07:15 +0100, Lawrence D'Oliveiro

[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 said:
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"?
 
R

Rhodri James

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.
 
L

Lie Ryan

Rhodri said:
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.
 

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top