Python Toolbox
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.
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_keysto specify if the parser should ignore extra unspecified keys instead of causing errors (default toTrue)
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) (default tostr) - a
manyboolean to specify if the parameter can have a list of values (default toFalse) - a
defaultvalue (default toobject())
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:
dataof type 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
pathto the yaml file - a boolean
exist_okto specify if the parser should ignore a non-existing file (default toFalse)
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 class. This type of error is raised if the parser finds errors during parsing.
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.
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'
})
Release files for teklia-toolbox 0.3.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| teklia_toolbox-0.3.3.tar.gz | 12.1 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| teklia_toolbox-0.3.3-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 24.4 kB
Release files / teklia_toolbox-0.3.3.tar.gz
| Download URL | teklia_toolbox-0.3.3.tar.gz |
|---|---|
| Size | 12.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
dde18a49889934d3db44b629b836bf5c7cfa12ce074e44af7e5fa0628c37bb23
|
|
BLAKE2b-256 checksum How to use checksums |
375b0d1bb20f5c0c4228632169aeb54efb8b9b0bb5ca3179136f5152ed4d627e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.14.7
|
Release files / teklia_toolbox-0.3.3-py3-none-any.whl
| Download URL | teklia_toolbox-0.3.3-py3-none-any.whl |
|---|---|
| Size | 12.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
17221e30be421165c65b23f4939f81d57309898b29ce51b4dc61103674010597
|
|
BLAKE2b-256 checksum How to use checksums |
60ebad7a5e8fd6e2adf21a5d4e17b78363616dfe2d50d8fc59940f4efa22faae
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.14.7
|