-
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.48 KB
/
Copy pathstring_processing_pipeline.py
File metadata and controls
100 lines (70 loc) · 2.48 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 asyncio
def main():
loop = asyncio.get_event_loop()
# Initialize components
hisayer = HiSayer()
loop.create_task(hisayer.run())
splitter = StringSplitter()
loop.create_task(splitter.run())
lowercaser = LowerCaser()
loop.create_task(lowercaser.run())
uppercaser = UpperCaser()
loop.create_task(uppercaser.run())
stringjoiner = StringJoiner()
loop.create_task(stringjoiner.run())
printer = Printer()
loop.create_task(printer.run())
# 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
loop.run_until_complete(printer.run())
class HiSayer:
out_lines = asyncio.Queue()
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 = asyncio.Queue()
out_leftpart = asyncio.Queue()
out_rightpart = asyncio.Queue()
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 = asyncio.Queue()
out_lines = asyncio.Queue()
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 = asyncio.Queue()
out_lines = asyncio.Queue()
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 = asyncio.Queue()
in_rightpart = asyncio.Queue()
out_lines = asyncio.Queue()
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 = asyncio.Queue()
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__":
main()