Skip to main content

A library to create flexible option managers.

Project description

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(True, bool)
    """ Whether to validate arguments to functions and methods. """

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

    scaling: Option(
        {"x": 1.0, "y": 2.0, "z": 1.0},
        Mapping[Literal["x", "y", "z"], float],
        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(True, bool)
#   default value ^^^^  ^^^^ option type

eq_atol = Option(1e-8, float, 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(
    {"x": 1.0, "y": 2.0, "z": 1.0},
    Mapping[Literal["x", "y", "z"], float], # <- type hints supported
    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.

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

optmanage-1.0.0.post3.tar.gz (766.4 kB view details)

Uploaded Source

Built Distribution

optmanage-1.0.0.post3-py3-none-any.whl (9.1 kB view details)

Uploaded Python 3

File details

Details for the file optmanage-1.0.0.post3.tar.gz.

File metadata

  • Download URL: optmanage-1.0.0.post3.tar.gz
  • Upload date:
  • Size: 766.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.12.0

File hashes

Hashes for optmanage-1.0.0.post3.tar.gz
Algorithm Hash digest
SHA256 2d0b86f281801c14b258d2170bd9e523f08687d921f6deb8f82e330d996f383c
MD5 3c20172897147309b7197a4a0035d86a
BLAKE2b-256 cdcca1ee27865ab1f94a8d2642e8229529a04eec5b05c613e3ae6ee1cb51d9be

See more details on using hashes here.

File details

Details for the file optmanage-1.0.0.post3-py3-none-any.whl.

File metadata

File hashes

Hashes for optmanage-1.0.0.post3-py3-none-any.whl
Algorithm Hash digest
SHA256 f5d1b4286f03aec9c18fc1a68a21103065978fe72c6aff37d3405004085fc3b5
MD5 8b57b253db74027e0eace3bc26260c48
BLAKE2b-256 e09289c3adcec3db73db1b5c1c8a70b7ffe2d96f4d16fed8d0abb3fc28a90fd8

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page