hoangs's picture
Upload model.py
3778698 verified
from collections import defaultdict
from typing import Dict, List
def merge_schedules(entries: List[dict]) -> Dict[str, List[dict]]:
"""Group simple schedule entries by day of week.
Each entry is expected to have:
- `days`: comma-separated days like "mon,tue"
- `time_local`
- `zone_id`
Returns a mapping day -> list of small dicts with time and zone.
"""
result: Dict[str, List[dict]] = defaultdict(list)
for e in entries:
days_field = str(e.get("days", "")).strip()
time_local = str(e.get("time_local", "00:00"))
zone_id = str(e.get("zone_id", ""))
if not days_field:
continue
days = [d.strip() for d in days_field.split(",") if d.strip()]
for d in days:
result[d].append({"time_local": time_local, "zone_id": zone_id})
# sort each day's list by time
for d in result:
result[d].sort(key=lambda x: x["time_local"])
return dict(result)
if __name__ == "__main__":
entries = [
{"days": "mon,wed", "time_local": "09:00", "zone_id": "LR_CENTER"},
{"days": "mon", "time_local": "20:00", "zone_id": "KT_COOK"},
]
print(merge_schedules(entries))