Skip to main content

Small boiler plate with tools for multithreading.

Project description

minibone

Check Deploy PyPI version

minbone is a small boiler plate with tools for multithreading and others.

  • Config: To handle configuration setting
  • Daemon: For multithreading tasks
  • Logging: To setup a logger friendly with filerotation
  • Among others (I will add more later)

It will be deployed to PyPi when a new release is created

Installation

pip install minibone

Config

Allows to handle configuration settings in memory and/or persists them into toml/yaml/json formats

from minibone.config import Config

# Create a new set of settings and persists them
cfg = Config(settings={"listen": "localhost", "port": 80}, filepath="config.toml")	
cfg.add("debug", True)	
cfg.to_toml()

# Load settings from a file. Defaults can be set. More information: help(Config.from_toml)
cfg2 = Config.from_toml("config.toml")

Usually config files are editted externaly then loaded as read only on your code, so in such case, you may want to subclass Config for easier usage

from minibone.config import Config

class MyConfig(Config):

    def __init__(self):
        defaults = {"main": {"listen": "localhost", "port": 80}}
        settings = Config.from_toml(filepath="config.toml", defaults=defaults)
        super().__init__(settings=settings)

    def listen(self) -> str:
        return self["main"]["listen"]

    def port(self) -> int:
        return self["main"]["port"]

if __name__ == "__main__":
    cfg = MyConfig()
    print(cfg.port())
    # it will print the default port value if not port setting was defined in config.toml

Logging

It allows to setup a logger using UTC time that outputs to stderr or to a file. It is friendly to filerotation when setting output to a file

import logging

from minibone.logging import setup_log

if __name__ == "__main__":

    # setup_log must be called only once in your code.
    # So you have to choice if logging to stderr or to a file when calling it

    setup_log(level="INFO")
    logging.info('This is a log to the stderr')

    # or call the next lines instead if you want to log into a file
    # setup_log(file="sample.log", level="INFO")
    # logging.info('yay!')

Daemon

It is just another python class to do jobs / tasks in the background using threads. It can be used in two modes: subclasing and callback

Usage as SubClass mode

  • Subclass Daemon
  • call super().init() in yours
  • Overwrite on_process method with yours
  • Add logic you want to run inside on_process
  • Be sure your methods are safe-thread to avoid race condition
  • self.lock is available for { lock.acquire => your_logic => lock.release }
  • call start() method to keep running on_process in a new thread
  • call stop() to finish the thread

Check sample_clock.py for a sample

Usage as callback mode

  • Instance Daemon by passing a callable
  • Add logic to your callable method
  • Be sure your callable and methods are safe-thread to avoid race condition
  • call start() method to keep running callable in a new thread
  • call stop() to finish the thread

Check sample_clock_callback.py for a sample

Contribution

  • Feel free to clone this repository, and send any pull requests.
  • Add issues if something is not working as expected.

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

minibone-0.2.0.tar.gz (44.8 kB view details)

Uploaded Source

Built Distribution

minibone-0.2.0-py3-none-any.whl (32.3 kB view details)

Uploaded Python 3

File details

Details for the file minibone-0.2.0.tar.gz.

File metadata

  • Download URL: minibone-0.2.0.tar.gz
  • Upload date:
  • Size: 44.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for minibone-0.2.0.tar.gz
Algorithm Hash digest
SHA256 45b16919884105e07a76f81f12c8a80276bbab05281111ecb21dae5bf8e8cd6e
MD5 4a1573d6c6556e918491df53425dba68
BLAKE2b-256 08cc00560bb1b2425f8c71d6f78203bba3f92af9fc655bc687bbbebfb437c4ad

See more details on using hashes here.

File details

Details for the file minibone-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: minibone-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 32.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for minibone-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b80d95fa9cedac5674b434da40a6c82e0ec9512ba73009eb3e8b7eeeffc79f90
MD5 892633d2901cb40c639477a3b38f4a06
BLAKE2b-256 489936b5f5ddb5745224983c3bbb7ef61adfcff3804491d1a4d44bc991109b2b

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 Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page