是否有一个python库,允许您在创建递归堆栈时调用函数内的函数

时间:2017-08-25 10:05:18

标签: python transitions fsm

我正试图从一个状态切换到另一个状态。每个状态由一个函数表示。我想处理函数内部切换的逻辑,而不是调用函数的主循环。是否有允许这种模式的python库。 例如:

def c(count):
    if count<10:
        a(count+1)
        print("dont' keep track of post-order printing")
    else:
        print("when count is reached, program should terminate")
def b(count):
    c(count):
def a(count):
    b(count)
a()

这只是一个例子,我试图使用有限状态模式,其中逻辑在转换on_enter方法内。基本上,机器将自动从一种状态转换到下一种状态,而无需使用main_loop。

3 个答案:

答案 0 :(得分:1)

有fn.tcor https://github.com/kachayev/fn.py/blob/master/README.rst#trampolines-decorator。但是这个库似乎没有维护。你可以在Python中寻找尾部优化......你可能会找到一些东西......

答案 1 :(得分:1)

transitions包含有序转换以及queued转换,这些转换可能就是您要查找的内容。基本技巧是在处理完同一事件时调用同一事件(使用finalize)。即使转换未成功(并非所有finalize返回conditions),也会处理传递给True的回调。使用queued转换不会立即处理事件,而是在当前处理的事件之后,这将阻止大量递归。

from transitions import Machine
import time


class Model(object):

    # initialise counter and counter limit
    def __init__(self):
        self.counter = 0
        self.limit = 5

    # will be called in every cycle and increase the counter
    def increase_counter(self):
        self.counter += 1
        print("Counter increased to ", self.counter)
        time.sleep(0.5)

    # will be called whenever a new state has been entered
    def reset_counter(self):
        self.counter = 0
        print("Counter reset; Current state is ", model.state)

    # this function returns whether the limit has already been reached
    def limit_reached(self):
        return self.counter >= self.limit

# initialising the previously defined model
model = Model()
# creating some state names
states = ['A', 'B', 'C', 'D']

# configuring the state machine:
# pass the model (for callbacks), pass the state names,
# disable auto_transitions since we will not need them
# set the initial state to 'A' and call a (currently) undefined
# model function 'next_state' after EVERY triggered event.
# 'queued' means that every transition is finished before the next event is handled
machine = Machine(model, states=states, auto_transitions=False,
                  queued=True, initial='A', finalize_event='next_state')

# now add ordered transitions: 
# Depending on the order of the passed state names,
# create transitions from each state 'n' to state 'n+1' called 'next_state'.
# 'prepare' each transition attempt by increasing the counter
# afterwards check the 'conditions' (is the counter limit reached)
# if all callbacks in 'conditions' return True, the transition
# is conducted and callbacks in 'after' are processed (counter reset)
machine.add_ordered_transitions(prepare='increase_counter', conditions='limit_reached',
                                after='reset_counter', trigger='next_state')

# model will go into an infinite loop; can be triggered in a thread
model.next_state()

您可以尝试减少increase_counter中的睡眠定时器,以检查是否会出现递归错误(您不应该)。如果将queued=False设置为标准行为,则会立即或多或少地触发递归错误,因为将立即处理所有机器触发器。

答案 2 :(得分:0)

我希望它可能符合您的期望

def c(count):
    if count<10:
        print("don't keep track of post-order printing")
        c(count+1)
    else:
        print("when count is reached, program should terminate")

输出:

>>> c(1)
don't keep track of post-order printing
don't keep track of post-order printing
don't keep track of post-order printing
don't keep track of post-order printing
don't keep track of post-order printing
don't keep track of post-order printing
don't keep track of post-order printing
don't keep track of post-order printing
don't keep track of post-order printing
when count is reached, program should terminate