Skip to main content

Basic functionality for Python projects: logging, base class, and validation utilities.

Project description

pybase

Basic functionality for Python projects: logging setup, a Base class with built-in logger, validation utilities, and exception logging helpers.

Installation

pip install visionscaper-pybase

Logging

Setup

Call setup_logging() at application startup to configure the root logger:

from basics.logging import setup_logging, get_logger

setup_logging()

logger = get_logger('my_module')
logger.info('Application started')

Output:

20260209-151557.123 INFO: my_module::<module>: Application started

Getting a Logger

Use get_logger() to create named loggers. The default log level is DEBUG, but can be overridden:

from basics.logging import get_logger

# Module-level logger (common pattern)
module_logger = get_logger(__name__)

# Logger with custom log level
import logging
quiet_logger = get_logger('noisy_lib', log_level=logging.WARNING)

Log Format

The default format includes a timestamp prefix:

YYYYMMDD-HHMMSS.msecs LEVEL: name::funcName: message

Example:

20260209-151557.123 INFO: train::main: Starting training
20260209-151557.456 WARNING: train::main: Low memory

Environment Variables

Variable Default Description
PYBASE_LOG_PID unset Set to 1 to prefix log lines with the process ID ([PID])
PYBASE_NO_TIMESTAMP_LOG unset Set to 1 to disable the timestamp prefix
PYBASE_SUPPRESS_LEGACY_WARNINGS unset Set to 1 to suppress the deprecation warning about auto-configuration

Example with PID enabled:

[4521] 20260209-151557.123 INFO: train::main: Starting training

Custom Format

You can pass a custom format string and date format to setup_logging():

from basics.logging import setup_logging

setup_logging(
    format_string='%(asctime)s %(levelname)s %(message)s',
    datefmt='%Y-%m-%d %H:%M:%S',
)

Advanced: Building Format Strings

Use build_logging_format() to programmatically construct format strings with optional timestamp and PID, independent of environment variables:

from basics.logging import build_logging_format

fmt = build_logging_format(include_timestamp=True, include_pid=True)

Legacy Compatibility

Importing basics.logging currently configures the root logger automatically via logging.basicConfig(). This ensures existing code continues to work, but will be removed in a future version. New code should call setup_logging() explicitly.

Set PYBASE_SUPPRESS_LEGACY_WARNINGS=1 to suppress the deprecation warning.

Base Class

The Base class provides a built-in logger for any class that inherits from it:

from basics.base import Base

class MyProcessor(Base):

    def __init__(self, name, **kwargs):
        super().__init__(**kwargs)
        self._log.info(f'Processor created: {name}')

    def process(self, data):
        self._log.debug(f'Processing {len(data)} items')
        return [item * 2 for item in data]

proc = MyProcessor('example')
proc.process([1, 2, 3])

Output:

20260209-151557.123 INFO: MyProcessor::__init__: Processor created: example
20260209-151557.124 DEBUG: MyProcessor::process: Processing 3 items

The logger name defaults to the class name. Override it with pybase_logger_name:

proc = MyProcessor('example', pybase_logger_name='custom_name')
# Logs will show: custom_name::__init__: ...

Customizing the Logger

Subclasses can override _pybase_get_logger_name() to customize the logger name dynamically, or _get_logger() to use a different logger implementation entirely:

class MyProcessor(Base):

    def __init__(self, name, **kwargs):
        self._name = name
        super().__init__(**kwargs)

    def _pybase_get_logger_name(self):
        return f'MyProcessor[{self._name}]'

Exception Logging

Use log_exception() to log exceptions with their type and traceback through the logging system:

from basics.logging import get_logger
from basics.logging_utils import log_exception

logger = get_logger(__name__)

try:
    result = 1 / 0
except Exception as e:
    log_exception(logger, 'Division failed', e)

Validation Utilities

Type-checking helpers available from basics.base_utils or basics.validation_utils:

from basics.base_utils import is_number, is_dict, is_sequence, is_callable

is_number(42)           # True
is_dict({'a': 1})       # True
is_sequence([1, 2, 3])  # True
is_callable(print)      # True
Function Description
is_number(n) Instance of numbers.Number
is_mapping(d) Instance of collections.abc.Mapping
is_dict(d) Instance of dict
is_bool(d) Instance of bool
is_class(c) Instance of type
is_sequence(l) Instance of collections.abc.Sequence
is_non_empty_string(s) str with length > 0
is_file(fname) os.path.isfile() check
is_callable(func) callable() check
is_int_sequence(seq) Sequence where all elements are numbers
sequences_are_compatible(a, b) Two sequences with equal length

License

MIT License. See LICENSE for details.

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

visionscaper_pybase-0.4.0.tar.gz (6.1 kB view details)

Uploaded Source

Built Distribution

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

visionscaper_pybase-0.4.0-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

File details

Details for the file visionscaper_pybase-0.4.0.tar.gz.

File metadata

  • Download URL: visionscaper_pybase-0.4.0.tar.gz
  • Upload date:
  • Size: 6.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.11

File hashes

Hashes for visionscaper_pybase-0.4.0.tar.gz
Algorithm Hash digest
SHA256 2deeaa72cf4106a704cf78b2b62714635746b9d5f56f41c51f1e796a43d69fcd
MD5 e8f3dd741cc2a26781dc2c2ea23d91d9
BLAKE2b-256 be1b975a900ab7de31ca4166e735dfc1c256933ce0e57d8b414aeb089a2c59bc

See more details on using hashes here.

File details

Details for the file visionscaper_pybase-0.4.0-py3-none-any.whl.

File metadata

File hashes

Hashes for visionscaper_pybase-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 db22e0bbe2f3d46e23d4a38fa6f9a03d4a9b787084f6fc8bc624c8f67363c1ff
MD5 ac0d19aa95ccb81a53612741685fff67
BLAKE2b-256 3e23bd29aef3a222083a4921fdf39f5c87c39aa993ec151e0dba63bcd9a3535e

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