Upload model.py
Browse files
model.py
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Literal, TypedDict, List
|
| 2 |
+
|
| 3 |
+
DayPattern = Literal[
|
| 4 |
+
"daily",
|
| 5 |
+
"weekdays",
|
| 6 |
+
"weekend",
|
| 7 |
+
"mon_wed_fri",
|
| 8 |
+
"tue_thu",
|
| 9 |
+
"mon_thu",
|
| 10 |
+
"sat",
|
| 11 |
+
"sun",
|
| 12 |
+
"monthly",
|
| 13 |
+
]
|
| 14 |
+
|
| 15 |
+
class Routine(TypedDict):
|
| 16 |
+
routine_id: str
|
| 17 |
+
days: str
|
| 18 |
+
time_local: str # HH:MM
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
class RoutineDecision(TypedDict):
|
| 22 |
+
routine_id: str
|
| 23 |
+
should_run: bool
|
| 24 |
+
reason: str
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def _matches_day(pattern: str, weekday: int) -> bool:
|
| 28 |
+
"""weekday: 0=Monday ... 6=Sunday"""
|
| 29 |
+
p = pattern.lower()
|
| 30 |
+
if p == "daily":
|
| 31 |
+
return True
|
| 32 |
+
if p == "weekdays":
|
| 33 |
+
return weekday < 5
|
| 34 |
+
if p == "weekend":
|
| 35 |
+
return weekday >= 5
|
| 36 |
+
if p == "sat":
|
| 37 |
+
return weekday == 5
|
| 38 |
+
if p == "sun":
|
| 39 |
+
return weekday == 6
|
| 40 |
+
if p == "mon_wed_fri":
|
| 41 |
+
return weekday in (0, 2, 4)
|
| 42 |
+
if p == "tue_thu":
|
| 43 |
+
return weekday in (1, 3)
|
| 44 |
+
if p == "mon_thu":
|
| 45 |
+
return weekday in (0, 3)
|
| 46 |
+
if p == "monthly":
|
| 47 |
+
return False # handled elsewhere or by calendar
|
| 48 |
+
return False
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
def decide_routines_to_run(routines: List[Routine], weekday: int, current_time: str) -> List[RoutineDecision]:
|
| 52 |
+
"""Decide which routines should run at a given weekday/time.
|
| 53 |
+
|
| 54 |
+
- weekday: 0=Monday ... 6=Sunday
|
| 55 |
+
- current_time: 'HH:MM' string
|
| 56 |
+
"""
|
| 57 |
+
decisions: List[RoutineDecision] = []
|
| 58 |
+
for r in routines:
|
| 59 |
+
rid = r["routine_id"]
|
| 60 |
+
days = r.get("days", "daily")
|
| 61 |
+
time_local = r.get("time_local", "00:00")
|
| 62 |
+
|
| 63 |
+
if not _matches_day(days, weekday):
|
| 64 |
+
decisions.append(
|
| 65 |
+
{
|
| 66 |
+
"routine_id": rid,
|
| 67 |
+
"should_run": False,
|
| 68 |
+
"reason": f"Pattern '{days}' does not match this weekday.",
|
| 69 |
+
}
|
| 70 |
+
)
|
| 71 |
+
continue
|
| 72 |
+
|
| 73 |
+
if time_local == current_time:
|
| 74 |
+
decisions.append(
|
| 75 |
+
{
|
| 76 |
+
"routine_id": rid,
|
| 77 |
+
"should_run": True,
|
| 78 |
+
"reason": "Time and day pattern match current moment.",
|
| 79 |
+
}
|
| 80 |
+
)
|
| 81 |
+
else:
|
| 82 |
+
decisions.append(
|
| 83 |
+
{
|
| 84 |
+
"routine_id": rid,
|
| 85 |
+
"should_run": False,
|
| 86 |
+
"reason": f"Current time {current_time} != routine time {time_local}.",
|
| 87 |
+
}
|
| 88 |
+
)
|
| 89 |
+
return decisions
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
if __name__ == "__main__":
|
| 93 |
+
sample = [
|
| 94 |
+
{"routine_id": "RT_MORNING_LR", "days": "daily", "time_local": "07:15"},
|
| 95 |
+
{"routine_id": "RT_LAUNDRY_REMIND", "days": "sat", "time_local": "10:00"},
|
| 96 |
+
]
|
| 97 |
+
# Example: Saturday at 07:15 -> weekday=5
|
| 98 |
+
for d in decide_routines_to_run(sample, weekday=5, current_time="07:15"):
|
| 99 |
+
print(d)
|