add examples
This commit is contained in:
+15
-15
@@ -3,9 +3,9 @@ import asyncio, warnings
|
||||
class BaseNode:
|
||||
def __init__(self): self.params,self.successors={},{}
|
||||
def set_params(self,params): self.params=params
|
||||
def add_successor(self,node,cond="default"):
|
||||
if cond in self.successors: warnings.warn(f"Overwriting successor for condition '{cond}'")
|
||||
self.successors[cond]=node;return node
|
||||
def add_successor(self,node,action="default"):
|
||||
if action in self.successors: warnings.warn(f"Overwriting successor for action '{action}'")
|
||||
self.successors[action]=node;return node
|
||||
def prep(self,shared): return None
|
||||
def exec(self,shared,prep_res): return None
|
||||
def _exec(self,shared,prep_res): return self.exec(shared,prep_res)
|
||||
@@ -18,13 +18,13 @@ class BaseNode:
|
||||
if self.successors: warnings.warn("Node won't run successors. Use a parent Flow instead.")
|
||||
return self._run(shared)
|
||||
def __rshift__(self,other): return self.add_successor(other)
|
||||
def __sub__(self,cond):
|
||||
if isinstance(cond,str): return _ConditionalTransition(self,cond)
|
||||
raise TypeError("Condition must be a string")
|
||||
def __sub__(self,action):
|
||||
if isinstance(action,str): return _ConditionalTransition(self,action)
|
||||
raise TypeError("Action must be a string")
|
||||
|
||||
class _ConditionalTransition:
|
||||
def __init__(self,src,cond): self.src,self.cond=src,cond
|
||||
def __rshift__(self,tgt): return self.src.add_successor(tgt,self.cond)
|
||||
def __init__(self,src,action): self.src,self.action=src,action
|
||||
def __rshift__(self,tgt): return self.src.add_successor(tgt,self.action)
|
||||
|
||||
class Node(BaseNode):
|
||||
def __init__(self,max_retries=1):
|
||||
@@ -42,16 +42,16 @@ class BatchNode(Node):
|
||||
def _exec(self,shared,items): return [super(Node,self)._exec(shared,i) for i in items]
|
||||
|
||||
class Flow(BaseNode):
|
||||
def __init__(self,start_node):
|
||||
def __init__(self,start):
|
||||
super().__init__()
|
||||
self.start_node=start_node
|
||||
def get_next_node(self,curr,cond):
|
||||
nxt=curr.successors.get(cond if cond is not None else "default")
|
||||
self.start=start
|
||||
def get_next_node(self,curr,action):
|
||||
nxt=curr.successors.get(action if action is not None else "default")
|
||||
if not nxt and curr.successors:
|
||||
warnings.warn(f"Flow ends: condition '{cond}' not found in {list(curr.successors)}")
|
||||
warnings.warn(f"Flow ends: action '{action}' not found in {list(curr.successors)}")
|
||||
return nxt
|
||||
def _exec(self,shared,params=None):
|
||||
curr,p=self.start_node,(params if params else {**self.params})
|
||||
curr,p=self.start,(params if params else {**self.params})
|
||||
while curr:
|
||||
curr.set_params(p)
|
||||
c=curr._run(shared)
|
||||
@@ -83,7 +83,7 @@ class AsyncNode(Node):
|
||||
|
||||
class AsyncFlow(Flow,AsyncNode):
|
||||
async def _exec_async(self,shared,params=None):
|
||||
curr,p=self.start_node,(params if params else {**self.params})
|
||||
curr,p=self.start,(params if params else {**self.params})
|
||||
while curr:
|
||||
curr.set_params(p)
|
||||
c=await curr._run_async(shared) if hasattr(curr,"run_async") else curr._run(shared)
|
||||
|
||||
Reference in New Issue
Block a user