optmanage 1.0.0.post0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- optmanage/__init__.py +20 -0
- optmanage/manager.py +227 -0
- optmanage/option.py +269 -0
- optmanage/py.typed +0 -0
- optmanage-1.0.0.post0.dist-info/LICENSE +21 -0
- optmanage-1.0.0.post0.dist-info/METADATA +107 -0
- optmanage-1.0.0.post0.dist-info/RECORD +9 -0
- optmanage-1.0.0.post0.dist-info/WHEEL +5 -0
- optmanage-1.0.0.post0.dist-info/top_level.txt +1 -0
optmanage/__init__.py
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"""
|
|
2
|
+
A library that can be used to implement flexible option managers,
|
|
3
|
+
supporting options with default values, static type hints, runtime type
|
|
4
|
+
checking, and custom runtime validation logic.
|
|
5
|
+
|
|
6
|
+
See :class:`~optmanage.manager.OptionManager` for full details.
|
|
7
|
+
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
# A library to create flexible option managers.
|
|
11
|
+
# Copyright (C) 2023 Hashberg Ltd
|
|
12
|
+
|
|
13
|
+
from __future__ import annotations
|
|
14
|
+
|
|
15
|
+
__version__ = "1.0.0"
|
|
16
|
+
|
|
17
|
+
from .option import Option
|
|
18
|
+
from .manager import OptionManager
|
|
19
|
+
|
|
20
|
+
__all__ = ("Option", "OptionManager")
|
optmanage/manager.py
ADDED
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Base class for option managers.
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
# OptManage: A library to create flexible option managers.
|
|
6
|
+
# Copyright (C) 2023 Hashberg Ltd
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
from collections.abc import Iterator, Mapping
|
|
11
|
+
from contextlib import contextmanager
|
|
12
|
+
from typing import Any, ClassVar, Type
|
|
13
|
+
from .option import Option
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class OptionManager:
|
|
17
|
+
"""
|
|
18
|
+
A base class that can be used to implement flexible option managers,
|
|
19
|
+
supporting options with default values, static type hints, runtime type
|
|
20
|
+
checking, and custom runtime validation logic.
|
|
21
|
+
|
|
22
|
+
Subclasses should define options as class attributes,
|
|
23
|
+
using the :class:`Option` descriptor.
|
|
24
|
+
|
|
25
|
+
A simple example of an option manager:
|
|
26
|
+
|
|
27
|
+
.. code-block:: python
|
|
28
|
+
|
|
29
|
+
class MyOptions(OptionManager):
|
|
30
|
+
''' Options of some library. '''
|
|
31
|
+
|
|
32
|
+
validate = Option(True, bool)
|
|
33
|
+
''' Whether to validate arguments to functions and methods. '''
|
|
34
|
+
|
|
35
|
+
eq_atol = Option(1e-08, float, lambda x: x >= 0)
|
|
36
|
+
''' Absolute tolerance used for equality comparisons.'''
|
|
37
|
+
|
|
38
|
+
print_prec = Option(3, int, lambda x: x >= 0)
|
|
39
|
+
''' Number of decimal digits to be displayed when printing. '''
|
|
40
|
+
|
|
41
|
+
Each option is defined as a class attribute of type :class:`Option`,
|
|
42
|
+
passing a default value, a type, and optionally a validator function:
|
|
43
|
+
|
|
44
|
+
.. code-block:: python
|
|
45
|
+
|
|
46
|
+
validate = Option(True, bool)
|
|
47
|
+
# default value ^^^^ ^^^^ option type
|
|
48
|
+
|
|
49
|
+
print_prec = Option(3, int, lambda x: x >= 0)
|
|
50
|
+
# optional validator ^^^^^^^^^^^^^^^^
|
|
51
|
+
|
|
52
|
+
The option manager can then be instantiated as usual:
|
|
53
|
+
|
|
54
|
+
.. code-block:: python
|
|
55
|
+
|
|
56
|
+
options = MyOptions()
|
|
57
|
+
|
|
58
|
+
"""
|
|
59
|
+
|
|
60
|
+
__options: ClassVar[dict[Type[OptionManager], dict[str, Option[Any]]]] = {}
|
|
61
|
+
|
|
62
|
+
def __init_subclass__(cls) -> None:
|
|
63
|
+
"""
|
|
64
|
+
Hook to create the initial options mapping for a subclass of
|
|
65
|
+
:class:`OptionManager`, by collecting all options from all
|
|
66
|
+
superclasses of ``cls`` which are themselves subclasses of
|
|
67
|
+
:class:`OptionManager`.
|
|
68
|
+
|
|
69
|
+
Options declared in ``cls`` are bound subsequently.
|
|
70
|
+
|
|
71
|
+
:raises KeyError: If a duplicate option name is found.
|
|
72
|
+
"""
|
|
73
|
+
__options = OptionManager.__options
|
|
74
|
+
mro_options_list = [
|
|
75
|
+
__options.get(cls, {}),
|
|
76
|
+
*(__options[base] for base in cls.__mro__[1:] if base in __options),
|
|
77
|
+
]
|
|
78
|
+
merged_options: dict[str, Option[Any]] = {}
|
|
79
|
+
for options in reversed(mro_options_list):
|
|
80
|
+
for k, opt in options.items():
|
|
81
|
+
prev_opt = merged_options.get(k, None)
|
|
82
|
+
if prev_opt is None:
|
|
83
|
+
merged_options[k] = opt
|
|
84
|
+
elif prev_opt is not opt:
|
|
85
|
+
error = (
|
|
86
|
+
f"Duplicate option name {k!r} for option managers "
|
|
87
|
+
f"{prev_opt.owner.__name__} and "
|
|
88
|
+
f"{opt.owner.__name__} in MRO of {cls.__name__}."
|
|
89
|
+
)
|
|
90
|
+
raise KeyError(error)
|
|
91
|
+
OptionManager.__options[cls] = merged_options
|
|
92
|
+
|
|
93
|
+
@classmethod
|
|
94
|
+
def _bind_option(cls, name: str, option: Option[Any]) -> None:
|
|
95
|
+
"""
|
|
96
|
+
Binds an option to this option manager.
|
|
97
|
+
|
|
98
|
+
:raises KeyError: If an option with the same name is already bound.
|
|
99
|
+
"""
|
|
100
|
+
assert cls is not OptionManager, "Cannot bind option to OptionManager."
|
|
101
|
+
assert option.owner is cls, "Attempting to incorrectly bind option."
|
|
102
|
+
options = OptionManager.__options.setdefault(cls, {})
|
|
103
|
+
if name in options:
|
|
104
|
+
raise KeyError(f"Duplicate option name {name!r}.")
|
|
105
|
+
options[name] = option
|
|
106
|
+
|
|
107
|
+
@classmethod
|
|
108
|
+
def _validate_values(cls, **option_values: Any) -> dict[str, Option[Any]]:
|
|
109
|
+
"""
|
|
110
|
+
Validates the given option values. See :meth:`Option.validate`.
|
|
111
|
+
"""
|
|
112
|
+
cls_options = cls.__options[cls]
|
|
113
|
+
options: dict[str, Option[Any]] = {}
|
|
114
|
+
for k, v in option_values.items():
|
|
115
|
+
(opt := cls_options[k]).validate(v)
|
|
116
|
+
options[k] = opt
|
|
117
|
+
return options
|
|
118
|
+
|
|
119
|
+
__slots__ = ("__weakref__",)
|
|
120
|
+
|
|
121
|
+
def set(self, **option_values: Any) -> Mapping[str, Any]:
|
|
122
|
+
"""
|
|
123
|
+
Sets values for the given options.
|
|
124
|
+
Returns the old values of the options that were set.
|
|
125
|
+
|
|
126
|
+
Example usage:
|
|
127
|
+
|
|
128
|
+
.. code-block:: python
|
|
129
|
+
|
|
130
|
+
options.set(validate=False, print_prec=5)
|
|
131
|
+
# Permanently sets 'validate' and 'print_prec' options.
|
|
132
|
+
|
|
133
|
+
For errors, see :meth:`Option.__set__`.
|
|
134
|
+
"""
|
|
135
|
+
options = type(self)._validate_values(**option_values)
|
|
136
|
+
return {k: options[k]._set(self, v) for k, v in option_values.items()}
|
|
137
|
+
|
|
138
|
+
def keys(self) -> Iterator[str]:
|
|
139
|
+
"""Iterator over option names."""
|
|
140
|
+
options = OptionManager.__options[type(self)]
|
|
141
|
+
return iter(options.keys())
|
|
142
|
+
|
|
143
|
+
def values(self) -> Iterator[Any]:
|
|
144
|
+
"""Iterator over option values."""
|
|
145
|
+
options = OptionManager.__options[type(self)]
|
|
146
|
+
return (opt._get(self) for opt in options.values())
|
|
147
|
+
|
|
148
|
+
def items(self) -> Iterator[tuple[str, Any]]:
|
|
149
|
+
"""Iterator over option name-value pairs."""
|
|
150
|
+
options = OptionManager.__options[type(self)]
|
|
151
|
+
return ((k, opt._get(self)) for k, opt in options.items())
|
|
152
|
+
|
|
153
|
+
def reset(self) -> Mapping[str, Any]:
|
|
154
|
+
"""
|
|
155
|
+
Resets all options to their default values.
|
|
156
|
+
Returns the old values for all options.
|
|
157
|
+
|
|
158
|
+
Example usage:
|
|
159
|
+
|
|
160
|
+
.. code-block:: python
|
|
161
|
+
|
|
162
|
+
options.reset()
|
|
163
|
+
# Permanently resets all options to default values.
|
|
164
|
+
|
|
165
|
+
For errors, see :meth:`Option.reset`.
|
|
166
|
+
"""
|
|
167
|
+
return {
|
|
168
|
+
k: opt.reset(self)
|
|
169
|
+
for k, opt in OptionManager.__options[type(self)].items()
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
@contextmanager
|
|
173
|
+
def __call__(self, **option_values: Any) -> Iterator[None]:
|
|
174
|
+
"""
|
|
175
|
+
With-context manager to temporarily set option values.
|
|
176
|
+
|
|
177
|
+
Example usage:
|
|
178
|
+
|
|
179
|
+
.. code-block:: python
|
|
180
|
+
|
|
181
|
+
with options(print_prec=5):
|
|
182
|
+
# Sets 'print_prec' to 5 temporarily in this context.
|
|
183
|
+
...
|
|
184
|
+
# Value of 'print_prec' is restored to its previous value here.
|
|
185
|
+
|
|
186
|
+
:meta public:
|
|
187
|
+
"""
|
|
188
|
+
options = type(self)._validate_values(**option_values)
|
|
189
|
+
for k, v in option_values.items():
|
|
190
|
+
options[k]._push(self, v)
|
|
191
|
+
try:
|
|
192
|
+
yield
|
|
193
|
+
finally:
|
|
194
|
+
for k in option_values:
|
|
195
|
+
options[k]._pop(self)
|
|
196
|
+
|
|
197
|
+
def __getitem__(self, name: str) -> Any:
|
|
198
|
+
"""
|
|
199
|
+
Gets the value of the given option.
|
|
200
|
+
|
|
201
|
+
:meta public:
|
|
202
|
+
"""
|
|
203
|
+
try:
|
|
204
|
+
return getattr(self, name)
|
|
205
|
+
except AttributeError:
|
|
206
|
+
# pylint: disable = raise-missing-from
|
|
207
|
+
raise KeyError(f"Option {name!r} not found.")
|
|
208
|
+
|
|
209
|
+
def __setitem__(self, name: str, value: Any) -> None:
|
|
210
|
+
"""
|
|
211
|
+
Sets the value of the given option.
|
|
212
|
+
|
|
213
|
+
For errors, see :meth:`Option.__set__`.
|
|
214
|
+
|
|
215
|
+
:meta public:
|
|
216
|
+
"""
|
|
217
|
+
try:
|
|
218
|
+
setattr(self, name, value)
|
|
219
|
+
except AttributeError:
|
|
220
|
+
# pylint: disable = raise-missing-from
|
|
221
|
+
raise KeyError(f"Option {name!r} not found.")
|
|
222
|
+
|
|
223
|
+
def __repr__(self) -> str:
|
|
224
|
+
id_str = f"{id(self):#x}"
|
|
225
|
+
options_lines = [f" {k!r}: {v!r}," for k, v in self.items()]
|
|
226
|
+
lines = ["OptionManager({", *options_lines, "}) at " + id_str]
|
|
227
|
+
return "\n".join(lines)
|
optmanage/option.py
ADDED
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Descriptor to define options in an option manager.
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
# OptManage: A library to create flexible option managers.
|
|
6
|
+
# Copyright (C) 2023 Hashberg Ltd
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import sys
|
|
11
|
+
from typing import (
|
|
12
|
+
TYPE_CHECKING,
|
|
13
|
+
Any,
|
|
14
|
+
Generic,
|
|
15
|
+
Protocol,
|
|
16
|
+
Type,
|
|
17
|
+
TypeVar,
|
|
18
|
+
overload,
|
|
19
|
+
)
|
|
20
|
+
from typing_validation import can_validate, validate
|
|
21
|
+
|
|
22
|
+
if sys.version_info[1] >= 12:
|
|
23
|
+
from typing import Self
|
|
24
|
+
else:
|
|
25
|
+
from typing_extensions import Self
|
|
26
|
+
|
|
27
|
+
if TYPE_CHECKING:
|
|
28
|
+
from .manager import OptionManager
|
|
29
|
+
|
|
30
|
+
ValueT = TypeVar("ValueT")
|
|
31
|
+
"""
|
|
32
|
+
Invariant type variable for option values (see :class:`Option`).
|
|
33
|
+
"""
|
|
34
|
+
|
|
35
|
+
ValueT_contra = TypeVar("ValueT_contra", contravariant=True)
|
|
36
|
+
""" Contravariant type variable for option values (see :class:`Option`). """
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
class Validator(Protocol[ValueT_contra]):
|
|
40
|
+
"""
|
|
41
|
+
Structura type for an option validator.
|
|
42
|
+
"""
|
|
43
|
+
|
|
44
|
+
def __call__(self, value: ValueT_contra, /) -> bool:
|
|
45
|
+
...
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
class Option(Generic[ValueT]):
|
|
49
|
+
"""
|
|
50
|
+
Descriptor for an option.
|
|
51
|
+
For usage examples, see :class:`OptionManager`.
|
|
52
|
+
|
|
53
|
+
See https://docs.python.org/3/reference/datamodel.html#descriptors
|
|
54
|
+
for a discussion of descriptors in Python.
|
|
55
|
+
"""
|
|
56
|
+
|
|
57
|
+
__values: dict[OptionManager, ValueT]
|
|
58
|
+
__value_stacks: dict[OptionManager, list[ValueT]]
|
|
59
|
+
__default: ValueT
|
|
60
|
+
__type: Type[ValueT]
|
|
61
|
+
__validator: Validator[ValueT] | None
|
|
62
|
+
__owner: Type[OptionManager]
|
|
63
|
+
__name: str
|
|
64
|
+
|
|
65
|
+
__slots__ = (
|
|
66
|
+
"__owner",
|
|
67
|
+
"__name",
|
|
68
|
+
"__values",
|
|
69
|
+
"__value_stacks",
|
|
70
|
+
"__default",
|
|
71
|
+
"__type",
|
|
72
|
+
"__validator",
|
|
73
|
+
)
|
|
74
|
+
|
|
75
|
+
def __new__(
|
|
76
|
+
cls,
|
|
77
|
+
default: ValueT,
|
|
78
|
+
ty: Any = None,
|
|
79
|
+
validator: Validator[ValueT] | None = None,
|
|
80
|
+
) -> Self:
|
|
81
|
+
"""
|
|
82
|
+
Creates a new option descriptor.
|
|
83
|
+
For usage examples, see :class:`OptionManager`.
|
|
84
|
+
|
|
85
|
+
:param default: The default value for the option.
|
|
86
|
+
:param ty: The type of the option. If not specified, defaults to the
|
|
87
|
+
type of the default value.
|
|
88
|
+
:param validator: A callable that takes a value and returns whether
|
|
89
|
+
it is valid for this option. If not specified,
|
|
90
|
+
defaults to :obj:`None` (no validation)
|
|
91
|
+
|
|
92
|
+
:meta public:
|
|
93
|
+
"""
|
|
94
|
+
if ty is None:
|
|
95
|
+
ty = type(default)
|
|
96
|
+
elif not can_validate(ty):
|
|
97
|
+
raise TypeError(f"Cannot validate type {ty!r}.")
|
|
98
|
+
if validator is not None and not callable(validator):
|
|
99
|
+
raise TypeError(f"Expected callable validator, got {validator!r}.")
|
|
100
|
+
instance = super().__new__(cls)
|
|
101
|
+
instance.__values = {}
|
|
102
|
+
instance.__value_stacks = {}
|
|
103
|
+
instance.__default = default
|
|
104
|
+
instance.__type = ty
|
|
105
|
+
instance.__validator = validator
|
|
106
|
+
instance.validate(instance.__default)
|
|
107
|
+
return instance
|
|
108
|
+
|
|
109
|
+
@property
|
|
110
|
+
def default(self) -> ValueT:
|
|
111
|
+
"""
|
|
112
|
+
The default value for this option.
|
|
113
|
+
"""
|
|
114
|
+
return self.__default
|
|
115
|
+
|
|
116
|
+
@property
|
|
117
|
+
def type(self) -> Type[ValueT]:
|
|
118
|
+
"""
|
|
119
|
+
The type of this option.
|
|
120
|
+
"""
|
|
121
|
+
return self.__type
|
|
122
|
+
|
|
123
|
+
@property
|
|
124
|
+
def validator(self) -> Validator[ValueT] | None:
|
|
125
|
+
"""
|
|
126
|
+
The validator for this option, or :obj:`None` if no additional
|
|
127
|
+
validation logic is specified (other than runtime type-checking).
|
|
128
|
+
"""
|
|
129
|
+
return self.__validator
|
|
130
|
+
|
|
131
|
+
@property
|
|
132
|
+
def name(self) -> str:
|
|
133
|
+
"""
|
|
134
|
+
The name of this option.
|
|
135
|
+
|
|
136
|
+
:raises AttributeError: if accessed before the option has been
|
|
137
|
+
bound to an option manager.
|
|
138
|
+
"""
|
|
139
|
+
return self.__name
|
|
140
|
+
|
|
141
|
+
@property
|
|
142
|
+
def owner(self) -> Type[OptionManager]:
|
|
143
|
+
"""
|
|
144
|
+
The owner of this option.
|
|
145
|
+
|
|
146
|
+
:raises AttributeError: if accessed before the option has been
|
|
147
|
+
bound to an option manager.
|
|
148
|
+
"""
|
|
149
|
+
return self.__owner
|
|
150
|
+
|
|
151
|
+
def validate(self, value: ValueT) -> None:
|
|
152
|
+
"""
|
|
153
|
+
Validates a given value for this option:
|
|
154
|
+
|
|
155
|
+
- Checks that the value has the correct type.
|
|
156
|
+
- If a validator is specified, checks that the value is valid.
|
|
157
|
+
"""
|
|
158
|
+
validate(value, self.__type)
|
|
159
|
+
if (validator := self.__validator) is not None:
|
|
160
|
+
if not validator(value):
|
|
161
|
+
name_str = (
|
|
162
|
+
f" {self.__name!r}" if hasattr(self, "__name") else ""
|
|
163
|
+
)
|
|
164
|
+
raise ValueError(
|
|
165
|
+
f"Invalid value for option{name_str}: " f"{value!r}."
|
|
166
|
+
)
|
|
167
|
+
|
|
168
|
+
def reset(self, instance: OptionManager) -> ValueT:
|
|
169
|
+
"""
|
|
170
|
+
Resets the option to its default value.
|
|
171
|
+
"""
|
|
172
|
+
# pylint: disable = import-outside-toplevel
|
|
173
|
+
from .manager import OptionManager
|
|
174
|
+
|
|
175
|
+
validate(instance, OptionManager)
|
|
176
|
+
if self._has_temporary_value(instance):
|
|
177
|
+
raise ValueError(
|
|
178
|
+
f"Option {self.__name!r} has a temporary value, "
|
|
179
|
+
"cannot be permanently reset."
|
|
180
|
+
)
|
|
181
|
+
if instance not in (values := self.__values):
|
|
182
|
+
return self.__default
|
|
183
|
+
old_value = values[instance]
|
|
184
|
+
del values[instance]
|
|
185
|
+
return old_value
|
|
186
|
+
|
|
187
|
+
def __set_name__(self, owner: Type[OptionManager], name: str) -> None:
|
|
188
|
+
"""
|
|
189
|
+
Hook to automatically bind the option to the option manager
|
|
190
|
+
that owns it.
|
|
191
|
+
|
|
192
|
+
:meta public:
|
|
193
|
+
"""
|
|
194
|
+
self.__owner = owner
|
|
195
|
+
self.__name = name
|
|
196
|
+
owner._bind_option(name, self)
|
|
197
|
+
|
|
198
|
+
def __set__(self, instance: OptionManager, value: ValueT) -> None:
|
|
199
|
+
"""
|
|
200
|
+
Sets the option to the given value.
|
|
201
|
+
The value is validated before being set.
|
|
202
|
+
|
|
203
|
+
:raises TypeError: If the value has the wrong type.
|
|
204
|
+
:raises ValueError: If the value is invalid for this option.
|
|
205
|
+
:raises ValueError: If the option has a temporarily set value.
|
|
206
|
+
|
|
207
|
+
:meta public:
|
|
208
|
+
"""
|
|
209
|
+
if self._has_temporary_value(instance):
|
|
210
|
+
raise ValueError(
|
|
211
|
+
f"Option {self.__name!r} has a temporary value, "
|
|
212
|
+
"cannot be permanently set."
|
|
213
|
+
)
|
|
214
|
+
self.validate(value)
|
|
215
|
+
self._set(instance, value)
|
|
216
|
+
|
|
217
|
+
@overload
|
|
218
|
+
def __get__(
|
|
219
|
+
self, instance: None, _: Type[OptionManager] | None = None
|
|
220
|
+
) -> Self:
|
|
221
|
+
...
|
|
222
|
+
|
|
223
|
+
@overload
|
|
224
|
+
def __get__(
|
|
225
|
+
self, instance: OptionManager, _: Type[OptionManager] | None = None
|
|
226
|
+
) -> ValueT:
|
|
227
|
+
...
|
|
228
|
+
|
|
229
|
+
def __get__(
|
|
230
|
+
self,
|
|
231
|
+
instance: OptionManager | None,
|
|
232
|
+
_: Type[OptionManager] | None = None,
|
|
233
|
+
) -> ValueT | Self:
|
|
234
|
+
"""
|
|
235
|
+
Gets the current value for the given option.
|
|
236
|
+
If no value has been set, the default value is returned.
|
|
237
|
+
|
|
238
|
+
:meta public:
|
|
239
|
+
"""
|
|
240
|
+
if instance is None:
|
|
241
|
+
return self
|
|
242
|
+
return self._get(instance)
|
|
243
|
+
|
|
244
|
+
def _get(self, instance: OptionManager) -> ValueT:
|
|
245
|
+
if instance not in (values := self.__values):
|
|
246
|
+
return self.__default
|
|
247
|
+
return values[instance]
|
|
248
|
+
|
|
249
|
+
def _set(self, instance: OptionManager, value: ValueT) -> ValueT:
|
|
250
|
+
old_value = self.__values.get(instance, self.__default)
|
|
251
|
+
self.__values[instance] = value
|
|
252
|
+
return old_value
|
|
253
|
+
|
|
254
|
+
def _push(self, instance: OptionManager, value: ValueT) -> ValueT:
|
|
255
|
+
old_value = self._get(instance)
|
|
256
|
+
self.__value_stacks.setdefault(instance, []).append(old_value)
|
|
257
|
+
return self._set(instance, value)
|
|
258
|
+
|
|
259
|
+
def _pop(self, instance: OptionManager) -> ValueT:
|
|
260
|
+
stack = self.__value_stacks.get(instance, None)
|
|
261
|
+
if not stack:
|
|
262
|
+
raise ValueError(f"Option {self.__name!r} has no temporary value.")
|
|
263
|
+
prev_value = self._set(instance, stack.pop())
|
|
264
|
+
if not stack:
|
|
265
|
+
del self.__value_stacks[instance]
|
|
266
|
+
return prev_value
|
|
267
|
+
|
|
268
|
+
def _has_temporary_value(self, instance: OptionManager) -> bool:
|
|
269
|
+
return instance in self.__value_stacks
|
optmanage/py.typed
ADDED
|
File without changes
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Hashberg
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: optmanage
|
|
3
|
+
Version: 1.0.0.post0
|
|
4
|
+
Summary: A library to create flexible option managers.
|
|
5
|
+
Home-page: https://github.com/hashberg-io/optmanage
|
|
6
|
+
Author: hashberg
|
|
7
|
+
Author-email: sg495@users.noreply.github.com
|
|
8
|
+
Project-URL: Bug Tracker, https://github.com/hashberg-io/optmanage/issues
|
|
9
|
+
Classifier: Development Status :: 4 - Beta
|
|
10
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
15
|
+
Classifier: Operating System :: OS Independent
|
|
16
|
+
Classifier: Natural Language :: English
|
|
17
|
+
Classifier: Typing :: Typed
|
|
18
|
+
Requires-Python: >=3.10
|
|
19
|
+
Description-Content-Type: text/x-rst
|
|
20
|
+
License-File: LICENSE
|
|
21
|
+
Requires-Dist: typing-extensions >=4.6.0
|
|
22
|
+
Requires-Dist: typing-validation >=1.2.4
|
|
23
|
+
Provides-Extra: dev
|
|
24
|
+
Requires-Dist: mypy ; extra == 'dev'
|
|
25
|
+
Requires-Dist: pylint ; extra == 'dev'
|
|
26
|
+
Requires-Dist: pytest ; extra == 'dev'
|
|
27
|
+
Requires-Dist: pytest-cov ; extra == 'dev'
|
|
28
|
+
|
|
29
|
+
optmanage: A library to create flexible option managers.
|
|
30
|
+
========================================================
|
|
31
|
+
|
|
32
|
+
.. image:: https://img.shields.io/badge/python-3.10+-green.svg
|
|
33
|
+
:target: https://docs.python.org/3.10/
|
|
34
|
+
:alt: Python versions
|
|
35
|
+
|
|
36
|
+
.. image:: https://img.shields.io/pypi/v/optmanage.svg
|
|
37
|
+
:target: https://pypi.python.org/pypi/optmanage/
|
|
38
|
+
:alt: PyPI version
|
|
39
|
+
|
|
40
|
+
.. image:: https://img.shields.io/pypi/status/optmanage.svg
|
|
41
|
+
:target: https://pypi.python.org/pypi/optmanage/
|
|
42
|
+
:alt: PyPI status
|
|
43
|
+
|
|
44
|
+
.. image:: http://www.mypy-lang.org/static/mypy_badge.svg
|
|
45
|
+
:target: https://github.com/python/mypy
|
|
46
|
+
:alt: Checked with Mypy
|
|
47
|
+
|
|
48
|
+
.. image:: https://readthedocs.org/projects/optmanage/badge/?version=latest
|
|
49
|
+
:target: https://optmanage.readthedocs.io/en/latest/?badge=latest
|
|
50
|
+
:alt: Documentation Status
|
|
51
|
+
|
|
52
|
+
.. image:: https://github.com/hashberg-io/optmanage/actions/workflows/python-pytest.yml/badge.svg
|
|
53
|
+
:target: https://github.com/hashberg-io/optmanage/actions/workflows/python-pytest.yml
|
|
54
|
+
:alt: Python package status
|
|
55
|
+
|
|
56
|
+
.. image:: https://img.shields.io/badge/readme%20style-standard-brightgreen.svg?style=flat-square
|
|
57
|
+
:target: https://github.com/RichardLitt/standard-readme
|
|
58
|
+
:alt: standard-readme compliant
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
A library to create flexible option managers.
|
|
62
|
+
|
|
63
|
+
.. contents::
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
Install
|
|
67
|
+
-------
|
|
68
|
+
|
|
69
|
+
This project is currently in private development. At present, you should clone the repository and install it locally as follows:
|
|
70
|
+
|
|
71
|
+
.. code-block:: console
|
|
72
|
+
|
|
73
|
+
$ pip install --upgrade .
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
One the package is published, you will be able to install the latest release from `PyPI <https://pypi.org/project/optmanage/>`_ as follows:
|
|
77
|
+
|
|
78
|
+
.. code-block:: console
|
|
79
|
+
|
|
80
|
+
$ pip install --upgrade optmanage
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
Usage
|
|
84
|
+
-----
|
|
85
|
+
|
|
86
|
+
This project is currently in private development. For usage examples, refer to the ``notebooks`` folder.
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
API
|
|
91
|
+
---
|
|
92
|
+
|
|
93
|
+
At present, documentation is built locally by the script ``docs/make-api-clean-html.bat``, and subsequently accessed from ``docs/_build/html/index.html``.
|
|
94
|
+
|
|
95
|
+
One the package is published, the full API documentation will be available at https://optmanage.readthedocs.io/
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
Contributing
|
|
99
|
+
------------
|
|
100
|
+
|
|
101
|
+
This project is currently in private development. Public contribution guidelines are available at `<CONTRIBUTING.md>`_.
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
License
|
|
105
|
+
-------
|
|
106
|
+
|
|
107
|
+
`LGPL © Hashberg Ltd. <LICENSE>`_
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
optmanage/__init__.py,sha256=RrwiqUAMI5ly0VHceBucf0nCrqD7p2bfOWnjaDjzzqo,539
|
|
2
|
+
optmanage/manager.py,sha256=hj2WeLKgRqpnjy_n9Xd_OhxjKdkV2saxAwWf_GKMrwc,7671
|
|
3
|
+
optmanage/option.py,sha256=oY0fbkSFD4cAUN99Nh2Fr53MhJJVn8FusOMjXpj12iU,8228
|
|
4
|
+
optmanage/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
5
|
+
optmanage-1.0.0.post0.dist-info/LICENSE,sha256=ehRQwyeCxuWTVrM_vGZcvbSFORSDGwYX3ayUZ8XCSnY,1086
|
|
6
|
+
optmanage-1.0.0.post0.dist-info/METADATA,sha256=zZU6N_5i8Byc2GXVsXgpali29U4pZx22EM5oM_8Cj5k,3480
|
|
7
|
+
optmanage-1.0.0.post0.dist-info/WHEEL,sha256=oiQVh_5PnQM0E3gPdiz09WCNmwiHDMaGer_elqB3coM,92
|
|
8
|
+
optmanage-1.0.0.post0.dist-info/top_level.txt,sha256=_aWE1mr-aDbm_SLOra2vfCC19qTCYvTtJ0cEAZw6hOM,10
|
|
9
|
+
optmanage-1.0.0.post0.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
optmanage
|