oddments 0.1.0__tar.gz
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.
- oddments-0.1.0/LICENSE +21 -0
- oddments-0.1.0/PKG-INFO +29 -0
- oddments-0.1.0/README.md +7 -0
- oddments-0.1.0/oddments/__init__.py +2 -0
- oddments-0.1.0/oddments/decorators.py +41 -0
- oddments-0.1.0/oddments/utils.py +111 -0
- oddments-0.1.0/pyproject.toml +18 -0
oddments-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Zachary Einck
|
|
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.
|
oddments-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: oddments
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Collection of general-purpose utilities.
|
|
5
|
+
Home-page: https://github.com/zteinck/oddments
|
|
6
|
+
License: MIT
|
|
7
|
+
Author: Zachary Einck
|
|
8
|
+
Author-email: zacharyeinck@gmail.com
|
|
9
|
+
Requires-Python: >=3.8,<4.0
|
|
10
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
17
|
+
Requires-Dist: numpy
|
|
18
|
+
Requires-Dist: pandas
|
|
19
|
+
Project-URL: Repository, https://github.com/zteinck/oddments
|
|
20
|
+
Description-Content-Type: text/markdown
|
|
21
|
+
|
|
22
|
+
# oddments
|
|
23
|
+
`oddments` is a Python package that offers a collection of general-purpose utilities.
|
|
24
|
+
|
|
25
|
+
## Installation
|
|
26
|
+
```sh
|
|
27
|
+
pip install oddments
|
|
28
|
+
```
|
|
29
|
+
|
oddments-0.1.0/README.md
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
from functools import wraps
|
|
2
|
+
|
|
3
|
+
from .utils import validate_value
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def validate_setter(call_func=False, **kwargs):
|
|
9
|
+
'''
|
|
10
|
+
Description
|
|
11
|
+
------------
|
|
12
|
+
Decorator for methods that are ultimately decorated by setters.
|
|
13
|
+
Validates the value being set via validate_value().
|
|
14
|
+
|
|
15
|
+
Parameters
|
|
16
|
+
------------
|
|
17
|
+
call_func : bool
|
|
18
|
+
If True, the wrapped function is called with self and value arguments
|
|
19
|
+
passed. Otherwise, a protected attribute is set mirroring func's name.
|
|
20
|
+
This parameter is useful for those scenarios where custom logic needs
|
|
21
|
+
to be applied before setting the protected attribute.
|
|
22
|
+
kwargs : dict
|
|
23
|
+
keyword arguments passed to validate_value().
|
|
24
|
+
|
|
25
|
+
Returns
|
|
26
|
+
------------
|
|
27
|
+
None
|
|
28
|
+
'''
|
|
29
|
+
|
|
30
|
+
def decorator(func):
|
|
31
|
+
|
|
32
|
+
@wraps(func)
|
|
33
|
+
def wrapper(self, value):
|
|
34
|
+
attr = func.__name__
|
|
35
|
+
validate_value(attr=attr, value=value, **kwargs)
|
|
36
|
+
if call_func: return func(self, value)
|
|
37
|
+
setattr(self, '_' + attr, value)
|
|
38
|
+
|
|
39
|
+
return wrapper
|
|
40
|
+
|
|
41
|
+
return decorator
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def validate_value(
|
|
7
|
+
value,
|
|
8
|
+
types,
|
|
9
|
+
attr=None,
|
|
10
|
+
finite=False,
|
|
11
|
+
min_value=None,
|
|
12
|
+
max_value=None,
|
|
13
|
+
min_inclusive=False,
|
|
14
|
+
max_inclusive=False,
|
|
15
|
+
whitelist=None,
|
|
16
|
+
blacklist=None,
|
|
17
|
+
none_ok=False,
|
|
18
|
+
empty_ok=True
|
|
19
|
+
):
|
|
20
|
+
'''
|
|
21
|
+
Description
|
|
22
|
+
------------
|
|
23
|
+
Validates an an argument's type and value.
|
|
24
|
+
|
|
25
|
+
Parameters
|
|
26
|
+
------------
|
|
27
|
+
value : *
|
|
28
|
+
argument to validate
|
|
29
|
+
types : object | tuple
|
|
30
|
+
one or more valid types of which value is allowed to be an instance.
|
|
31
|
+
attr : str | None
|
|
32
|
+
Optional name of value to include in error messages. If None, 'value'
|
|
33
|
+
is used.
|
|
34
|
+
finite : bool
|
|
35
|
+
if True, the value is not allowed to be -np.inf, +np.inf, np.nan, or
|
|
36
|
+
any other value that would cause np.isfinite to return False.
|
|
37
|
+
min_value : float | None
|
|
38
|
+
minimum allowed value
|
|
39
|
+
max_value : float | None
|
|
40
|
+
maximum allowed value
|
|
41
|
+
min_inclusive : bool
|
|
42
|
+
if True, value is allowed to be equal to min_value (e.g. greater than
|
|
43
|
+
or equal to)
|
|
44
|
+
max_inclusive : bool
|
|
45
|
+
if True, value is allowed to be equal to max_value (e.g. less than or
|
|
46
|
+
equal to)
|
|
47
|
+
whitelist : str | list
|
|
48
|
+
list of acceptable values
|
|
49
|
+
blacklist : str | list
|
|
50
|
+
list of prohibited values
|
|
51
|
+
none_ok : bool
|
|
52
|
+
if True, None is an acceptable value and no further validation is
|
|
53
|
+
necessary.
|
|
54
|
+
empty_ok : bool
|
|
55
|
+
if True, len(value) is allowed to be zero.
|
|
56
|
+
|
|
57
|
+
Returns
|
|
58
|
+
------------
|
|
59
|
+
None
|
|
60
|
+
'''
|
|
61
|
+
if none_ok and value is None: return
|
|
62
|
+
attr_name = f"'{attr or 'value'}'"
|
|
63
|
+
|
|
64
|
+
if not isinstance(types, tuple):
|
|
65
|
+
types = (types,)
|
|
66
|
+
|
|
67
|
+
if not isinstance(value, types):
|
|
68
|
+
type_names = [f'<{x.__name__}>' for x in types]
|
|
69
|
+
type_names = f"{', '.join(type_names[:-1])} or {type_names[-1]}" \
|
|
70
|
+
if len(type_names) > 1 else type_names[0]
|
|
71
|
+
value_type = f'<{type(value).__name__}>'
|
|
72
|
+
raise TypeError(f"{attr_name} must be a {type_names}, got {value_type}.")
|
|
73
|
+
|
|
74
|
+
if blacklist is not None:
|
|
75
|
+
if isinstance(blacklist, str):
|
|
76
|
+
blacklist = [blacklist]
|
|
77
|
+
if value in blacklist:
|
|
78
|
+
raise ValueError(f"{attr_name} cannot be in {blacklist}, got {value}.")
|
|
79
|
+
|
|
80
|
+
if whitelist is not None:
|
|
81
|
+
if isinstance(whitelist, str):
|
|
82
|
+
whitelist = [whitelist]
|
|
83
|
+
if value in whitelist: return
|
|
84
|
+
raise ValueError(f"{attr_name} must be in {whitelist}, got {value}.")
|
|
85
|
+
|
|
86
|
+
if not empty_ok and len(value) == 0:
|
|
87
|
+
raise ValueError(f"{attr_name} cannot be empty.")
|
|
88
|
+
|
|
89
|
+
if finite:
|
|
90
|
+
if not np.isfinite(value):
|
|
91
|
+
raise TypeError(f"{attr_name} must be finite, got {value}")
|
|
92
|
+
|
|
93
|
+
msg = f"{attr_name} must be {{0}} {{1}}, got {value}"
|
|
94
|
+
|
|
95
|
+
if min_value is not None:
|
|
96
|
+
symbol = None
|
|
97
|
+
if min_inclusive and value < min_value:
|
|
98
|
+
symbol = '≥'
|
|
99
|
+
if not min_inclusive and value <= min_value:
|
|
100
|
+
symbol = '>'
|
|
101
|
+
if symbol is not None:
|
|
102
|
+
raise ValueError(msg.format(symbol, min_value))
|
|
103
|
+
|
|
104
|
+
if max_value is not None:
|
|
105
|
+
symbol = None
|
|
106
|
+
if max_inclusive and value > max_value:
|
|
107
|
+
symbol = '≤'
|
|
108
|
+
if not max_inclusive and value >= max_value:
|
|
109
|
+
symbol = '<'
|
|
110
|
+
if symbol is not None:
|
|
111
|
+
raise ValueError(msg.format(symbol, max_value))
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
[tool.poetry]
|
|
2
|
+
name = "oddments"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "Collection of general-purpose utilities."
|
|
5
|
+
authors = ["Zachary Einck <zacharyeinck@gmail.com>"]
|
|
6
|
+
license = "MIT"
|
|
7
|
+
readme = "README.md"
|
|
8
|
+
repository = "https://github.com/zteinck/oddments"
|
|
9
|
+
homepage = "https://github.com/zteinck/oddments"
|
|
10
|
+
|
|
11
|
+
[tool.poetry.dependencies]
|
|
12
|
+
python = "^3.8"
|
|
13
|
+
numpy = "*"
|
|
14
|
+
pandas = "*"
|
|
15
|
+
|
|
16
|
+
[build-system]
|
|
17
|
+
requires = ["poetry-core"]
|
|
18
|
+
build-backend = "poetry.core.masonry.api"
|