-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpypes-example.py
More file actions
executable file
·82 lines (60 loc) · 1.88 KB
/
Copy pathpypes-example.py
File metadata and controls
executable file
·82 lines (60 loc) · 1.88 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
#!/usr/bin/env python
""" Usage example of the pypes micro-framework. """
from pypes import setup_logger
from pypes import PypeSegment
from pypes import PypeLine
from pypes import wrap_for_next_segment
main_logger = setup_logger('main')
class FirstDemoProcess(PypeSegment):
""" Crafts a string if None given. Else it adds some processing. """
def process(self, stuff=None):
self.log.warn('I work the stuff: %s', stuff)
if not stuff:
return 'Stuff was worked...'
else:
return stuff + 'and processed...'
class SecondDemoProcess(PypeSegment):
""" Adds something to the beginning of the string. """
def process(self, some, more=None):
self.log.warn('%s %s', some, more)
stuff = 'Preprocessed by {} using {}: {}'.format(self, more, some)
return wrap_for_next_segment(stuff=stuff)
combined_process = PypeLine(
[
FirstDemoProcess('one'),
SecondDemoProcess('two'),
FirstDemoProcess('three')
],
name='demopipe'
)
class FailingProcess(PypeSegment):
def check_inputs(self):
""" Does not accept previous... """
failing_process = PypeLine(
[
FailingProcess(),
FailingProcess(),
],
name='demofail',
continue_on_errors=False,
)
def main():
""" Run an example.
>>> main()
<BLANKLINE>
==== Simple Processing ====
<BLANKLINE>
...
"""
print('\n==== Simple Processing ====\n')
result = combined_process.process()
main_logger.success("First result: %s", result)
print('\n==== Fails expected ====\n')
try:
failing_process.process()
except TypeError as ex:
main_logger.success("Error was expected: %r" % (ex))
print('\n==== continue_on_errors ====\n')
failing_process.continue_on_errors = True
failing_process.process()
if __name__ == '__main__': main()