[Python-ideas] time.Timer

anatoly techtonik techtonik at gmail.com
Wed Feb 26 06:24:10 CET 2014


UX improvement fix for the common case:

import time

class Timer(object):
  def __init__(self, seconds):
    self.seconds = seconds
    self.restart()

  def restart(self):
    self.end = time.time() + self.seconds

  @property
  def expired(self):
    return (time.time() > self.end)

Example:

class FPS(object):
  def __init__(self):
    self.counter = 0
    self.timer = Timer(1)
  def process(self):
    self.counter += 1
    if self.timer.expired:
      print "FPS: %s" % self.counter
      self.counter = 0
      self.timer.restart()

All code above is in public domain.
-- 
anatoly t.


More information about the Python-ideas mailing list