实现Python`when`构造的最佳方法是什么?

2024-10-17 06:21:25 发布

您现在位置:Python中文网/ 问答频道 /正文

假设你要发射火箭。当你上升时,你需要根据给定的表来确定角度。然而,在某些情况下,您需要执行一次性操作(例如,如果助推器是空的,则释放助推器,或者在特定高度丢弃整流罩)。你知道吗

我现在要做的是:

fairing_jettisoned = False
while apogee < TARGET_APOGEE:
    aim_rocket(speed, altitude, apogee)
    if not fairing_jettisoned and altitude > FAIRING_ALT:
        jettison_fairing()
        fairing_jettisoned = True

这对于更复杂的程序来说是非常乏味的,因为你必须来回跳跃。你知道吗

其他语言有一个when语句(when altitude > FAIRING_ALT:),但在Python中并不存在。你知道吗

我尝试用类来实现它。这是黑客,但似乎工作得很好:

class when(object):
    calls = {}
    def __init__(self, expr, func, *args, runs_left=1, **kwargs):
        _, fname, line, *_ = inspect.stack()[1]
        stored_instance = when.calls.get((fname, line), self)
        if stored_instance == self:
            self.func = func
            self.args = args
            self.kwargs = kwargs
            self.runs_left = runs_left
            when.calls[(fname, line)] = self
        self = stored_instance
        self.expr = expr
        if self.runs_left and self.expr:
            self.runs_left -= 1
            return self.func(*self.args, **self.kwargs

可以这样运行:

while apogee < TARGET_APOGEE:
    aim_rocket(speed, altitude, apogee)
    when(altitude > FAIRING_ALT, jettison_fairing)

最终,我想要的是这种语法:

while apogee < TARGET_APOGEE:
    aim_rocket(speed, altitude, apogee)
    when(altitude > FAIRING_ALT, runs_left=3):
        # Do a bunch of things; no need for a lambda or function.
        jettison_fairing()

我还尝试用__enter____exit__实现它,但是如果它已经运行了,我想不出一种方法可以跳到__exit__。使用async/await可以实现这样的功能吗?或者是完全不同的原因?你知道吗


Tags: selfrunsargsaltleftkwargsfuncwhen
1条回答
网友
1楼 · 发布于 2024-10-17 06:21:25

您可以尝试使用线程设置一些内容,例如:

from threading import Thread
import time, random

def when(condition, action) -> Thread:
    def threadtask():
        while not condition():
            time.sleep(0.5)
        action()
    t = Thread(target = threadtask)
    t.start()
    return t

altitude = 1
def jettison_fairing():
    print("Jettison the fairing!")
when(lambda: altitude > 500, jettison_fairing)
while altitude < 1000:
    time.sleep(1)
    altitude += random.randint(0,300)
    print("Altitude is now ",altitude)

# Output:

##Altitude is now  173
##Altitude is now  290
##Altitude is now  370
##Altitude is now  395
##Altitude is now  464
##Altitude is now  564
##Jettison the fairing!
##Altitude is now  736
##Altitude is now  762
##Altitude is now  1057

为了获得额外的积分,你可以写一个装饰师来做。你知道吗

然而。您所描述的是编程real-time systems所面临的一些问题。没有什么简单的“何时”结构能解决你的问题。例如,“什么时候”对你意味着什么,程序多久检查一次高度?如果您的程序同时等待50个不同的“when”条件,是否有更高优先级的条件?如果两个“when”条件同时发生,一组代码告诉计算机放弃整流罩,而另一组则假定整流罩仍然存在,会发生什么?你知道吗

相关问题 更多 >