change the syntax of exec

This commit is contained in:
zachary62
2024-12-29 02:40:27 +00:00
parent 96cecb9086
commit 2550decdc5
11 changed files with 103 additions and 101 deletions
+3 -3
View File
@@ -3,11 +3,11 @@ import asyncio
import sys
from pathlib import Path
sys.path.append(str(Path(__file__).parent.parent))
sys.path.insert(0, str(Path(__file__).parent.parent))
from minillmflow import AsyncNode, BatchAsyncFlow
class AsyncDataProcessNode(AsyncNode):
def exec(self, shared_storage, prep_result):
def prep(self, shared_storage):
key = self.params.get('key')
data = shared_storage['input_data'][key]
if 'results' not in shared_storage:
@@ -18,7 +18,7 @@ class AsyncDataProcessNode(AsyncNode):
async def post_async(self, shared_storage, prep_result, proc_result):
await asyncio.sleep(0.01) # Simulate async work
key = self.params.get('key')
shared_storage['results'][key] = proc_result * 2 # Double the value
shared_storage['results'][key] = prep_result * 2 # Double the value
return "processed"
class AsyncErrorNode(AsyncNode):
+6 -6
View File
@@ -3,7 +3,7 @@ import asyncio
import sys
from pathlib import Path
sys.path.append(str(Path(__file__).parent.parent))
sys.path.insert(0, str(Path(__file__).parent.parent))
from minillmflow import Node, AsyncNode, AsyncFlow
@@ -17,7 +17,7 @@ class AsyncNumberNode(AsyncNode):
super().__init__()
self.number = number
def exec(self, shared_storage, data):
def prep(self, shared_storage):
# Synchronous work is allowed inside an AsyncNode,
# but final 'condition' is determined by post_async().
shared_storage['current'] = self.number
@@ -34,7 +34,7 @@ class AsyncIncrementNode(AsyncNode):
"""
Demonstrates incrementing the 'current' value asynchronously.
"""
def exec(self, shared_storage, data):
def prep(self, shared_storage):
shared_storage['current'] = shared_storage.get('current', 0) + 1
return "incremented"
@@ -110,7 +110,7 @@ class TestAsyncFlow(unittest.TestCase):
"""
class BranchingAsyncNode(AsyncNode):
def exec(self, shared_storage, data):
def exec(self, data):
value = shared_storage.get("value", 0)
shared_storage["value"] = value
# We'll decide branch based on whether 'value' is positive
@@ -124,12 +124,12 @@ class TestAsyncFlow(unittest.TestCase):
return "negative_branch"
class PositiveNode(Node):
def exec(self, shared_storage, data):
def exec(self, data):
shared_storage["path"] = "positive"
return None
class NegativeNode(Node):
def exec(self, shared_storage, data):
def exec(self, data):
shared_storage["path"] = "negative"
return None
+6 -6
View File
@@ -2,11 +2,11 @@ import unittest
import sys
from pathlib import Path
sys.path.append(str(Path(__file__).parent.parent))
sys.path.insert(0, str(Path(__file__).parent.parent))
from minillmflow import Node, BatchFlow, Flow
class DataProcessNode(Node):
def exec(self, shared_storage, prep_result):
def prep(self, shared_storage):
key = self.params.get('key')
data = shared_storage['input_data'][key]
if 'results' not in shared_storage:
@@ -14,7 +14,7 @@ class DataProcessNode(Node):
shared_storage['results'][key] = data * 2
class ErrorProcessNode(Node):
def exec(self, shared_storage, prep_result):
def prep(self, shared_storage):
key = self.params.get('key')
if key == 'error_key':
raise ValueError(f"Error processing key: {key}")
@@ -107,14 +107,14 @@ class TestBatchFlow(unittest.TestCase):
def test_nested_flow(self):
"""Test batch processing with nested flows"""
class InnerNode(Node):
def exec(self, shared_storage, prep_result):
def exec(self, prep_result):
key = self.params.get('key')
if 'intermediate_results' not in shared_storage:
shared_storage['intermediate_results'] = {}
shared_storage['intermediate_results'][key] = shared_storage['input_data'][key] + 1
class OuterNode(Node):
def exec(self, shared_storage, prep_result):
def exec(self, prep_result):
key = self.params.get('key')
if 'results' not in shared_storage:
shared_storage['results'] = {}
@@ -148,7 +148,7 @@ class TestBatchFlow(unittest.TestCase):
def test_custom_parameters(self):
"""Test batch processing with additional custom parameters"""
class CustomParamNode(Node):
def exec(self, shared_storage, prep_result):
def exec(self, prep_result):
key = self.params.get('key')
multiplier = self.params.get('multiplier', 1)
if 'results' not in shared_storage:
+10 -12
View File
@@ -2,7 +2,7 @@ import unittest
import sys
from pathlib import Path
sys.path.append(str(Path(__file__).parent.parent))
sys.path.insert(0, str(Path(__file__).parent.parent))
from minillmflow import Node, BatchNode, Flow
class ArrayChunkNode(BatchNode):
@@ -14,16 +14,14 @@ class ArrayChunkNode(BatchNode):
# Get array from shared storage and split into chunks
array = shared_storage.get('input_array', [])
chunks = []
for i in range(0, len(array), self.chunk_size):
end = min(i + self.chunk_size, len(array))
chunks.append((i, end))
for start in range(0, len(array), self.chunk_size):
end = min(start + self.chunk_size, len(array))
chunks.append(array[start: end])
return chunks
def exec(self, shared_storage, chunk_indices):
start, end = chunk_indices
array = shared_storage['input_array']
def exec(self, chunk):
# Process the chunk and return its sum
chunk_sum = sum(array[start:end])
chunk_sum = sum(chunk)
return chunk_sum
def post(self, shared_storage, prep_result, proc_result):
@@ -32,7 +30,7 @@ class ArrayChunkNode(BatchNode):
return "default"
class SumReduceNode(Node):
def exec(self, shared_storage, data):
def prep(self, shared_storage):
# Get chunk results from shared storage and sum them
chunk_results = shared_storage.get('chunk_results', [])
total = sum(chunk_results)
@@ -48,9 +46,9 @@ class TestBatchNode(unittest.TestCase):
}
chunk_node = ArrayChunkNode(chunk_size=10)
chunks = chunk_node.prep(shared_storage)
self.assertEqual(chunks, [(0, 10), (10, 20), (20, 25)])
chunk_node.run(shared_storage)
results = shared_storage['chunk_results']
self.assertEqual(results, [45, 145, 110])
def test_map_reduce_sum(self):
"""
+5 -5
View File
@@ -2,7 +2,7 @@ import unittest
import sys
from pathlib import Path
sys.path.append(str(Path(__file__).parent.parent))
sys.path.insert(0, str(Path(__file__).parent.parent))
from minillmflow import Node, Flow
class NumberNode(Node):
@@ -10,7 +10,7 @@ class NumberNode(Node):
super().__init__()
self.number = number
def exec(self, shared_storage, data):
def prep(self, shared_storage):
shared_storage['current'] = self.number
class AddNode(Node):
@@ -18,7 +18,7 @@ class AddNode(Node):
super().__init__()
self.number = number
def exec(self, shared_storage, data):
def prep(self, shared_storage):
shared_storage['current'] += self.number
class MultiplyNode(Node):
@@ -26,7 +26,7 @@ class MultiplyNode(Node):
super().__init__()
self.number = number
def exec(self, shared_storage, data):
def prep(self, shared_storage):
shared_storage['current'] *= self.number
class CheckPositiveNode(Node):
@@ -37,7 +37,7 @@ class CheckPositiveNode(Node):
return 'negative'
class NoOpNode(Node):
def exec(self, shared_storage, data):
def prep(self, shared_storage):
# Do nothing, just pass
pass
+5 -4
View File
@@ -2,8 +2,9 @@ import unittest
import asyncio
import sys
from pathlib import Path
sys.path.append(str(Path(__file__).parent.parent))
sys.path.insert(0, str(Path(__file__).parent.parent))
from minillmflow import Node, Flow
# Simple example Nodes
@@ -12,7 +13,7 @@ class NumberNode(Node):
super().__init__()
self.number = number
def exec(self, shared_storage, prep_result):
def prep(self, shared_storage):
shared_storage['current'] = self.number
class AddNode(Node):
@@ -20,7 +21,7 @@ class AddNode(Node):
super().__init__()
self.number = number
def exec(self, shared_storage, prep_result):
def prep(self, shared_storage):
shared_storage['current'] += self.number
class MultiplyNode(Node):
@@ -28,7 +29,7 @@ class MultiplyNode(Node):
super().__init__()
self.number = number
def exec(self, shared_storage, prep_result):
def prep(self, shared_storage):
shared_storage['current'] *= self.number
class TestFlowComposition(unittest.TestCase):