I am crazy about needing to calculate my work shifts for my part-time job while attending University. This is because I cannot work more than 20 hours a week.
Yes, this code is crap, I wrote it in 20 minutes... my brain operates kinda like a pachinko machine, and whatever it writes for stupidly annoying scripts like this it just writes, screw quality. Don't ding me for that, please, I just haven't had enough coffee or sleep lately to write better code.
So, I wrote a Python script that lets me do one of two things:
- Process my 'normal' work schedule, which is hardcoded as the 'default' schedule
- Process the first argument (which should be a dict containing the name of the day of the week, and contain a list of lists which contain the start and end time for each shift, using 24hr time format), passed into the program at runtime as an argument.
Regardless of these cases, it will always return something like:
Total Weekly Hours: 15.0
(as an example, using the 'default' hardcoded schedule).
When executing as a shell command, or with the python
executable, you have to put the "schedule" dict in quotes. This is an example of execution of the file and passing in a schedule where I have to take on additional hours (on the command line, so this would be executed verbatim):
./workhours.py "{'Monday': [['09:00', '13:00']], 'Tuesday': [['09:00', '10:30'], ['17:00', '19:00']], 'Wednesday': [['09:00', '11:00']], 'Thursday': [['13:00', '15:00']], 'Friday': [['10:00', '17:00']]}"
As long as the first argument is an entire dict, as a string so it's parsed right by the application, it seems to work fine, and gives me correct values (for example, the last example would return a value of 18.5
hours which is accurate).
While this works, I'd like suggestions for how I could improve this. It's a quickly-thrown-together script that does what I want, but suggestions for improvement are definitely accepted.
This is the code:
workhours.py:
#!/usr/bin/python
import sys
import os
from ast import literal_eval
from datetime import datetime, timedelta
_TIME_FORMAT = '%H:%M'
def set_timeformat(self, formatstr=_TIME_FORMAT):
self._TIME_FORMAT = formatstr
_DEFAULT_SCHEDULE = {
'Sunday': None,
'Monday': [['09:00', '13:00']],
'Tuesday': None,
'Wednesday': [['09:00', '11:00']],
'Thursday': [['13:00', '15:00']],
'Friday': [['10:00', '17:00']],
'Saturday': None
}
def _calc_shift_hours(shift):
tdelta = datetime.strptime(shift[1], _TIME_FORMAT) - datetime.strptime(shift[0], _TIME_FORMAT)
if tdelta.days < 0:
tdelta = timedelta(days=0, seconds=tdelta.seconds, microseconds=tdelta.microseconds)
return tdelta.seconds / 3600.0
def _calc_workhours(sched=_DEFAULT_SCHEDULE):
hours = {
'Sunday': 0,
'Monday': 0,
'Tuesday': 0,
'Wednesday': 0,
'Thursday': 0,
'Friday': 0,
'Saturday': 0
}
totalhours = 0
for day in sched.iterkeys():
if sched[day] is None:
pass
else:
# noinspection PyTypeChecker
for shift in sched[day]:
hours[day] += _calc_shift_hours(shift)
totalhours += hours[day]
os.system('clear')
print "Total Weekly Hours: %s" % totalhours
if __name__ == '__main__':
try:
dictarg = literal_eval(sys.argv[1])
if isinstance(dictarg, dict):
schedule = dictarg
else:
schedule = _DEFAULT_SCHEDULE
except IndexError:
schedule = _DEFAULT_SCHEDULE
_calc_workhours(schedule)