timeit's environment

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • rurpy@yahoo.com

    #1

    timeit's environment

    Why doesn't the following work? It generates a "NameError: global
    name 'data' is not defined" error.

    import timeit

    global data
    data = [3,8,4,8,6,0,5,7 ,2,1]

    env = "global data; x = data"

    print timeit.Timer('x .sort()', env).timeit()
    print timeit.Timer('x .sort(cmp=cmp', env).timeit()

    How can I get timeit() to see an external (to it) variable?
    (In the real program 'data' is very expensive to create and
    contains non-reproducable data and the two timeit calls
    must be run on identical objects.

  • Alex Martelli

    #2
    Re: timeit's environment

    <rurpy@yahoo.co m> wrote:
    [color=blue]
    > Why doesn't the following work? It generates a "NameError: global
    > name 'data' is not defined" error.
    >
    > import timeit
    >
    > global data
    > data = [3,8,4,8,6,0,5,7 ,2,1]
    >
    > env = "global data; x = data"
    >
    > print timeit.Timer('x .sort()', env).timeit()
    > print timeit.Timer('x .sort(cmp=cmp', env).timeit()
    >
    > How can I get timeit() to see an external (to it) variable?
    > (In the real program 'data' is very expensive to create and
    > contains non-reproducable data and the two timeit calls
    > must be run on identical objects.[/color]

    You have to use 'from __main__ import data as x' rather than just say
    'global data; x=data', because timeit is a separate module from your
    __main__ one.


    Alex

    Comment

    • rurpy@yahoo.com

      #3
      Re: timeit's environment


      Alex Martelli wrote:[color=blue]
      > <rurpy@yahoo.co m> wrote:
      >[color=green]
      > > Why doesn't the following work? It generates a "NameError: global
      > > name 'data' is not defined" error.
      > >
      > > import timeit
      > >
      > > global data
      > > data = [3,8,4,8,6,0,5,7 ,2,1]
      > >
      > > env = "global data; x = data"
      > >
      > > print timeit.Timer('x .sort()', env).timeit()
      > > print timeit.Timer('x .sort(cmp=cmp', env).timeit()
      > >
      > > How can I get timeit() to see an external (to it) variable?
      > > (In the real program 'data' is very expensive to create and
      > > contains non-reproducable data and the two timeit calls
      > > must be run on identical objects.[/color]
      >
      > You have to use 'from __main__ import data as x' rather than just say
      > 'global data; x=data', because timeit is a separate module from your
      > __main__ one.[/color]

      Ahh, (slaps forehead) that makes sense. Thank you.

      After posting I looked again at the documentation and at the
      bottom of the example subsection, they also mention using
      import (although without explaining why.)

      Since I've been bitching about documentation in another
      thread, I'm curious... Would it be obvious to anyone of
      low to intermediate python skills that using global would
      not work in this case? Would it be obvious that using an
      import is the answer? Or can I blame this partially on the
      documentation? :-) I think the scoping issue could have
      at least been mentioned in the Timer class or timeit method
      descriptions. There is no mention there of exactly what
      environment the code is run in.

      Comment

      • Scott David Daniels

        #4
        Re: timeit's environment

        rurpy@yahoo.com wrote:
        [color=blue]
        > Since I've been bitching about documentation in another
        > thread, I'm curious... Would it be obvious to anyone of
        > low to intermediate python skills that using global would
        > not work in this case? Would it be obvious that using an
        > import is the answer? Or can I blame this partially on the
        > documentation? :-) I think the scoping issue could have
        > at least been mentioned in the Timer class or timeit method
        > descriptions. There is no mention there of exactly what
        > environment the code is run in.[/color]

        Perhaps you could write a paragraph or two that would have
        informed you and send it to the destination mentioned on the
        documentation page.


        --
        -Scott David Daniels
        scott.daniels@a cm.org

        Comment

        • rurpy@yahoo.com

          #5
          Re: timeit's environment


          Scott David Daniels wrote:[color=blue]
          > rurpy@yahoo.com wrote:
          >[color=green]
          > > Since I've been bitching about documentation in another
          > > thread, I'm curious... Would it be obvious to anyone of
          > > low to intermediate python skills that using global would
          > > not work in this case? Would it be obvious that using an
          > > import is the answer? Or can I blame this partially on the
          > > documentation? :-) I think the scoping issue could have
          > > at least been mentioned in the Timer class or timeit method
          > > descriptions. There is no mention there of exactly what
          > > environment the code is run in.[/color]
          >
          > Perhaps you could write a paragraph or two that would have
          > informed you and send it to the destination mentioned on the
          > documentation page.[/color]

          Yes, but I was hoping to get some sense of how such
          a submission might be viewed prior to going through
          the work of doing it.

          Comment

          • Scott David Daniels

            #6
            Re: timeit's environment

            rurpy@yahoo.com wrote:[color=blue]
            > Scott David Daniels wrote:[/color]
            ....[color=blue][color=green]
            >> Perhaps you could write a paragraph or two that would have
            >> informed you and send it to the destination mentioned on the
            >> documentation page.[/color]
            >
            > Yes, but I was hoping to get some sense of how such
            > a submission might be viewed prior to going through
            > the work of doing it.
            >[/color]
            I assure you that if it does seem to ease the pain we'd love to
            include it. A newbie knows what's confusing about the intro parts.
            Once you've been around a while, you no longer even read those
            intro things, and so don't know where they suffer. "It's to hard
            for me to understand" is a content-free complaint that is too often
            heard, but "if you said, 'In this instance be sure to ...' in paragraph
            three ..." is a clear explanation of what is wrong and how it might be
            fixed that will definitely be read.

            --
            -Scott David Daniels
            scott.daniels@a cm.org

            Comment

            Working...