-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstring_processing_pipeline.py
More file actions
100 lines (70 loc) · 2.51 KB
/
Copy pathstring_processing_pipeline.py
File metadata and controls
100 lines (70 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import flowbase
async def main():
net = flowbase.Network()
# Initialize components
hisayer = HiSayer()
net.add_process("hisayer", hisayer)
splitter = StringSplitter()
net.add_process("hisayer", splitter)
lowercaser = LowerCaser()
net.add_process("hisayer", lowercaser)
uppercaser = UpperCaser()
net.add_process("hisayer", uppercaser)
stringjoiner = StringJoiner()
net.add_process("hisayer", stringjoiner)
printer = Printer()
net.add_process("hisayer", printer)
# Connect network
splitter.in_lines = hisayer.out_lines
lowercaser.in_lines = splitter.out_leftpart
uppercaser.in_lines = splitter.out_rightpart
stringjoiner.in_leftpart = lowercaser.out_lines
stringjoiner.in_rightpart = uppercaser.out_lines
printer.in_lines = stringjoiner.out_lines
# Run the full event loop
await net.run()
class HiSayer:
out_lines = flowbase.Port()
async def run(self):
for i in range(20):
await self.out_lines.put(f"Hi hi for the {i+1}:th time...")
class StringSplitter:
in_lines = flowbase.Port()
out_leftpart = flowbase.Port()
out_rightpart = flowbase.Port()
async def run(self):
while not self.in_lines.empty():
s = await self.in_lines.get()
await self.out_leftpart.put(s[: int(len(s) / 2)])
await self.out_rightpart.put(s[int(len(s) / 2) :])
class LowerCaser:
in_lines = flowbase.Port()
out_lines = flowbase.Port()
async def run(self):
while not self.in_lines.empty():
s = await self.in_lines.get()
await self.out_lines.put(s.lower())
class UpperCaser:
in_lines = flowbase.Port()
out_lines = flowbase.Port()
async def run(self):
while not self.in_lines.empty():
s = await self.in_lines.get()
await self.out_lines.put(s.upper())
class StringJoiner:
in_leftpart = flowbase.Port()
in_rightpart = flowbase.Port()
out_lines = flowbase.Port()
async def run(self):
while not self.in_leftpart.empty() or not self.in_rightpart.empty():
leftpart = await self.in_leftpart.get()
rightpart = await self.in_rightpart.get()
await self.out_lines.put(f"{leftpart}{rightpart}")
class Printer:
in_lines = flowbase.Port()
async def run(self):
while not self.in_lines.empty():
s = await self.in_lines.get()
print(f"Printer got line: {s}")
if __name__ == "__main__":
flowbase.run(main(), debug=True)