Skip to content

Instantly share code, notes, and snippets.

@void4
Created April 1, 2017 22:04
Show Gist options
  • Save void4/3004e9e08f88dd7b2f060db081b09396 to your computer and use it in GitHub Desktop.
Save void4/3004e9e08f88dd7b2f060db081b09396 to your computer and use it in GitHub Desktop.
Python, Time, Interval, __bool__, __nonzero__
# Object which is true once after <interval> seconds
# For Python2, use the __nonzero__ method
class Every:
def __init__(self, interval):
self.interval = interval
self.lasttime = time()
def __bool__(self):
current = time()
if current-self.lasttime>=self.interval:
self.lasttime = current
return True
return False
every = Every(1)
while True:
if every:
print("One second passed")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment