2017-08-25 4 views
-3

ある州から別の州に切り替えることを試みています。各状態は関数で表されます。関数を呼び出すメインループではなく、関数内で切り替えるロジックを処理したいと思います。そのようなパターンを可能にするPythonライブラリはありますか?例えばだから、 :関数内で関数を呼び出すことができるpythonライブラリがありますか?再帰的スタックを作成するwtihout

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を使用せずにある状態から次の状態に移行します。

+0

私はこのコルーチンをおそらく見るでしょう –

+0

@ user252046再帰的な問題は何ですか?なぜあなたは "それなし"を望んでいますか? – Rajez

+0

ベースケースに達した後にプログラムがコールバックしないようにしたい。そしてループは永遠に続く。私はそれが関数を呼び出すメインループを持つことによって達成することができますが、それは混乱の全体をたくさん作成するだろう知っている。 – user252046

答えて

0

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を設定した場合、すべてのマシントリガーが即座に処理されるため、再帰エラーが多少なりとも発生します。

+0

これは完全に機能します! – user252046

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 
関連する問題