1

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
bbartling
  • 3,288
  • 9
  • 43
  • 88
  • I'm having some trouble understandign what you actually want. Can you clarify what `weekday_rules` and `weekend_rules` are supposed to do? – Pranav Hosangadi Jul 07 '21 at 15:22
  • Note that your `if ...` condition seems to be missing parentheses. Did you mean `(between_time and not disabled and weekday_rules) or (between_time and not disabled and weekend_rules)`? If so, (and AFAIU `weekday_rules` is simply `not weekend_rules`), this will boil down to simply `if (between_time and not disabled): return True; else: return False`, or `return between_time and not disabled` – Pranav Hosangadi Jul 07 '21 at 15:24
  • 2
    Your dictionaries have "True" and "False" as strings. Your `disabled` statement is comparing these with boolean `True` and `False`. – not_speshal Jul 07 '21 at 15:26
  • @PranavHosangadi I am just trying to verify if current weekday number is a weekend or weekday as well as if the dictionary keys are True too. If they are False, then return False – bbartling Jul 07 '21 at 15:29
  • @not_speshal is there a better way to do this? I realize its strings – bbartling Jul 07 '21 at 15:30
  • Maybe I just need to change to `str(False)` didnt realize this... – bbartling Jul 07 '21 at 15:32
  • Or just "False"? But it would be better to change your dictionary values to boolean like: `data1 = {'setpoint': '366', 'Weekdays': True, 'Weekends': True, 'starttime': '02:40', 'endtime': '22:40'}` – not_speshal Jul 07 '21 at 15:33
  • This works, `disabled = data['Weekends'] == str(False) and data['Weekdays'] == str(False)` thanks for pointing out `strings` – bbartling Jul 07 '21 at 15:33
  • @not_speshal thanks for the tips if you posted an answer Ill hit the green check. Ill also look into changing the dictionary values to Boolean as you recommended... – bbartling Jul 07 '21 at 15:38

1 Answers1

1

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
not_speshal
  • 22,093
  • 2
  • 15
  • 30