Skip to main content

Custom library.

Project description

customlib

A few tools for day to day work.


Installation:

python -m pip install [--upgrade] customlib

Available tools:

CfgParser

# module main.py

from os.path import dirname
from os.path import join
from sys import modules

from customlib.config import get_config

# main module:
MODULE = modules.get("__main__")

# root directory:
ROOT: str = dirname(MODULE.__file__)

# default config file path:
CONFIG: str = join(ROOT, "config", "customlib.ini")

# backup config params:
BACKUP: dict = {
    "SECTION": {
        "option_1": "one",
        "option_2": 2,
        # extended interpolation (refer to `ConfigParser` documentation):
        "option_3": r"${DEFAULT:directory}\value",
    },
}

# by passing a value to `name` param,
# we can have more named instances:
cfg = get_config(name="root")

# we can set default section options:
cfg.set_defaults(directory=ROOT)

# we can provide a backup dictionary
# in case our config file does not exist
# and a new one will be created
cfg.open(file_path=CONFIG, encoding="UTF-8", fallback=BACKUP)

# we're parsing cmd-line arguments
cfg.parse()

# we can also do this...
# cfg.parse(["--section-option_1", "one", "--section-option_2", "2"])

To pass cmd-line arguments:

  > python -O main.py --section-option value --section-option value

cmd-line args have priority over config file and will override the cfg params.

Because it inherits from ConfigParser and with the help of our converters we now have four extra methods to use in our advantage.

some_list = cfg.getlist("SECTION", "option")
some_tuple = cfg.gettuple("SECTION", "option")
some_set = cfg.getset("SECTION", "option")
some_dict = cfg.getdict("SECTION", "option")

The configuration files are read & written using FileHandler (see customlib.filehandlers), a custom context-manager with thread & file locking abilities.

Logger

from os.path import dirname, realpath, join
from sys import modules

from customlib.logging import get_logger, Logger
from customlib.config import get_config, CfgParser

# setting configuration (for example):
MODULE = modules.get("__main__")
ROOT: str = dirname(realpath(MODULE.__file__))
BACKUP: dict = {
    "LOGGER": {
        "basename": "customlib",  # if handler is `file`
        "folder": r"${DEFAULT:directory}\logs",
        "handler": "console",  # or `file`
        "debug": False,  # if set to `True` it will also print `DEBUG` messages
    }
}

cfg: CfgParser = get_config(name="my_config")
cfg.set_defaults(directory=ROOT)
cfg.read_dict(dictionary=BACKUP, source="<logging>")

log: Logger = get_logger(name="my_logger", config=cfg)
# we can pass a config instance

or

from customlib.logging import get_logger, Logger

log: Logger = get_logger(name="my_logger", config="my_config")
# it will look for a config instance named `my_config`

or

from customlib.logging import get_logger, Logger

log: Logger = get_logger(name="my_logger", basename="customlib", handler="file", debug=True)
# it will create and use its own config instance


log.debug("Testing debug messages...")
log.info("Testing info messages...")
log.warning("Testing warning messages...")
log.error("Testing error messages...")
log.critical("Testing critical messages...")

By default, debugging is set to False and must be enabled to work. See CfgParser section for this.

The log file is prefixed with a date and will have an index number attached before the extension (ex: 2022-08-01_customlib.1.log). When it reaches 1 Mb the file handler will switch to another file by incrementing its index with 1.

The folder tree is by default structured as follows:

.
└───logs
    └───year (ex: 2022)
        └───month (ex: january)
            ├───2022-08-01_customlib.1.log
            ├───2022-08-01_customlib.2.log
            └───2022-08-01_customlib.3.log

When the current month changes, a new folder is created and the previous one is archived.


NOTE:

Documentation is not complete...

More tools to be added soon...

Work in progress...


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

customlib-5.0.4.tar.gz (28.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

customlib-5.0.4-py3-none-any.whl (36.7 kB view details)

Uploaded Python 3

File details

Details for the file customlib-5.0.4.tar.gz.

File metadata

  • Download URL: customlib-5.0.4.tar.gz
  • Upload date:
  • Size: 28.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.5

File hashes

Hashes for customlib-5.0.4.tar.gz
Algorithm Hash digest
SHA256 4145b800d33d65b8bfe6b9634b421260f7a0732d5366ad4123779f2affeb7ba3
MD5 d431d6adb93d5173f8339689fa045d31
BLAKE2b-256 a07941ef80b622bafaa92265a40e92a48b5216a44cb0153859eea028210ab62c

See more details on using hashes here.

File details

Details for the file customlib-5.0.4-py3-none-any.whl.

File metadata

  • Download URL: customlib-5.0.4-py3-none-any.whl
  • Upload date:
  • Size: 36.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.5

File hashes

Hashes for customlib-5.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 25a1bc6c456e649f291803ee5e17e5493d5fd7bcb7a70f9c037c34928131a21a
MD5 7f99203dcaa229ef9385e19b0cf489ca
BLAKE2b-256 3c0419d91a6bd8a9fc5c0ca1917ac2c1415fc1cadecef087e73e02d010810ece

See more details on using hashes here.

Supported by

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