Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > TypeError: int argument required

Reply
Thread Tools

TypeError: int argument required

 
 
lucius
Guest
Posts: n/a
 
      06-12-2009
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;strokeink;stroke-width:5;fill-opacity:0.1;stroke-
opacity:0.9\"/> " % (absX, absY, w, h)

Thank you for any help.
 
Reply With Quote
 
 
 
 
alex23
Guest
Posts: n/a
 
      06-12-2009
On Jun 12, 1:56*pm, lucius <(E-Mail Removed)> 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...)
 
Reply With Quote
 
 
 
 
Rhodri James
Guest
Posts: n/a
 
      06-12-2009
On Fri, 12 Jun 2009 04:56:24 +0100, lucius <(E-Mail Removed)> 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;strokeink;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
 
Reply With Quote
 
Lawrence D'Oliveiro
Guest
Posts: n/a
 
      06-14-2009
In message <(E-Mail Removed)>, 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.

 
Reply With Quote
 
Rhodri James
Guest
Posts: n/a
 
      06-14-2009
On Sun, 14 Jun 2009 10:43:30 +0100, Lawrence D'Oliveiro
<(E-Mail Removed)_zealand> wrote:

> In message <(E-Mail Removed)>, 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
 
Reply With Quote
 
Lawrence D'Oliveiro
Guest
Posts: n/a
 
      06-15-2009
In message <(E-Mail Removed)>, Rhodri
James wrote:

> On Sun, 14 Jun 2009 10:43:30 +0100, Lawrence D'Oliveiro
> <(E-Mail Removed)_zealand> wrote:
>
>> In message <(E-Mail Removed)>, 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.

 
Reply With Quote
 
Rhodri James
Guest
Posts: n/a
 
      06-15-2009
On Mon, 15 Jun 2009 01:33:50 +0100, Lawrence D'Oliveiro
<(E-Mail Removed)_zealand> wrote:

> In message <(E-Mail Removed)>, Rhodri
> James wrote:
>
>> On Sun, 14 Jun 2009 10:43:30 +0100, Lawrence D'Oliveiro
>> <(E-Mail Removed)_zealand> wrote:
>>
>>> In message <(E-Mail Removed)>,
>>> 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
 
Reply With Quote
 
Bearophile
Guest
Posts: n/a
 
      06-15-2009
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;strokeink;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;strokeink;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; strokeink; 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
 
Reply With Quote
 
Lawrence D'Oliveiro
Guest
Posts: n/a
 
      06-16-2009
In message <(E-Mail Removed)>, Rhodri
James wrote:

> On Mon, 15 Jun 2009 01:33:50 +0100, Lawrence D'Oliveiro
> <(E-Mail Removed)_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.

 
Reply With Quote
 
Lie Ryan
Guest
Posts: n/a
 
      06-16-2009
Lawrence D'Oliveiro wrote:
> In message <(E-Mail Removed)>, Rhodri
> James wrote:
>
>> On Mon, 15 Jun 2009 01:33:50 +0100, Lawrence D'Oliveiro
>> <(E-Mail Removed)_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*/
'''
)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Newbie help (TypeError: int argument required) Iain Adams Python 2 06-09-2008 05:46 AM
Difference between int i, j; and int i; int j; arun C Programming 8 07-31-2006 05:11 AM
int a[10]; int* p=(int*)((&a)+1); But why p isn't equal to ((&a)+1)? aling C++ 8 10-20-2005 02:42 PM
int main(int argc, char *argv[] ) vs int main(int argc, char **argv ) Hal Styli C Programming 14 01-20-2004 10:00 PM
dirty stuff: f(int,int) cast to f(struct{int,int}) Schnoffos C Programming 2 06-27-2003 03:13 AM



Advertisments