Reputation: 3502
Can I get a tip on a better way of Boolean conditional logic in Python? I am trying to return a True or False based on the current_time
being between a starttime
& endtime
as well as other Boolean logic to check if the current_day
that is datetime weekday number is a weekend or weekday.
Is a function seemed best fit for this? Any tips help I am having an issue where if Weekends
or Weekdays
are both False
that means the event is disabled or if the current_time
is not between starttime
& endtime
the event will also be disabled, so Return False
from datetime import datetime
now = datetime.now()
current_time = now.strftime("%H:%M")
print("Current Time is ", current_time)
# To Get the Week Number
current_day = datetime.today().weekday()
print("Current Weekday Number is ", current_day)
data1 = {'setpoint': '366', 'Weekdays': 'True', 'Weekends': 'True', 'starttime': '02:40', 'endtime': '22:40'}
data2 = {'setpoint': '366', 'Weekdays': 'False', 'Weekends': 'True', 'starttime': '02:40', 'endtime': '22:40'}
data3 = {'setpoint': '366', 'Weekdays': 'True', 'Weekends': 'False', 'starttime': '02:40', 'endtime': '03:40'}
data4 = {'setpoint': '366', 'Weekdays': 'False', 'Weekends': 'False', 'starttime': '02:40', 'endtime': '22:40'}
def condition_checker(data,current_time,current_day):
between_time = data['starttime'] <= current_time <= data['endtime']
#print("between_time", between_time)
disabled = data['Weekends'] == False and data['Weekdays'] == False
print("disabled is", disabled)
weekday_rules = current_day in [0,1,2,3,4]
#print("weekday_rules", weekday_rules)
weekend_rules = current_day in [5,6]
#print("weekend_rules", weekend_rules)
if between_time and not disabled and weekday_rules or between_time and not disabled and weekend_rules:
return True
else:
return False
data1 seems to work Ok:
# True weekend/weekdays and on start/end times
condition_checker(data1,current_time,current_day)
returns
disabled is False
True
data2 seems to work Ok:
# True on Weekends and start/end times
condition_checker(data2,current_time,current_day)
returns
disabled is False
True
data3 seems to work Ok:
# False on times
condition_checker(data3,current_time,current_day)
returns
disabled is False
False
This is where my logic isnt working, should be false on disabled
data4 NOT working
# False on disabled
condition_checker(data4,current_time,current_day)
returns
disabled is False
True
Upvotes: 1
Views: 129
Reputation: 23146
You are comparing strings with boolean values in your function. I would change the dictionaries to hold boolean True
/False
like so:
data1 = {'setpoint': '366', 'Weekdays': True, 'Weekends': True, 'starttime': '02:40', 'endtime': '22:40'}
data2 = {'setpoint': '366', 'Weekdays': False, 'Weekends': True, 'starttime': '02:40', 'endtime': '22:40'}
data3 = {'setpoint': '366', 'Weekdays': True, 'Weekends': False, 'starttime': '02:40', 'endtime': '03:40'}
data4 = {'setpoint': '366', 'Weekdays': False, 'Weekends': False, 'starttime': '02:40', 'endtime': '22:40'}
Alternatively (less preferable but also works), you can change your function to check against string
values:
def condition_checker(data,current_time,current_day):
between_time = data['starttime'] <= current_time <= data['endtime']
disabled = data['Weekends'] == "False" and data['Weekdays'] == "False"
print("disabled is", disabled)
weekday_rules = current_day in [0,1,2,3,4]
weekend_rules = current_day in [5,6]
if between_time and not disabled and weekday_rules or between_time and not disabled and weekend_rules:
return True
else:
return False
Upvotes: 1