Newer
Older
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
Python toolbox that provides a timer and a configuration parser.
## Installation
To install this module run:
```
pip install teklia-toolbox
```
## Timer
Wrapper that calculates the execution time of instructions. This information is stored in the `delta` attribute which is of type [datetime.timedelta](https://docs.python.org/3/library/datetime.html#available-types).
```python
from teklia_toolbox.time import Timer
with Timer() as t:
# Some code
pass
print(f'These instructions took {t.delta}')
```
## Configuration parser
### ConfigParser
The `ConfigParser` class allows to instantiate a parser. It takes as argument:
- a boolean `allow_extra_keys` to specify if the parser should ignore extra unspecified keys instead of causing errors (default to `True`)
#### Add option
The `add_option` function allows to add parameter to the parser. It takes as argument:
- a parameter `name`
- a parameter `type` (which must be [callable](https://docs.python.org/3/library/functions.html#callable)) (default to `str`)
- a `many` boolean to specify if the parameter can have a list of values (default to `False`)
- a `default` value (default to `object()`)
#### Add subparser
The `add_subparser` function adds a parser as a new option to the initial parser, to allow finer control over nested configuration options. It takes the same arguments as the `ConfigParser` class and the `add_option` function.
#### Parse data
The `parse_data` function parses configuration data from a dict. It will raise `ConfigurationError` if any error is detected. Otherwise it returns a dictionary. It takes as argument:
- `data` of type [Mapping](https://docs.python.org/3/library/collections.abc.html#collections.abc.Mapping)
#### Parse
The `parse` function parses configuration data from a yaml file. It will raise `ConfigurationError` if any error is detected. Otherwise it returns a dictionary. It takes as argument:
- a `path` to the yaml file
- a boolean `exist_ok` to specify if the parser should ignore a non-existing file (default to `False`)
```python
from teklia_toolbox.config import ConfigParser
parser = ConfigParser()
parser.add_option('names', type=str, many=True, default=[])
parser.add_option('pseudo', type=str) # Required
parser.add_option('age', type=int, default=21)
parents_parser = parser.add_subparser('parents', default={})
mother_parser = parents_parser.add_subparser('mother', default={})
mother_parser.add_option('name', type=str, default=None)
mother_parser.add_option('age', type=int, default=None)
father_parser = parents_parser.add_subparser('father', default={})
father_parser.add_option('name', type=str, default=None)
father_parser.add_option('age', type=int, default=None)
# This will return
# {
# 'names': ['Pierre', 'Dupont'],
# 'pseudo': 'BoumBoum',
# 'age': 21,
# 'parents': {
# 'mother': {
# 'name': 'Marie',
# 'age': None
# },
# 'father': {
# 'name': None,
# 'age': None
# }
# }
# }
parser.parse_data({
'names': ['Pierre', 'Dupont'],
'pseudo': 'BoumBoum',
'parents': {
'mother': {
'name' : 'Marie'
}
}
})
```
### ConfigurationError
The `ConfigurationError` class inherits from the [ValueError](https://docs.python.org/3/library/exceptions.html#ValueError) class. This type of error is raised if the parser finds errors during parsing.
```python
from teklia_toolbox.config import ConfigurationError
raise ConfigurationError("Oops..")
```
### dir_path and file_path
The `dir_path` and `file_path` functions allow you to easily add path or file parameters to the parser.
```python
from teklia_toolbox.config import ConfigParser
from teklia_toolbox.config import dir_path, file_path
parser = ConfigParser()
parser.add_option('root_path', type=dir_path, default=None)
parser.add_option('csv_file', type=file_path, default=None)
# This will return
# {
# 'root_path': PosixPath('/sweet/home'),
# 'csv_file': PosixPath('/coucou.csv')
# }
parser.parse_data({
'root_path': '/sweet/home/',
'csv_file': './coucou.csv'
})
```