Generally everything is looking rather simple and not complicated. That’s good. Below are some things I’ve noticed. Keep in mind those are just tips, pointers and other suggestions what can be looked at. Some of these can be applied also to other places in code.
startHour = startTime.split(" ")
hr_minu = startHour[0].split(":")
hr = int(hr_minu[0])
minu = int(hr_minu[1])
am_pm = startHour[1]
python has very handy syntax allowing simplifying such assignments and don’t deal with the indexes, for example having list with two elements and two variables to assign to elements from that list:
words = ['Word 1', 'Word 2']
first, second = words
Would assign 'Word 1'
to first
and 'Word 2'
to second
.
if new_hr > 12 and new_hr < 24:
One more handy python syntax allows chaining two separate conditions using comparison operators and comparing the same variable. For example a > 1 and a < 5
can be written as:
1 < a < 5
else:
pass
else
and pass
is obsolete here
while day_index >= 7:
day_index = day_index % 7
This while
loop will never be executed more than once, as % 7
always should bring the number down under the 7
.
if (startingDay):
startingDay = str(startingDay)
startingDay = startingDay.lower()
initialDay = daysWeek.index(startingDay)
Parenthesis in if (startingDay):
is not needed. Also for the following lines, assuming
that function is called correctly and startingDay
has some non falsy value, this makes str
type change to str
. After getting rid of that and moving lower()
method call three lines can be replaced by single line.
final_hr = str(new_hr) + ":" + str(new_minu) + " " + str(am_pm)
You can take a look at the so-called f-strings or format
method of the str
type. These can help a bit with making lines like this slightly more readable.
new_time = final_hr
I suppose comment before this line might be explaining why this assignment is done, but I’m unable to read that. Why not call it new_time
from the start, instead of final_hr
?
Definitely take a look at the mentioned PEP8 style guide for python, it contains some general guidelines how code written in python is usually formatted. Especially notice how it’s recommended to name variables, as that’s a bit inconsistent in this function.