[Tutor] How to enable pausing my stop_watch.py ?

Alan Gauld alan.gauld at blueyonder.co.uk
Wed Jul 7 21:04:52 CEST 2004


> I would like to enable pausing and then resuming stop_watch.py. For
> example, say I've set it to beep after 30 minutes. I start it, but
then
> discontinue the activity I'm timing. I want to pause the stopwatch
by
> pressing a key and then resume later by pressing (another?) key,
just as
> I could do with a real stopwatch. Is this possible?

Yes, you might want to look at the getch() function.

Windows has it in msvcrt module
Linux in the curses module

An example in my event driven programming topic in my tutor

> And I'd also really appreciate a close critique of the code I have
so far.
>

Not very close but...

> # stop_watch.py
> import time, winsound
>
> def hms(seconds):
>      """Convert seconds to tuplet (hours, minutes, seconds)"""
>      import time

Buy convention move import outside the function, otherwise you
waste a little time importing it every time the function is
called and although Python does smart imports it still uses
a little bit of effort...

>      hours, minutes = 0, 0
>
>      if seconds >= 60 and seconds < 3600:

       if 60 <= seconds < 3600:    # python trickery...

>          minutes = divmod(seconds,60)[0]
>          seconds = divmod(seconds,60)[1]

           minutes, seconds = divmod(seconds,60)


>      elif seconds >= 3600:

       else:   # since if outside the range above it must be else...

>          hours = divmod(seconds,3600)[0]
>          seconds = divmod(seconds,3600)[1]

           hours,seconds = divmod(seconds,3600)

>          minutes = divmod(seconds,60)[0]
>          seconds = divmod(seconds,60)[1]
>      return hours, minutes, seconds # returns a tuple, e.g. (1, 40,
8.5)

       return hours, divmod(seconds,60)  # h, m, s


Cuts it to 6 lines...

> hours = raw_input("Enter the number of hours to time ")
> if hours == "":
>      h_seconds = 0
> else:
>      h_seconds = 3600 * int(hours)

  h_seconds = (hours and 3600*int(hours)) or 0  # shorter but more
obscure

Works like this:
Only evaluate 3600*hours if hours NOT ""
If result is False(ie hours was "") then evaluate
second part of OR - ie 0

> minutes = raw_input("And the number of minutes ")
> if minutes == "":
>      m_seconds = 0
> else:
>      m_seconds = 60 * int(minutes)

Same trick as above

> seconds = raw_input("And the number of seconds ")
> t0 = time.time()
> if seconds == "":
>      seconds = 0
> seconds = h_seconds + m_seconds + int(seconds)
> print "Seconds total is", seconds
>
> t = hms(seconds)  # delete this line!!!!!
> print "Beep will sound after %d hours %d minutes %d seconds\n"  \
>        % (t[0], t[1], t[2])

replace line immediately above with
         % hms(seconds)

> r = 10 # get report of time-passed and time-left every r seconds
> k = r
> while True:
>      t1 = time.time()
>      seconds_passed = t1 - t0
>      seconds_left = seconds - int(seconds_passed)
>
>      if seconds_passed >= seconds:
>          break
>
>      if seconds_passed > k:
>          p = hms(seconds_passed)  ## delete these two lines
>          l = hms(seconds_left)
>          print "%d hours %d minutes %d seconds have passed" \
>                % (p[0], p[1], p[2])

                 % hms(seconds_passed)

>          print "%d hours %d minutes %d seconds are left\n" \
>                % (l[0], l[1], l[2])

                 % hms(seconds_left)

>          k += r

           k += 10    # not sure what value r adds?

The loop will repeat very very rapidly and use a lot of CPU.
Try inserting a time.sleep() call to cut CPU activity. Try
a value of, say 0.25. Add your call to getch() here too.
Set a guard condition (called paused?) and put an if statement
in front of the t1 = ... line above.

> print "seconds elapsed:", (t1 - t0) # as a check on accuracy
> print "TIME'S UP!"
>
> # winsound.Beep(frequency, duration in milliseconds) - only for
Windows
> winsound.Beep(500,5)
> """

HTH,

Alan G
Author of the Learn to Program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld/tutor2/



More information about the Tutor mailing list