Skip to main content
Python versions PyPI version PyPI status Checked with Mypy Documentation Status Python package status standard-readme compliant

Flexible option managers, supporting options with default values, static type hints, runtime type checking, and custom runtime validation logic.

Install

You can install the latest release from PyPI as follows:

$ pip install --upgrade optmanage

Usage

Custom option manager classes can be created by subclassing OptionManager and using the Option descriptor to set options. An option manager object can then be obtained by instantiating the option manager class:

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()

Each option takes a default value, a type, and an optional validator function:

validate = Option(bool, True)
#     option type ^^^^  ^^^^ default value

eq_atol = Option(float, 1e-8, lambda x: x >= 0)
#           optional validator ^^^^^^^^^^^^^^^^

Any type supported by the typing-validation library can be used for options, including PEP 484 type hints:

scaling: Option(
    Mapping[Literal["x", "y", "z"], float], # <- type hints supported
    {"x": 1.0, "y": 2.0, "z": 1.0},
    lambda scaling: all(v >= 0 for v in scaling.values())
)

Options can be accessed and set like attributes of the options object:

print(options.scaling)  # {'x': 1.0, 'y': 2.0, 'z': 1.0}
options.scaling = {"x": 2.5, "y": 1.5, "z": 1.0}
print(options.scaling) # {'x': 2.5, 'y': 1.5, 'z': 1.0}

It is possible to set multiple options simultaneously using the set method of the options object:

options.set(validate=False, eq_atol=1e-3)
print(options.validate) # False
print(options.eq_atol)  # 0.001

It is also possible to use the options object as a context manager, for temporary option setting:

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

All options can be reset to their default values by using the OptionManager.reset method of the options object:

options.set(validate=False, eq_atol=1e-3)
print(options.validate) # False
print(options.eq_atol)  # 0.001
options.reset()
print(options.validate) # True
print(options.eq_atol)  # 0.00000001

An individual option can be reset to its default value by using the Option.reset method of the Option object, accessed from the option manager class:

options.set(validate=False, eq_atol=1e-3)
print(options.validate) # False
print(options.eq_atol)  # 0.001
MyOptions.eq_atol.reset(options) # resets 'eq_atol' on the 'options' object
print(options.validate) # True
print(options.eq_atol)  # 0.001

API

Full documentation is available at https://optmanage.readthedocs.io/

Contributing

This project is currently in private development. Public contribution guidelines are available at CONTRIBUTING.md.

License

MIT © Hashberg Ltd.

Metadata

Release files for optmanage 1.1.1

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 1.1.1
File Size Uploaded
optmanage-1.1.1.tar.gz 766.5 kB Details

Built distribution (wheel)

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

Total release size: 775.6 kB

Release files / optmanage-1.1.1.tar.gz

Download URL optmanage-1.1.1.tar.gz
Size 766.5 kB
Tags Source
SHA-256 checksum
How to use checksums
f92ffec120ab67f905d564c6e89ba09b125f19657e41347dd10bd79662d2a50c
BLAKE2b-256 checksum
How to use checksums
e3ce562acfea90bd98108664f44e7d45b21dcbd5043aba22f85e0e78e2081e82
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.12.0

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

Download URL optmanage-1.1.1-py3-none-any.whl
Size 9.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
bca925868f022bfd4cd610088d609c72d18b0f13943b2f5edba3bb0540a23dc7
BLAKE2b-256 checksum
How to use checksums
bbb38500321d8d4d2471bfc84f3069c2aaa11ea217ad2dca7682afe73683df2a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.12.0

Release history Release notifications | RSS feed

This release

1.1.1 This release

2 release files

1.1.0

2 release files

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