I'm writing a small program in python-3.6 to control my garden watering.
I have a file where I set the timers. For example:
Monday 7:00
The involved functions look like this:
def getTimerFile():
""" Get timers from file an prepare them."""
timerFile = config.get("Basic", "TimerFile")
allowedDays = ["Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
"Sunday"]
try:
with open(timerFile) as f:
d = dict(x.rstrip().split(" ", 1) for x in f)
for key, value in d.items():
time.strptime(value, "%H:%M")
if key not in allowedDays:
raise ValueError(" {}".format(key))
except Exception as e:
# TODO: Error handling
return d
def setTimers(timerDict):
"""Set timer threads."""
for key, value in timerDict.items():
findDaySchedule(key, value)
def findDaySchedule(weekday, stTime):
"""Helper function
for switch/case statement."""
return {"Monday": schedule.every().monday.at(stTime).do(mv),
"Tuesday": schedule.every().tuesday.at(stTime).do(mv),
"Wednesday": schedule.every().wednesday.at(stTime).do(mv),
"Thursday": schedule.every().thursday.at(stTime).do(mv),
"Friday": schedule.every().friday.at(stTime).do(mv),
"Saturday": schedule.every().saturday.at(stTime).do(mv),
"Sunday": schedule.every().sunday.at(stTime).do(mv)
}.get(weekday, None) # Default is None
This works as expected if I use only one timer per day. But if for ex. I want two timers on Saturday it will only take the last one.
So what would be the best solution to work with this duplicate keys. I tried something with 'defaultdict' but I didn't get the loop for the timers working correctly
Thank's in advance