I’m a senior PHP and Perl dude, but Python is new to me. I’m enjoying learning it! I wrote the below code, but I can’t shake off the feeling that it could be better written by someone with “senior level” Python skills. Are you true Pythonians up to a fun challenge?
Please note: I want the code to readable. Python is suppose to be readable – we are not writing Perl here people! (example: I like that ‘weekday’ is a string instead of an integer, makes it VERY clear)
import datetime
today = datetime.datetime.now()
weekday = today.strftime("%a")
hourmin = int(today.strftime("%H%M"))
print "today here is: " + today.strftime("%c") # for debug
if weekday == "Sat" or \
(weekday == "Sun" and hourmin < 2000) or \
(weekday == "Fri" and hourmin > 1630) or \
(hourmin >= 1630 and hourmin < 2000) :
print "bad time"
else:
print "good time"
You can map a day string to a lambda that takes hourmin and determines whether it is bad or not. For example:
edit: Follow kindall’s advice.