Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > str() and repr() question

Reply
Thread Tools

str() and repr() question

 
 
adima
Guest
Posts: n/a
 
      04-26-2007
Hi All!
Sorry, my English isnt good, but get a try to describe my problem.

Today we wrote next script:

import os, glob, time, string
files_to_test = ( "J:\\BWNEW\\!Unerase\\test.test", "L:\\Temp\Nick\
\test.test", "F:\\TRANSIT\\nick\\test.test")
outfile="c:\\temp\\statistic"


def DoTestTime(file):
StartTime = time.time()
filehandle = open(file)
alllines = filehandle.read()
filehandle.close()

FileSize = os.path.getsize(file)
EndTime = time.time()
return time.ctime() , "\tDisk\t" , file[:3] , "\tfile size (bytes) =
\t" , FileSize , "\taccess time =\t" , EndTime - StartTime


if __name__ == "__main__":
out = open(outfile, 'w')
for EachFile in files_to_test:
str = DoTestTime(EachFile)
print type(str)
for s in str:
print s
out.write(str(s))
out.close()

When I executed it, the output was
C:\test\py>File_Stat.py
Thu Apr 26 12:08:33 2007
<type 'str'>
Traceback (most recent call last):
File "C:\test\py\File_Stat.py", line 26, in ?
out.write(str(s))
TypeError: 'tuple' object is not callable

When I replace "str" with "repr" in out.write(str(s)), script executed
normally.

Where is the problem here?
Thanks in advance.

 
Reply With Quote
 
 
 
 
=?ISO-8859-15?Q?Thomas_Kr=FCger?=
Guest
Posts: n/a
 
      04-26-2007
adima schrieb:
> str = DoTestTime(EachFile)

^^^
> print type(str)
> for s in str:
> print s
> out.write(str(s))

^^^

> Where is the problem here?
> Thanks in advance.
>


You have overwritten the built-in str function some lines above.

Thomas

--
sinature: http://nospam.nowire.org/signature_usenet.png
 
Reply With Quote
 
 
 
 
Mark T
Guest
Posts: n/a
 
      04-26-2007

"adima" <> wrote in message
news: oups.com...
> Hi All!
> Sorry, my English isnt good, but get a try to describe my problem.
>
> Today we wrote next script:
>
> import os, glob, time, string
> files_to_test = ( "J:\\BWNEW\\!Unerase\\test.test", "L:\\Temp\Nick\
> \test.test", "F:\\TRANSIT\\nick\\test.test")
> outfile="c:\\temp\\statistic"
>
>
> def DoTestTime(file):
> StartTime = time.time()
> filehandle = open(file)
> alllines = filehandle.read()
> filehandle.close()
>
> FileSize = os.path.getsize(file)
> EndTime = time.time()
> return time.ctime() , "\tDisk\t" , file[:3] , "\tfile size (bytes) =
> \t" , FileSize , "\taccess time =\t" , EndTime - StartTime
>
>
> if __name__ == "__main__":
> out = open(outfile, 'w')
> for EachFile in files_to_test:
> str = DoTestTime(EachFile)
> print type(str)
> for s in str:
> print s
> out.write(str(s))
> out.close()
>
> When I executed it, the output was
> C:\test\py>File_Stat.py
> Thu Apr 26 12:08:33 2007
> <type 'str'>
> Traceback (most recent call last):
> File "C:\test\py\File_Stat.py", line 26, in ?
> out.write(str(s))
> TypeError: 'tuple' object is not callable
>
> When I replace "str" with "repr" in out.write(str(s)), script executed
> normally.
>
> Where is the problem here?
> Thanks in advance.
>


You might want to check out pychecker.py. Its output:

Processing Script1...

Warnings...

Script1.py:1: Imported module (glob) not used
Script1.py:1: Imported module (string) not used
Script1.py:9: Local variable (alllines) not used
Script1.py:20: (str) shadows builtin

You've replaced the built-in function str with the return value of
DoTestTime().

-Mark

 
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
question about .h file and .cpp file,also compiler question key9 C++ 7 09-13-2006 06:45 PM
Thanks (and apologies) for your comments on "The Most Obvious Question You Are Ever Likely To See" (e.g. No-Brainer security question) Curious George Computer Security 1 03-14-2005 10:01 PM
Re: General USB Printer setup question, and situation-specific question AG A+ Certification 0 01-13-2005 11:13 PM
Three question which is not yet answered clearly and correct so far !! challenging question in xsl and also in xsl fo Philip Meyer XML 0 11-30-2003 04:42 PM
newbie delurking, a lighting question, and a pricing question verminiusrex Digital Photography 2 08-08-2003 10:10 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57