如何缩短 if 语句?
How do I get the if-statement shorter?
我想将 1 到 4 个光栅连接到我的 RaspberryPi,但我不知道如何在较短的线路中区分大小写(见下文)。有人能告诉我如何用更短的方式编写以下代码吗?下面写的每个案例都应该这样做。
LIGHT_BARRIER_PIN_1 = 24
l2, l3, l4 = False, False, False
if config.value("rfu630.lightbarrier") >= 2:
l2 = True
LIGHT_BARRIER_PIN_2 = 5
if config.value("rfu630.lightbarrier") >= 3:
l3 = True
LIGHT_BARRIER_PIN_3 = 6
if config.value("rfu630.lightbarrier") >= 4:
l4 = True
LIGHT_BARRIER_PIN_4 = 13
while True:
if l4 == True:
if GPIO.input(LIGHT_BARRIER_PIN_1) == GPIO.HIGH or GPIO.input(LIGHT_BARRIER_PIN_2) == GPIO.HIGH or GPIO.input(LIGHT_BARRIER_PIN_3) == GPIO.HIGH or GPIO.input(LIGHT_BARRIER_PIN_4) == GPIO.HIGH:
pass
elif l3 == True:
if GPIO.input(LIGHT_BARRIER_PIN_1) == GPIO.HIGH or GPIO.input(LIGHT_BARRIER_PIN_2) == GPIO.HIGH or GPIO.input(LIGHT_BARRIER_PIN_3) == GPIO.HIGH:
pass
elif l2 == True:
if GPIO.input(LIGHT_BARRIER_PIN_1) == GPIO.HIGH or GPIO.input(LIGHT_BARRIER_PIN_2) == GPIO.HIGH:
pass
else:
if GPIO.input(LIGHT_BARRIER_PIN_1) == GPIO.HIGH:
pass
对于下半部分,您可以预先计算语句:
while True:
a = GPIO.input(LIGHT_BARRIER_PIN_1) == GPIO.HIGH
b = GPIO.input(LIGHT_BARRIER_PIN_2) == GPIO.HIGH
c = GPIO.input(LIGHT_BARRIER_PIN_3) == GPIO.HIGH
d = GPIO.input(LIGHT_BARRIER_PIN_4) == GPIO.HIGH
if l4:
if (a|b|c|d):
pass
elif l3:
if (a|b|c):
pass
elif l2:
if (a|b):
pass
else:
if a:
pass
您可以列出一个 pin 号码列表,将其分成活动号码列表(根据您的配置),然后使用 any()
和 map()
来检查它们:
# light barrier pin #1 #2 #3 #4
LIGHT_BARRIER_PINS = [24, 5, 6, 13]
lightbarrier_max = config.value("rfu630.lightbarrier")
active_pins = LIGHT_BARRIER_PINS[:lightbarrier_max]
while True:
if any(map(lambda p: GPIO.input(p) == GPIO.HIGH, active_pins)):
pass
或者,为了便于阅读,使用辅助函数:
# light barrier pin #1 #2 #3 #4
LIGHT_BARRIER_PINS = [24, 5, 6, 13]
lightbarrier_max = config.value("rfu630.lightbarrier")
active_pins = LIGHT_BARRIER_PINS[:lightbarrier_max]
def is_high(pin):
return GPIO.input(pin) == GPIO.HIGH
while True:
if any(map(is_high, active_pins)):
pass
我想将 1 到 4 个光栅连接到我的 RaspberryPi,但我不知道如何在较短的线路中区分大小写(见下文)。有人能告诉我如何用更短的方式编写以下代码吗?下面写的每个案例都应该这样做。
LIGHT_BARRIER_PIN_1 = 24
l2, l3, l4 = False, False, False
if config.value("rfu630.lightbarrier") >= 2:
l2 = True
LIGHT_BARRIER_PIN_2 = 5
if config.value("rfu630.lightbarrier") >= 3:
l3 = True
LIGHT_BARRIER_PIN_3 = 6
if config.value("rfu630.lightbarrier") >= 4:
l4 = True
LIGHT_BARRIER_PIN_4 = 13
while True:
if l4 == True:
if GPIO.input(LIGHT_BARRIER_PIN_1) == GPIO.HIGH or GPIO.input(LIGHT_BARRIER_PIN_2) == GPIO.HIGH or GPIO.input(LIGHT_BARRIER_PIN_3) == GPIO.HIGH or GPIO.input(LIGHT_BARRIER_PIN_4) == GPIO.HIGH:
pass
elif l3 == True:
if GPIO.input(LIGHT_BARRIER_PIN_1) == GPIO.HIGH or GPIO.input(LIGHT_BARRIER_PIN_2) == GPIO.HIGH or GPIO.input(LIGHT_BARRIER_PIN_3) == GPIO.HIGH:
pass
elif l2 == True:
if GPIO.input(LIGHT_BARRIER_PIN_1) == GPIO.HIGH or GPIO.input(LIGHT_BARRIER_PIN_2) == GPIO.HIGH:
pass
else:
if GPIO.input(LIGHT_BARRIER_PIN_1) == GPIO.HIGH:
pass
对于下半部分,您可以预先计算语句:
while True:
a = GPIO.input(LIGHT_BARRIER_PIN_1) == GPIO.HIGH
b = GPIO.input(LIGHT_BARRIER_PIN_2) == GPIO.HIGH
c = GPIO.input(LIGHT_BARRIER_PIN_3) == GPIO.HIGH
d = GPIO.input(LIGHT_BARRIER_PIN_4) == GPIO.HIGH
if l4:
if (a|b|c|d):
pass
elif l3:
if (a|b|c):
pass
elif l2:
if (a|b):
pass
else:
if a:
pass
您可以列出一个 pin 号码列表,将其分成活动号码列表(根据您的配置),然后使用 any()
和 map()
来检查它们:
# light barrier pin #1 #2 #3 #4
LIGHT_BARRIER_PINS = [24, 5, 6, 13]
lightbarrier_max = config.value("rfu630.lightbarrier")
active_pins = LIGHT_BARRIER_PINS[:lightbarrier_max]
while True:
if any(map(lambda p: GPIO.input(p) == GPIO.HIGH, active_pins)):
pass
或者,为了便于阅读,使用辅助函数:
# light barrier pin #1 #2 #3 #4
LIGHT_BARRIER_PINS = [24, 5, 6, 13]
lightbarrier_max = config.value("rfu630.lightbarrier")
active_pins = LIGHT_BARRIER_PINS[:lightbarrier_max]
def is_high(pin):
return GPIO.input(pin) == GPIO.HIGH
while True:
if any(map(is_high, active_pins)):
pass