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
9
  • I'm having some trouble understandign what you actually want. Can you clarify what weekday_rules and weekend_rules are supposed to do? Commented Jul 7, 2021 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 Commented Jul 7, 2021 at 15:24
  • 2
    Your dictionaries have "True" and "False" as strings. Your disabled statement is comparing these with boolean True and False. Commented Jul 7, 2021 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 Commented Jul 7, 2021 at 15:29
  • @not_speshal is there a better way to do this? I realize its strings Commented Jul 7, 2021 at 15:30

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

5 Comments

Any chance you could give me a tip on how to do that? Still learning alot here... stackoverflow.com/questions/68289246/sqlite-boolean-operators
How to do what? Copy-paste the dictionaries I have in my answer and use your original function. Notice how the True and False values are not in quotes.
The data comes from HTML form input tables
In that case, it's better to change your function to compare with =="True". I'd rather not change input data.
Ok thanks for your help keep as a String comparison....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.