Skip to main content

optmanage

Python versions PyPI version PyPI status Checked with Mypy Documentation Status Python package status

Flexible option managers, supporting options with default values, static type hints, runtime type checking via typing-validation, and custom runtime validation logic: options are declared as class attributes of an OptionManager subclass, and can be set individually, in bulk, or temporarily within a context.

Install

Install the latest release from PyPI:

$ pip install --upgrade optmanage

The library requires Python 3.14 or later.

Usage

Custom option manager classes are created by subclassing OptionManager and using the Option descriptor to define options. Each option takes a type, a default value, and an optional validator function, which signals an invalid value by returning False or raising an exception. Any type supported by typing-validation can be used, including type hints such as Mapping[Literal["x", "y", "z"], float].

from collections.abc import Mapping
from typing import Literal
from optmanage import Option, OptionManager

class MyOptions(OptionManager):
    """Options of some library."""

    validate = Option(bool, True)
    """Whether to validate arguments to functions and methods."""

    eq_atol = Option(float, 1e-8, lambda x: x >= 0)
    """Absolute tolerance used for equality comparisons."""

    scaling = Option(
        Mapping[Literal["x", "y", "z"], float],
        {"x": 1.0, "y": 2.0, "z": 1.0},
        lambda scaling: all(v >= 0 for v in scaling.values()),
    )
    """Scaling for coordinate axes used in plots."""

options = MyOptions()

Options are accessed and set like attributes, or by name. Values of the wrong type raise TypeError, and values rejected by the validator raise ValueError:

print(options.scaling)  # {'x': 1.0, 'y': 2.0, 'z': 1.0}
options.scaling = {"x": 2.5, "y": 1.5, "z": 1.0}
options["eq_atol"] = 1e-6
options.eq_atol = -1.0  # ValueError
options.valdiate = False  # AttributeError: did you mean '.validate'?

Attributes other than options must be declared by the class, for example as annotations, so that misspelt option names are rejected rather than silently creating new attributes.

Several options can be set at once with set, which validates all values before setting any, and all options can be reset to their defaults with reset:

options.set(validate=False, eq_atol=1e-3)
options.reset()
MyOptions.eq_atol.reset(options)  # resets a single option

Calling the option manager gives a context manager which sets options temporarily:

with options(validate=False, eq_atol=1e-3):
    print(options.validate)  # False
print(options.validate)  # True

Inheritance, copying and pickling, and the behaviour of temporary values under concurrency are covered in the getting-started guide.

API

The full API documentation is available at optmanage.readthedocs.io.

Structure

  • src/optmanage/ — the package source; option.py defines the Option descriptor and the Validator protocol, manager.py defines OptionManager.
  • tests/ — the pytest suite.
  • docs/ — Sphinx documentation sources, built with autodoc_typehints.py (see sphinx-docscripts).
  • pypi-upload.py — builds the distribution and uploads it to PyPI.

Contributing

Contribution guidelines are available in CONTRIBUTING.md.

License

LGPL-3.0-or-later © Hashberg Ltd.

Metadata

Release files for optmanage 2.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for optmanage 2.0.0
File Size Uploaded Provenance Actions
optmanage-2.0.0.tar.gz 25.3 kB Without provenance Details for optmanage-2.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for optmanage 2.0.0
File Interpreter ABI Platform Actions
optmanage-2.0.0-py3-none-any.whl Python 3 none any Details for optmanage-2.0.0-py3-none-any.whl

Total release size: 48.8 kB

Release files / optmanage-2.0.0.tar.gz

Download URL optmanage-2.0.0.tar.gz
Size 25.3 kB
Tags Source
SHA-256 checksum
How to use checksums
7113248eb3a32ce22fa2e3aabc68a72d1e88dad5fba35e3b70d6c82a31337d7e
BLAKE2b-256 checksum
How to use checksums
d683424f6ac76d245cc02edd72805598e0fe7f59afc5d73a575ad322106795f2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.15.1

Release files / optmanage-2.0.0-py3-none-any.whl

Download URL optmanage-2.0.0-py3-none-any.whl
Size 23.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b194b9c86257b8608acb1ee9e5f846f2ec65747554e47335e000c6758a6f6a4b
BLAKE2b-256 checksum
How to use checksums
71db5a06bca803d80fde33ca5051ab84ff393269d7bc2cf04cede11df597aa4e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.15.1

Security for optmanage 2.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page