Bases and utilities for compatibility, features and validation.


License
MIT
Install
pip install basicco==8.10.0

Documentation

basicco

image

image

image

image

image

image

image

Overview

Base Classes and Utilities for compatibility, features and validation.

Motivation

While developing Python software for Visual Effects pipelines, I found myself having to write the same boiler-plate code over and over again, as well as struggling with compatibility issues and feature gaps between Python 2.7 and Python 3.7+.

So I decided to implement solutions for those issues at the Base, and basicco was born.

Base Classes

CompatBase

The goal with the CompatBaseMeta metaclass and the CompatBase class is to bridge some of the feature gaps between Python 2.7 and Python 3.7+.

This includes adding Python 2.7 workarounds for:

Base

In addition to the compatibility solutions, the goal with the BaseMeta metaclass and the Base class is to add useful low-level features that hopefully yield better code readability and validation.

This includes:
  • __weakref__ slot: Added by default.
  • locked_class: Public class attributes are read-only by default.
  • explicit_hash: Overriding __eq__ without overriding __hash__ will error.
  • namespace: Adds a protected __namespace unique to each class.
  • runtime_final: Runtime checking for classes and methods decorated with final.

SlottedBase

The SlottedBase class and the SlottedBaseMeta metaclass offer all features from Base and BaseMeta plus implicit __slots__ declaration. See slotted for more information.

Utilities

Apart from the features integrated into the base classes, basicco provides many general utility modules.

abstract_class

Better support for abstract classes.

Provides abstract decorators that can be used directly on methods but also on property getters, classmethods, and staticmethods (even in Python 2.7).

caller_module

Retrieve the caller's module name.

context_vars

Backport of the contextvars module for Python 2.7, based on MagicStack/contextvars.

When imported from Python 3, it simply redirects to the native contextvars module.

custom_repr

Custom representation functions for mappings, items, and iterables.

default_dir

Backport of Python 3's implementation of object.__dir__.

This allows for calling super().__dir__() from a subclass to leverage the default implementation.

descriptors

Configurable descriptors.

dynamic_class

Easily generate classes on the fly. This works best with a Base class. If provided a valid qualified name and module (uses caller_module by default), the class will be pickable/importable.

dynamic_code

Generate debuggable code on the fly that supports line numbers on tracebacks.

explicit_hash

Metaclass that forces __hash__ to be declared whenever __eq__ is declared.

fabricate_value

Run a value through a callable factory (or None).

func_tools

Backport of functools.cache, functools.lru_cache, and functools.update_wrapper for Python 2.7.

get_mro

Get consistent MRO amongst different python versions. This works even with generic classes in Python 2.7.

hash_cache_wrapper

An integer subclass that pickles/copies as None. This can be used to avoid serializing a cached hash value.

implicit_hash

Metaclass that forces __hash__ to None when __eq__ is declared. This is a backport of the default behavior in Python 3.

import_path

Generate importable dot paths and import from them.

init_subclass

Backport of the functionality of __init_subclass__ from PEP 487 to Python 2.7. This works for both Python 2 (using __kwargs__) and 3 (using the new class parameters).

lazy_tuple

Lazily-evaluated tuple-like structure.

locked_class

Prevents changing public class attributes.

mangling

Functions to mangle/unmangle/extract private names.

mapping_proxy

Mapping Proxy type (read-only dictionary) for older Python versions.

namespace

Wraps a dictionary/mapping and provides attribute-style access to it.

Also provides a NamespacedMeta metaclass that adds a __namespace protected class attribute that is unique to each class.

null_context

Backport of contextlib.nullcontext for Python 2.7.

obj_state

Get/update the state of an object, slotted or not (works even in Python 2.7).

Also provides a ReducibleMeta metaclass that allows for pickling instances of slotted classes in Python 2.7.

qualname

Python 2.7 compatible way of getting the qualified name. Based on wbolster/qualname. Also provides a QualnamedMeta metaclass with a __qualname__ class property for Python 2.7.

recursive_repr

Decorator that prevents infinite recursion for __repr__ methods.

runtime_final

Runtime-checked version of the typing.final decorator.

Can be used on methods, properties, classmethods, staticmethods, and classes that have RuntimeFinalMeta as a metaclass. It is also recognized by static type checkers and prevents subclassing and/or member overriding during runtime:

safe_not_equals

Backport of the default Python 3 behavior of __ne__ behavior for Python 2.7.

safe_repr

Decorator that prevents __repr__ methods from raising exceptions and return a default representation instead.

sentinel

Easily define singleton sentinel values and their type (for type hinting).

set_name

Backport of the functionality of __set_name__ from PEP 487 to Python 2.7.

suppress_exception

Backport of contextlib.suppress for Python 2.7. See null_context for an example usage.

type_checking

Runtime type checking with support for import paths and type hints.

unique_iterator

Iterator that yields unique values.