advice on this little script

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Benjamin Liebald

    #16
    Re: advice on this little script

    John Salerno wrote:[color=blue][color=green]
    >> for x in range(minutes,0 ,-1):
    >> sleep(60.0)
    >> print minutes, 'minutes remaining'
    >>[/color]
    >
    > I might be doing something wrong, but this just keeps printing out '10
    > minutes remaining' each time.[/color]

    should be:

    for x in range(minutes,0 ,-1):
    sleep(60.0)
    print x, 'minutes remaining'

    Ben

    Comment

    • John Salerno

      #17
      Re: advice on this little script

      James Stroud wrote:[color=blue]
      > John Salerno wrote:[color=green]
      >> Grant Edwards wrote:
      >>[color=darkred]
      >>> On 2006-03-09, John Salerno <johnjsal@NOSPA Mgmail.com> wrote:
      >>>
      >>>> from time import sleep
      >>>>
      >>>> minutes = input('Enter the number of minutes to wait: ')
      >>>>
      >>>> for x in range(minutes):
      >>>> sleep(1.0)
      >>>> minutes -= 1
      >>>> print minutes, 'minutes remaining.'
      >>>
      >>> for x in range(minutes,0 ,-1):
      >>> sleep(60.0)
      >>> print minutes, 'minutes remaining'
      >>>[/color]
      >> I might be doing something wrong, but this just keeps printing out '10
      >> minutes remaining' each time.[/color]
      >
      > print x, 'minutes remaining'
      >
      >
      >[/color]
      Ah! Thanks to both of you!

      Comment

      • Paul Rubin

        #18
        Re: advice on this little script

        John Salerno <johnjsal@NOSPA Mgmail.com> writes:[color=blue][color=green]
        > > for x in range(minutes,0 ,-1):
        > > sleep(60.0)
        > > print minutes, 'minutes remaining'
        > >[/color]
        > Nice! Cross off another line! I feel like Hemingway. :)[/color]

        Besides the bug mentioned, I don't think you should really do it that
        way, since sleep(60.0) might not sleep for exactly 60 sec (it could be
        longer or shorter). Preferable is something like (untested):

        now = time.time()
        sleep_until = now + 60*minutes
        while now < sleep_until:
        print int((now - sleep_until)/60), 'minutes remaining'
        sleep (60)
        now = time.time()

        You might actually want to stop sleeping a little bit early (say if
        you wake up 5 seconds before sleep_until), or round the message to the
        nearest number of minutes, etc.

        Comment

        • Alex Martelli

          #19
          Re: advice on this little script

          Grant Edwards <grante@visi.co m> wrote:
          ...[color=blue][color=green]
          > > Nice! Cross off another line! I feel like Hemingway. :)[/color]
          >
          > Was he the one who once apologized to his editor for a story
          > being so long because he was in a hurry and didn't have time to
          > make it shorter?[/color]

          Nope, that immortal quote is by Blaise Pascal (also known as a
          philosopher, a mathematician, and the developer of one of the best early
          mechanical calculators, as well as for the programming language Niklaus
          Wirth named after him, and the SI standard unit of pressure, 1 Pa == 1 N
          per square meter, also named in his honor because he was the first to
          use a barometer to measure altitude); it's found in his "Lettres
          Provinciales" (which is not a collection of "real" letters, but rather a
          philosophical, theological and political polemic cast in epistular
          form). It's often misattributed to Mark Twain, but this the first time
          I've heard it associated with Hemingway!



          Alex

          Comment

          • Alex Martelli

            #20
            Re: advice on this little script

            John Salerno <johnjsal@NOSPA Mgmail.com> wrote:
            ...[color=blue]
            > I think the 'from time import sleep' looks cleaner, because I'm only
            > taking what I need (is an import any more expensive than this from?),
            > but I also feel like the 'time.sleep' syntax is much more
            > self-describing and better to read than just 'sleep'.
            >
            > So what do you guys think between these two choices?[/color]

            I only use the 'from' statement to import specific modules from a
            package, never to import specific objects (functions, classes, or
            whatever) from a module. It scales much better: when reading a long
            module, I _know_ that any barename X always refers to a local,
            nested-scope, or global name, never to something snatched from
            who-recalls-what module -- in the latter case I'll never see a barename,
            but always somemodule.X, and searching for '... import somemodule' will
            immediately tell me where somemodule was coming from (should I need to
            be reminded of that information).


            Alex

            Comment

            • Steve Holden

              #21
              Re: advice on this little script

              Paul Rubin wrote:[color=blue]
              > John Salerno <johnjsal@NOSPA Mgmail.com> writes:
              >[color=green][color=darkred]
              >>>for x in range(minutes,0 ,-1):
              >>> sleep(60.0)
              >>> print minutes, 'minutes remaining'
              >>>[/color]
              >>
              >>Nice! Cross off another line! I feel like Hemingway. :)[/color]
              >
              >
              > Besides the bug mentioned, I don't think you should really do it that
              > way, since sleep(60.0) might not sleep for exactly 60 sec (it could be
              > longer or shorter). Preferable is something like (untested):
              >
              > now = time.time()
              > sleep_until = now + 60*minutes
              > while now < sleep_until:
              > print int((now - sleep_until)/60), 'minutes remaining'
              > sleep (60)
              > now = time.time()
              >
              > You might actually want to stop sleeping a little bit early (say if
              > you wake up 5 seconds before sleep_until), or round the message to the
              > nearest number of minutes, etc.[/color]

              You might also want to synchronize to a caesium clock, but the guy is
              timing his laundry, for Pete's sake! Can we agree your approach, while
              theoretically sound, might be a little over-complicated for a first
              application?

              regards
              Steve
              --
              Steve Holden +44 150 684 7255 +1 800 494 3119
              Holden Web LLC/Ltd www.holdenweb.com
              Love me, love my blog holdenweb.blogs pot.com

              Comment

              • Kent Johnson

                #22
                Re: advice on this little script

                Alex Martelli wrote:[color=blue]
                > John Salerno <johnjsal@NOSPA Mgmail.com> wrote:
                > ...
                >[color=green]
                >>I think the 'from time import sleep' looks cleaner, because I'm only
                >>taking what I need (is an import any more expensive than this from?),
                >>but I also feel like the 'time.sleep' syntax is much more
                >>self-describing and better to read than just 'sleep'.
                >>
                >>So what do you guys think between these two choices?[/color][/color]

                I use both fairly interchangeably . When I just need one or two things
                from the module, or if I am going to use something a lot, I use from xx
                import yy.[color=blue]
                >
                >
                > I only use the 'from' statement to import specific modules from a
                > package, never to import specific objects (functions, classes, or
                > whatever) from a module. It scales much better: when reading a long
                > module, I _know_ that any barename X always refers to a local,
                > nested-scope, or global name, never to something snatched from
                > who-recalls-what module -- in the latter case I'll never see a barename,
                > but always somemodule.X, and searching for '... import somemodule' will
                > immediately tell me where somemodule was coming from (should I need to
                > be reminded of that information).[/color]

                If you use from xx import yy, searching for yy will show you its
                provenance as well.

                From xx import * is evil because it does hide the provenance of names.
                It can also give unexpected results - if xx contains from zz import *,
                then all of the names from zz will also be imported into the module
                importing xx!

                Kent

                Comment

                • Grant Edwards

                  #23
                  Re: advice on this little script

                  On 2006-03-09, John Salerno <johnjsal@NOSPA Mgmail.com> wrote:[color=blue]
                  > Grant Edwards wrote:[color=green]
                  >> On 2006-03-09, John Salerno <johnjsal@NOSPA Mgmail.com> wrote:
                  >>[color=darkred]
                  >>> from time import sleep
                  >>>
                  >>> minutes = input('Enter the number of minutes to wait: ')
                  >>>
                  >>> for x in range(minutes):
                  >>> sleep(1.0)
                  >>> minutes -= 1
                  >>> print minutes, 'minutes remaining.'[/color]
                  >>
                  >> for x in range(minutes,0 ,-1):
                  >> sleep(60.0)
                  >> print minutes, 'minutes remaining'[/color][/color]

                  print x, 'minutes remaining'

                  Doh!

                  --
                  Grant Edwards grante Yow! Now KEN is having
                  at a MENTAL CRISIS beacuse
                  visi.com his "R.V." PAYMENTS are
                  OVER-DUE!!

                  Comment

                  • John Salerno

                    #24
                    Re: advice on this little script

                    Steve Holden wrote:
                    [color=blue]
                    > You might also want to synchronize to a caesium clock, but the guy is
                    > timing his laundry, for Pete's sake! Can we agree your approach, while
                    > theoretically sound, might be a little over-complicated for a first
                    > application?[/color]

                    LOL. Thanks, I was about to fall out of my chair! :)

                    Comment

                    • John Salerno

                      #25
                      Re: advice on this little script

                      Alex Martelli wrote:
                      [color=blue]
                      > I only use the 'from' statement to import specific modules from a
                      > package, never to import specific objects (functions, classes, or
                      > whatever) from a module.[/color]

                      I like that. So in my case I'd use 'import time' (which I actually
                      already changed last night). I think especially right now, while I'm new
                      to Python, it helps me to see 'time.sleep' instead of just 'sleep', so I
                      can begin to associate certain functions with their proper modules.

                      Not to mention, like you said, that importing specific pieces like
                      functions tends to look a little messy in the code when they aren't
                      qualified.

                      Comment

                      • Alex Martelli

                        #26
                        Re: advice on this little script

                        Kent Johnson <kent@kentsjohn son.com> wrote:
                        ...[color=blue]
                        > If you use from xx import yy, searching for yy will show you its
                        > provenance as well.[/color]

                        But when seeing the barename yy, it gives no clue whether the module
                        you're reading used 'from xx import yy' or defined yy in any other way
                        (localy, globally, or in any intermediate scope).

                        'from' has many other disadvantages, such as working badly with reload
                        _except_ when used specifically to import a module.


                        Alex

                        Comment

                        • Alex Martelli

                          #27
                          Re: advice on this little script

                          John Salerno <johnjsal@NOSPA Mgmail.com> wrote:
                          [color=blue]
                          > Alex Martelli wrote:
                          >[color=green]
                          > > I only use the 'from' statement to import specific modules from a
                          > > package, never to import specific objects (functions, classes, or
                          > > whatever) from a module.[/color]
                          >
                          > I like that. So in my case I'd use 'import time' (which I actually
                          > already changed last night). I think especially right now, while I'm new
                          > to Python, it helps me to see 'time.sleep' instead of just 'sleep', so I
                          > can begin to associate certain functions with their proper modules.[/color]

                          I'm hardly new to Python, yet it keeps helping me too;-).

                          [color=blue]
                          > Not to mention, like you said, that importing specific pieces like
                          > functions tends to look a little messy in the code when they aren't
                          > qualified.[/color]

                          I wouldn't call it messy, but rather potentially "confusing" in a large
                          module with many imports -- in the middle of the module,

                          a = b(c)

                          always gives the strong impression that b and cc are local or global (or
                          from an intermediate lexical scope), while

                          a = X.b(Y.c)

                          contains hints suggesting otherwise (if I had no clue what X and Y were
                          at this point, I'd _first_ check if they were imported modules, while it
                          would be my last resort in the former snippet regarding b and c).


                          Alex

                          Comment

                          • John Salerno

                            #28
                            Re: advice on this little script

                            Alex Martelli wrote:
                            [color=blue]
                            > I'm hardly new to Python, yet it keeps helping me too;-).[/color]

                            Yeah, from what I hear and read, you know a thing or two about the
                            language. :)

                            Comment

                            • BartlebyScrivener

                              #29
                              Re: advice on this little script

                              There are several of these writing quotes, all good in their own way,
                              because they emphasize concision as the first order of business for any
                              writer.

                              "If I had more time, I would write a short letter."--Blaise Pascal

                              "If the author had been less industrious, this book would be twice as
                              long."--Evelyn Waugh

                              Telegram from Mark Twain's publisher: "NEED 2-PAGE SHORT STORY IN TWO
                              DAYS." Twain replied: "NO CAN DO 2 PAGES TWO DAYS. CAN DO 30 PAGES 2
                              DAYS. NEED 30 DAYS TO DO 2 PAGES."

                              rpd
                              Critically-acclaimed novelist Richard Dooling is the author of five novels, along with two nonfiction works.


                              Comment

                              • BartlebyScrivener

                                #30
                                Re: advice on this little script

                                >>You might also want to synchronize to a caesium clock, but the guy is
                                timing his laundry, <<

                                My morning coffee just streamed out of my nose. Air. I need air.

                                rpd

                                Comment

                                Working...