singleton

class utilitools.singleton[source]
A metaclass of Singleton pattern.
Singleton pattern is a software design pattern that restricts the instantiation of a class to one “single” instance.

Tip

A singleton pattern is “acceptable” when it doesn’t affect the execution of the code.
__call__(*args, **kwargs)[source]
Apply a single instance.
Parameters
  • args (Any) –

    Variadic positional arguments will be passed directly to the original class constructor.

  • kwargs (Any) –

    Variadic keyword arguments will be passed directly to the original class constructor.

Returns

An instance of a selected class as a singleton object.

Return type

Any

Examples

import logging
import pathlib
import sys

from utilitools import singleton

class Logger(metaclass=singleton):
    def __init__(self, name, level, stream_handler, file_handler):
        self._logger = logging.getLogger(name)
        self._logger.setLevel(level)
        self._logger.addHandler(stream_handler)
        self._logger.addHandler(file_handler)

    def log(self, level, msg, *args, **kwargs):
        self._logger.log(level, msg, *args, **kwargs)

Create a logger singleton instance:

>>> Logger(
...     name=__name__,
...     level=logging.DEBUG,
...     stream_handler=logging.StreamHandler(sys.stdout),
...     file_handler=logging.FileHandler(pathlib.Path().resolve().joinpath('singleton.log')),
... )

Get the logger instance from anywhere:

>>> logger = Logger()
>>> logger.log(logging.DEBUG, 'A logging message.')