Fixed negative sleep_time error
This commit is contained in:
parent
94591ec51e
commit
319e6947b1
1 changed files with 16 additions and 11 deletions
|
@ -1,34 +1,36 @@
|
||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
|
|
||||||
class LoopTimer(object):
|
min_sleep_minutes = 0
|
||||||
|
|
||||||
|
class LoopTimer (object):
|
||||||
"""Manages loop times and sleeps until
|
"""Manages loop times and sleeps until
|
||||||
next loop."""
|
next loop."""
|
||||||
def __init__(self, loop_interval, run_on_hour=False):
|
def __init__ (self, loop_interval, run_on_hour = False):
|
||||||
self.interval = int(str(loop_interval))
|
self.interval = int(str(loop_interval))
|
||||||
self.on_hour = run_on_hour
|
self.on_hour = run_on_hour
|
||||||
self.loop_history = []
|
self.loop_history = []
|
||||||
|
|
||||||
def begin_loop(self):
|
def begin_loop (self):
|
||||||
begin_time = datetime.now()
|
begin_time = datetime.now()
|
||||||
print('\n_________Starting new loop___________')
|
print('\n_________Starting new loop___________')
|
||||||
print('Date: ' + begin_time.strftime('%a %d %b %y') + ', time: ' + begin_time.strftime('%H:%M') + '\n')
|
print('Date: ' + begin_time.strftime('%a %d %b %y') + ', time: ' + begin_time.strftime('%H:%M') + '\n')
|
||||||
self.__add_beginning__(begin_time)
|
self.__add_beginning__(begin_time)
|
||||||
|
|
||||||
def __add_beginning__(self, time):
|
def __add_beginning__ (self, time):
|
||||||
self.loop_history.append((time,))
|
self.loop_history.append((time,))
|
||||||
|
|
||||||
def __add_ending__(self, time):
|
def __add_ending__ (self, time):
|
||||||
current = self.get_current()
|
current = self.get_current()
|
||||||
self.loop_history[-1] = (current[0], time)
|
self.loop_history[-1] = (current[0], time)
|
||||||
|
|
||||||
def end_loop(self):
|
def end_loop (self):
|
||||||
end_time = datetime.now()
|
end_time = datetime.now()
|
||||||
self.__add_ending__(end_time)
|
self.__add_ending__(end_time)
|
||||||
|
|
||||||
def get_current(self):
|
def get_current (self):
|
||||||
return self.loop_history[-1]
|
return self.loop_history[-1]
|
||||||
|
|
||||||
def time_until_next(self):
|
def time_until_next (self):
|
||||||
interval_duration = timedelta(minutes=self.interval)
|
interval_duration = timedelta(minutes=self.interval)
|
||||||
loop_duration = self.get_last_duration()
|
loop_duration = self.get_last_duration()
|
||||||
sleep_time = interval_duration - loop_duration
|
sleep_time = interval_duration - loop_duration
|
||||||
|
@ -36,21 +38,24 @@ class LoopTimer(object):
|
||||||
time_until_hour = self.get_time_to_next_hour()
|
time_until_hour = self.get_time_to_next_hour()
|
||||||
if time_until_hour < sleep_time:
|
if time_until_hour < sleep_time:
|
||||||
return time_until_hour
|
return time_until_hour
|
||||||
|
|
||||||
|
if sleep_time < timedelta(0):
|
||||||
|
sleep_time = timedelta(0, 0, 0, 0, min_sleep_minutes)
|
||||||
return sleep_time
|
return sleep_time
|
||||||
|
|
||||||
def get_last_duration(self):
|
def get_last_duration (self):
|
||||||
if len(self.loop_history) == 0:
|
if len(self.loop_history) == 0:
|
||||||
return
|
return
|
||||||
begin, end = self.loop_history[-1]
|
begin, end = self.loop_history[-1]
|
||||||
return end - begin
|
return end - begin
|
||||||
|
|
||||||
def get_time_to_next_hour(self):
|
def get_time_to_next_hour (self):
|
||||||
cur = datetime.now()
|
cur = datetime.now()
|
||||||
rounded = datetime(cur.year, cur.month, cur.day, cur.hour)
|
rounded = datetime(cur.year, cur.month, cur.day, cur.hour)
|
||||||
next_hour_time = rounded + timedelta(hours=1)
|
next_hour_time = rounded + timedelta(hours=1)
|
||||||
return next_hour_time - datetime.now()
|
return next_hour_time - datetime.now()
|
||||||
|
|
||||||
def is_new_hour_loop(self):
|
def is_new_hour_loop (self):
|
||||||
if len(self.loop_history) < 2:
|
if len(self.loop_history) < 2:
|
||||||
return False
|
return False
|
||||||
previous_loop = self.loop_history[-2]
|
previous_loop = self.loop_history[-2]
|
||||||
|
|
Loading…
Reference in a new issue