Python:
''' --------------- ZEROTIMEGATE --------------- '''
import time
import datetime
from datetime import timedelta
''' ZEROTIMEGATE CLASS '''
class ZeroTimeGate:
# a gate that only opens x minutes after it has been set
def __init__(self, minutes) -> None:
self.pause = 1
self.openedGate = datetime.datetime.now()
self.checkPoint = datetime.datetime.now()
try:
self.pause = minutes
try:
time.sleep(0.1)
except Exception as e:
# TODO Auto-generated catch block
# e.printStackTrace()
pass
except:
pass
def isClosed(self) -> bool:
return (self.openedGate < datetime.datetime.now())
def isOpen(self) -> bool:
return (not (self.openedGate < datetime.datetime.now()))
def open(self, minutes: int):
now = datetime.datetime.now()
self.openedGate = now + timedelta(minutes=minutes)
def setPause(self, pause: int):
if 60 > pause > 0:
self.pause = pause
def resetCheckPoint(self):
self.checkPoint = datetime.datetime.now()
def getRunTimeTimeDifInSecondes(self) -> int:
# used to measure code snippets run time
now = datetime.datetime.now()
diff = self.checkPoint - now
return diff.total_seconds()
test :
Python:
# ZeroTimeGate testing
print("\n TESTING ZEROTIMEGATE CLASS")
gate = ZeroTimeGate(1)
print("The isClosed method should return true: ", gate.isClosed())
print("The isOpen method should return false: ", gate.isOpen())
print(gate.openedGate)
gate.open(3) # gate will stay opened for 3 minutes than close
print(gate.openedGate)
print("The isOpen method should return false: ", gate.isOpen())
print("ret true ", gate.isOpen()) # err
gate.checkPoint
print(gate.checkPoint)
print("time dif ?: ",
gate.getRunTimeTimeDifInSecondes())