-
-
Save lighth7015/5dd17bcd3c3bfd429bdc6b7794893877 to your computer and use it in GitHub Desktop.
eligibility.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from circuits.core.timers import Timer | |
from circuits.core import Component, Debugger, handler | |
from datetime import date, timedelta | |
import datetime | |
dt = datetime.datetime | |
class StartupEvent(Event): | |
name = 'startup' | |
class TimerEvent(Event): | |
name = "tick_event" | |
channels = [] | |
def __init__(self, name = "tick_event", *channels): | |
self.channels = channels | |
self.name = "tick:{}".format(name) | |
super(TimerEvent, self).__init__() | |
class Service(Component): | |
"""Eligibility Service""" | |
def seconds(self, date1, date2): | |
timedelta = date2 - date1 | |
return timedelta.days * 24 * 3600 + timedelta.seconds | |
def timeRemaining(self, seconds): | |
minutes, seconds = divmod(seconds, 60) | |
return (minutes if minutes > 0 else 0, seconds if seconds > 0 else 0) | |
@handler( "tick:third.1" ) | |
def handle_hourly(self, *arg, **kwarg): | |
print( "tick." ) | |
self.half1.reset(0) | |
@handler( "tick:third.2" ) | |
def tick_event(self, *arg, **kwarg): | |
print( "tick." ) | |
self.half2.reset(0) | |
@handler( "tick:third.3" ) | |
def tick_event(self, *arg, **kwarg): | |
print( "tick." ) | |
self.half3.reset(0) | |
@property | |
def third_hour1(self): | |
current = dt.now() + timedelta(minutes = 0) | |
return dt( year = current.year, month = current.month, day = current.day, \ | |
hour = current.hour, minute = current.minute, second = 0) | |
@property | |
def third_hour2(self): | |
current = dt.now() + timedelta(minutes = 20) | |
minutes = current.minute if current.minute < 60 else 20 | |
computed = dt( year = current.year, month = current.month, day = current.day, \ | |
hour = current.hour,minute = current.minute + 2, second = 0) | |
print(computed) | |
return computed | |
@property | |
def third_hour3(self): | |
current = dt.now() + timedelta(minutes = 40) | |
return dt( year = current.year, month = current.month, day = current.day, \ | |
hour = current.hour,minute = current.minute - 2, second = 0) | |
@property | |
def next_hour(self): | |
current = dt.now() + timedelta(hours = 1) | |
return dt( year = current.year, month = current.month, day = current.day, \ | |
hour = current.hour + 1, minute = 0, second = 0) | |
def init(self): | |
now = dt.now() | |
EventLog.Info( "Initializing system timers" ) | |
next_hour = self.next_hour | |
[ minutes, seconds ] = self.timeRemaining(self.seconds(now, next_hour)) | |
print( "{} minute{} {} second{} until {}:{:<02}{}".format( \ | |
minutes, \ | |
"s" if seconds > 1 else "s" if seconds == 0 else "", \ | |
seconds, \ | |
"s" if minutes > 1 else "s" if minutes == 0 else "", \ | |
int(next_hour.strftime("%I")), 0, next_hour.strftime("%p"))) | |
self.half1 = Timer(self.third_hour1, TimerEvent( "third.1" ), persist = True ) \ | |
.register(self) | |
self.half2 = Timer(self.third_hour2, TimerEvent( "third.2" ), persist = True ) \ | |
.register(self) | |
self.half3 = Timer(self.third_hour3, TimerEvent( "third.3" ), persist = True ) \ | |
.register(self) | |
self.hour = Timer(self.next_hour, TimerEvent( "hourly" ), persist = True ) \ | |
.register(self) | |
class App(Component): | |
service = Service() | |
app = (Debugger() + App()) | |
app.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment