Skip to main content

No project description provided

Project description

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_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) (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:

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)
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'
})

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

teklia_toolbox-0.1.8.tar.gz (12.2 kB view details)

Uploaded Source

Built Distribution

teklia_toolbox-0.1.8-py3-none-any.whl (12.3 kB view details)

Uploaded Python 3

File details

Details for the file teklia_toolbox-0.1.8.tar.gz.

File metadata

  • Download URL: teklia_toolbox-0.1.8.tar.gz
  • Upload date:
  • Size: 12.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for teklia_toolbox-0.1.8.tar.gz
Algorithm Hash digest
SHA256 13b491ee4724bd9ec23b7784af4f6c8c701a08a44b2963ec76fbb2632d0456f8
MD5 589ec3eadb2caa7220c0967aed5aea11
BLAKE2b-256 92065c8c0a134baf9b2820b8fdda219d00416b06b6bebffd1f603fcfad847deb

See more details on using hashes here.

File details

Details for the file teklia_toolbox-0.1.8-py3-none-any.whl.

File metadata

File hashes

Hashes for teklia_toolbox-0.1.8-py3-none-any.whl
Algorithm Hash digest
SHA256 671cc16205af3887f5503886af0f6a14928a77e045f31863ea33918769c67bb4
MD5 9802436052d602d18f4c20a8c6bf256b
BLAKE2b-256 05010bebec02b517aac26379ff5a015ca9baed57e60958388a5e4a97e4294fc1

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page