timeit.py examples in docs generate error

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • 3c273

    #1

    timeit.py examples in docs generate error

    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\Li b\timeit.py", line 285, in ?
    sys.exit(main() )
    File "C:\Python24\Li b\timeit.py", line 249, in main
    t = Timer(stmt, setup, timer)
    File "C:\Python24\Li b\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\Li b\timeit.py", line 285, in ?
    sys.exit(main() )
    File "C:\Python24\Li b\timeit.py", line 249, in main
    t = Timer(stmt, setup, timer)
    File "C:\Python24\Li b\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


  • Duncan Booth

    #2
    Re: timeit.py examples in docs generate error

    3c273 wrote:
    [color=blue]
    > 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\Li b\timeit.py", line 285, in ?
    > sys.exit(main() )
    > File "C:\Python24\Li b\timeit.py", line 249, in main
    > t = Timer(stmt, setup, timer)
    > File "C:\Python24\Li b\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[/color]

    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"

    Comment

    • 3c273

      #3
      Re: timeit.py examples in docs generate error

      "Duncan Booth" <duncan.booth@i nvalid.invalid> wrote in message
      news:Xns9781C41 BAEF48duncanboo th@127.0.0.1...[color=blue]
      > 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"[/color]
      That did the trick. Thanks!
      Louis


      Comment

      Working...