timeit.py examples in docs generate error

3

3c273

Hello,
I was reading the thread on try/except overhead and decided to try Alex's
examples but they kept generating exceptions. So I went to the docs and
tried the examples there (copied and pasted) with the same results (Win2k,
Python 2.4). Any help is appreciated. The output follows:
#1
C:\Python24\Lib>timeit.py 'if hasattr(int, "__nonzero__"): pass'
Traceback (most recent call last):
File "C:\Python24\Lib\timeit.py", line 285, in ?
sys.exit(main())
File "C:\Python24\Lib\timeit.py", line 249, in main
t = Timer(stmt, setup, timer)
File "C:\Python24\Lib\timeit.py", line 116, in __init__
code = compile(src, dummy_src_name, "exec")
File "<timeit-src>", line 6
'if
^
SyntaxError: EOL while scanning single-quoted string

#2
C:\Python24\Lib>timeit.py 'try:' ' str.__nonzero__' 'except
AttributeError:' '
pass'
Traceback (most recent call last):
File "C:\Python24\Lib\timeit.py", line 285, in ?
sys.exit(main())
File "C:\Python24\Lib\timeit.py", line 249, in main
t = Timer(stmt, setup, timer)
File "C:\Python24\Lib\timeit.py", line 116, in __init__
code = compile(src, dummy_src_name, "exec")
File "<timeit-src>", line 7
'
^
SyntaxError: EOL while scanning single-quoted string

Thanks again,
Louis
 
D

Duncan Booth

3c273 said:
I was reading the thread on try/except overhead and decided to try
Alex's examples but they kept generating exceptions. So I went to the
docs and tried the examples there (copied and pasted) with the same
results (Win2k, Python 2.4). Any help is appreciated. The output
follows: #1
C:\Python24\Lib>timeit.py 'if hasattr(int, "__nonzero__"): pass'
Traceback (most recent call last):
File "C:\Python24\Lib\timeit.py", line 285, in ?
sys.exit(main())
File "C:\Python24\Lib\timeit.py", line 249, in main
t = Timer(stmt, setup, timer)
File "C:\Python24\Lib\timeit.py", line 116, in __init__
code = compile(src, dummy_src_name, "exec")
File "<timeit-src>", line 6
'if
^
SyntaxError: EOL while scanning single-quoted string

Window's command processor doesn't recognise single quote marks as
meaning anything special, so your command is passing 4 separate arguments
to timeit.py instead of the 1 argument that a unix shell would be passing.

Try using double quotes around everything instead:

timeit.py "if hasattr(int, '__nonzero__'): pass"
 
3

3c273

Duncan Booth said:
Window's command processor doesn't recognise single quote marks as
meaning anything special, so your command is passing 4 separate arguments
to timeit.py instead of the 1 argument that a unix shell would be passing.

Try using double quotes around everything instead:

timeit.py "if hasattr(int, '__nonzero__'): pass"
That did the trick. Thanks!
Louis
 

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,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top