Skip to content

Instantly share code, notes, and snippets.

@notalentgeek
Last active December 12, 2016 16:55
Show Gist options
  • Save notalentgeek/34bfc6595db55e36debbf82ee702350b to your computer and use it in GitHub Desktop.
Save notalentgeek/34bfc6595db55e36debbf82ee702350b to your computer and use it in GitHub Desktop.
Python class to help to execute code for every second passed.
import datetime as dt
# This is a class to detect change in second.
# My first initial idea was to make a codes
# those execute for every second passed.
# The method here is to take the current time
# from datetime.datetime.now().time(), extract
# the second and then execute something when
# the value changes (most of the time codes
# will be executed for every one second).
class TimerSecondChange(object):
def __init__(self):
self.currSec = 0 # current second.
self.storSec = None # previously stored second.
self.chngSec = False # if there is change in second.
def Update(self):
# Always check current second.
self.currSec = int(dt.datetime.now().time().strftime("%S"))
# Compare the current second with
# previously stored second.
self.chngSec = True if self.currSec != self.storSec else False
if self.chngSec: self.storSec = self.currSec
def main():
tSt = TimerSecondChange()
while True:
tSt.Update()
if tSt.chngSec: print("One second has just passed.")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment