-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtasks.py
More file actions
142 lines (108 loc) · 4.21 KB
/
Copy pathtasks.py
File metadata and controls
142 lines (108 loc) · 4.21 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import importlib.util
import os
import sys
from types import ModuleType
from invoke import Context, Exit, task
_ROOT = os.path.dirname(os.path.abspath(__file__))
PROJECTS = (
'calculator_cli_app',
'fibonacci_sequence',
'fizzbuzz',
'letter_matching_inspection',
'nabeatsu',
)
def _load_application(project: str, *src_subdirs: str) -> ModuleType:
src = os.path.join(_ROOT, project, 'src')
for subdir in ('.', *src_subdirs):
path = os.path.normpath(os.path.join(src, subdir))
if path not in sys.path:
sys.path.insert(0, path)
spec = importlib.util.spec_from_file_location(
f'{project}_application', os.path.join(src, 'application.py'))
assert spec is not None and spec.loader is not None
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
return module
@task(positional=['seed', 'n'])
def run_calculator_cli_app(c: Context, seed: str, n: str) -> None:
"""Run CalculatorCliApp::Application"""
application = _load_application(
'calculator_cli_app', 'queries', 'validations')
application.Application.run(args=[seed, n])
@task
def print_fibonacci_sequence(c: Context) -> None:
"""Print fibonacci sequence"""
application = _load_application('fibonacci_sequence')
print('Provide the initial number to start the sequence from')
init_num = input().strip()
print('Provide the number of iterations')
iterations = input().strip()
params: dict[str, int] = {}
if init_num:
params['init_num'] = int(init_num)
if iterations:
params['iter'] = int(iterations)
print(', '.join(str(num) for num in application.fibonacci(**params)))
@task
def print_fizzbuzz_sequence(c: Context) -> None:
"""Print fizzbuzz sequence"""
application = _load_application('fizzbuzz')
print('Provide the initial number to start the sequence from')
init_num = int(input().strip())
print('Provide the terminal number of stop the sequence at')
terminal_num = int(input().strip())
print('Which logic do you want to use? '
'(1: if, 2: if and ternary, 3: ternary)')
logic_type = int(input().strip())
result: list[str] = []
for num in range(init_num, terminal_num + 1):
if logic_type == 1:
result.append(application.fizzbuzz_in_if(num))
elif logic_type == 2:
result.append(application.fizzbuzz_in_if_and_ternary(num))
elif logic_type == 3:
result.append(application.fizzbuzz_in_ternary(num))
else:
result.append(str(num))
print(', '.join(result))
@task
def print_letter_matching_inspection(c: Context) -> None:
"""Print the result of letter matching inspection"""
application = _load_application('letter_matching_inspection')
print('Provide the source string to compare from')
source = input().strip()
print('Provide the target string to compare with')
target = input().strip()
result = application.Application.exactly_equal_size_and_included(
source=source, target=target)
print(f'Result: {str(result).lower()}')
@task
def print_nabeatsu_sequence(c: Context) -> None:
"""Print nabeatsu sequence"""
application = _load_application('nabeatsu')
print('Provide the initial number to start the sequence from')
init_num = int(input().strip())
print('Provide the terminal number of stop the sequence at')
terminal_num = int(input().strip())
print('Which logic do you want to use? (1: if, 2: ternary)')
logic_type = int(input().strip())
result: list[str] = []
for num in range(init_num, terminal_num + 1):
if logic_type == 1:
result.append(application.go_crazy_in_if(num))
elif logic_type == 2:
result.append(application.go_crazy_in_ternary(num))
else:
result.append(str(num))
print(', '.join(result))
@task(default=True)
def test(c: Context) -> None:
"""Run all tests"""
failed: list[str] = []
for project in PROJECTS:
with c.cd(project):
result = c.run('pytest .', warn=True)
if result is None or not result.ok:
failed.append(project)
if failed:
raise Exit(f'Tests failed in: {", ".join(failed)}', code=1)