[英]multiple conditional boolean logic Python
我可以獲得有關 Python 中布爾條件邏輯的更好方法的提示嗎? 我試圖根據在starttime
和endtime
之間的current_time
以及其他布爾邏輯來返回 True 或 False,以檢查作為 datetime 工作日數的current_day
是否是周末或工作日。
一個函數似乎最適合這個? 任何提示都可以幫助我遇到一個問題,如果Weekends
或Weekdays
都為False
,則意味着該事件被禁用,或者如果current_time
不在starttime
和endtime
之間,則該事件也將被禁用,因此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 似乎工作正常:
# True weekend/weekdays and on start/end times
condition_checker(data1,current_time,current_day)
返回
disabled is False
True
data2 似乎工作正常:
# True on Weekends and start/end times
condition_checker(data2,current_time,current_day)
返回
disabled is False
True
data3 似乎工作正常:
# False on times
condition_checker(data3,current_time,current_day)
返回
disabled is False
False
這是我的邏輯不工作的地方,在disabled
數據4不工作時應該是假的
# False on disabled
condition_checker(data4,current_time,current_day)
返回
disabled is False
True
您正在將字符串與函數中的布爾值進行比較。 我會改變字典來保存布爾值True
/ False
像這樣:
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'}
或者(不太可取但也有效),您可以更改函數以檢查string
值:
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.