【发布时间】:2022-01-15 21:47:42
【问题描述】:
我正在开发一个接收连续数字输入流的模块。目标是检测输入数组超过某个预设阈值的第一次时间。换句话说,我需要运行一个比较函数,直到达到阈值;那么该功能需要“关闭”。
我的想法是使用装饰器来解决问题,因为我知道它们可以有效地用于运行一个函数,并且永远不会再次运行,这与我想要实现的有点相似。
在以下情况下,数字输入的连续流是:12, 19, 82, 92, 26, ...。在这种情况下,预期的输出将是:
Rand. val: 12
above_threshold returns False
Rand. val: 19
above_threshold returns False
Rand. val: 82
above_threshold returns True
Threshold has been reached!
Comparison function above_threshold shouldn't be called any more.
Rand. val: 92
Rand. val: 26
...
目前,above_threshold 在每个循环中都被调用,我还没有成功地使用装饰器“关闭”该功能。
import time
import random
random.seed(12771)
threshold = 75
def run_until_first_true_reached(f):
"""
Decorator that runs the function f until it first returns True.
After returning True once, it will stop running the wrapped function again.
"""
def wrapper(*args, **kwargs):
# If f is False
if not f(*args, **kwargs):
return f(*args, **kwargs)
# If f is True
else:
print("Threshold has been reached!")
print("Comparison function above_threshold shouldn't be called any more.")
# tried an empty "return" in this line but didn't solve the issue
return wrapper
@run_until_first_true_reached
def above_threshold(value, threshold):
if value > threshold:
print("above_threshold returns True")
return True
else:
print("above_threshold returns False")
return False
# Modelling the continuous stream of inputs
for _ in range(100):
rand_val = random.randint(1,100)
print("Rand. val: ", rand_val)
above_threshold(rand_val, threshold)
time.sleep(1)
【问题讨论】:
-
@BemwaMalak 在哪里?在
wrapper?的else子句中? -
可能感兴趣:
iter(f, x)产生对f的一系列调用,直到f返回x。 -
解决当前问题的尝试对我来说似乎(至少)很奇怪。为什么不在循环内:
if not threshold_reached(args, kw):handler_func(args, kw)? -
@chepner 如果我用 for 循环中的
above_threshold调用替换该行,使用您建议的内容:iter(above_threshold, True),above_threshold方法不会打印任何输出。 -
@CristiFati,我不明白你的意思。我想检测 first 值超过阈值的时间。但我不想打破循环。我只想在控制台上指示已达到阈值。只有一次。
标签: python decorator python-decorators