-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtraining.py
108 lines (84 loc) · 3.34 KB
/
training.py
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
"""My training scripts
I use this project to practice on Python 3.8.
Author: Farzad FARID
References:
- Python 3.8 tutorial
- Python 3.8 library
- Python 3.8 reference
Copyright 2020 Farzad FARID
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import sys
import argparse
import exercices
VERSION = '1.0'
def get_modules():
"""Return the list of all exercice modules with a short description"""
# Extract functions whose name does not start with '_' from module
functions = filter(
lambda sym: type(getattr(exercices, sym)) == type(main) and not sym.startswith('_'),
dir(exercices)
)
functions = list(functions)
# Do the same with list comprehension
functions2 = [
sym for sym in dir(exercices)
if type(getattr(exercices, sym)) == type(main) and not sym.startswith('_')
]
assert functions == functions2
modules = {}
for func_name in functions:
f = getattr(exercices, func_name) # Function object
name = f.__name__ # Function name
doc = (f.__doc__.strip() or "NO DOCUMENTATION").splitlines()[0] # First line of function documentation
modules[name] = doc
return modules
def main():
"""The main program calls the training sessions
This function uses Python's introspection in order to extract the list of
functions from the "exercises" module.
"""
parser = argparse.ArgumentParser(prog="python3-training",
description="My Python 3 training sessions")
parser.add_argument('module', nargs='?', default=None,
help='Execute only one module by name')
parser.add_argument('-l', '--list', action='store_true',
help='Display list of modules')
parser.add_argument('--version', action='version', version=f'{parser.prog} {VERSION}')
args = parser.parse_args()
# Basic argument parsing
if args.list:
modules = get_modules()
for name in modules:
print(f"{name:<15} -- {modules[name]}")
sys.exit(1)
modules = get_modules()
if args.module:
try:
modules = {args.module: modules[args.module]} # Reduce dict to one element
except KeyError:
sys.stderr.write(
f"Error: Unknown module {args.module}. Check module list with '{parser.prog} --list'.\n")
sys.exit(1)
print("List of exercises:")
for name in modules:
f = getattr(exercices, name) # Function object
title = name.capitalize() # Function name
doc = modules[name]
title_length = len(title) + len(doc)
print()
print("+" + "-" * (title_length + 4) + "+")
print(f"| {title}: {doc} |")
print("+" + "-" * (title_length + 4) + "+")
print()
f() # Let exceptions happen
if __name__ == '__main__':
main()