pythonic-fp-gadgets 3.0.1__py3-none-any.whl → 4.0.0__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.
- pythonic_fp/gadgets/__init__.py +61 -13
- pythonic_fp/gadgets/__init__.pyi +6 -0
- pythonic_fp/gadgets/box.py +22 -26
- pythonic_fp/gadgets/box.pyi +3 -2
- pythonic_fp/gadgets/{iterate_arguments.py → sentinels/__init__.py} +3 -22
- pythonic_fp/gadgets/sentinels/__init__.pyi +0 -0
- pythonic_fp/gadgets/sentinels/flavored.py +72 -0
- pythonic_fp/gadgets/sentinels/flavored.pyi +8 -0
- pythonic_fp/gadgets/sentinels/novalue.py +124 -0
- pythonic_fp/gadgets/sentinels/novalue.pyi +6 -0
- pythonic_fp/gadgets/wrap.py +124 -0
- pythonic_fp/gadgets/wrap.pyi +23 -0
- {pythonic_fp_gadgets-3.0.1.dist-info → pythonic_fp_gadgets-4.0.0.dist-info}/METADATA +16 -13
- pythonic_fp_gadgets-4.0.0.dist-info/RECORD +17 -0
- pythonic_fp/gadgets/iterate_arguments.pyi +0 -5
- pythonic_fp/gadgets/latest_common_ancestor.py +0 -51
- pythonic_fp/gadgets/latest_common_ancestor.pyi +0 -3
- pythonic_fp_gadgets-3.0.1.dist-info/RECORD +0 -12
- {pythonic_fp_gadgets-3.0.1.dist-info → pythonic_fp_gadgets-4.0.0.dist-info}/WHEEL +0 -0
- {pythonic_fp_gadgets-3.0.1.dist-info → pythonic_fp_gadgets-4.0.0.dist-info}/licenses/LICENSE +0 -0
pythonic_fp/gadgets/__init__.py
CHANGED
|
@@ -13,23 +13,71 @@
|
|
|
13
13
|
# limitations under the License.
|
|
14
14
|
|
|
15
15
|
"""
|
|
16
|
-
|
|
17
|
-
==============
|
|
16
|
+
**Module level function names.**
|
|
18
17
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
+------------------------------+------------------+------------------------------------------------+
|
|
26
|
-
| Return Iterator of arguments | function ``ita`` | ``pythonic_fp.gadgets.iterate_arguments`` |
|
|
27
|
-
+------------------------------+------------------+------------------------------------------------+
|
|
28
|
-
| Find least common base class | function ``lca`` | ``pythonic_fp.gadgets.latest_common_ancestor`` |
|
|
29
|
-
+------------------------------+------------------+------------------------------------------------+
|
|
18
|
+
========================== ================================================
|
|
19
|
+
Function Description
|
|
20
|
+
========================== ================================================
|
|
21
|
+
``first_common_ancestor`` Find least common base class in a type hierarchy
|
|
22
|
+
``iterate_over_arguments`` Return an iterator over the function's arguments
|
|
23
|
+
========================== ================================================
|
|
30
24
|
|
|
31
25
|
"""
|
|
32
26
|
|
|
27
|
+
from collections.abc import Iterator
|
|
28
|
+
from inspect import getmro
|
|
29
|
+
|
|
30
|
+
__all__ = ['iterate_over_arguments', 'first_common_ancestor']
|
|
31
|
+
|
|
33
32
|
__author__ = 'Geoffrey R. Scheller'
|
|
34
33
|
__copyright__ = 'Copyright (c) 2023-2025 Geoffrey R. Scheller'
|
|
35
34
|
__license__ = 'Apache License 2.0'
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def iterate_over_arguments[A](*args: A) -> Iterator[A]:
|
|
38
|
+
"""Function returning an iterator of its arguments.
|
|
39
|
+
|
|
40
|
+
.. note::
|
|
41
|
+
|
|
42
|
+
Does not create an object to iterate over.
|
|
43
|
+
|
|
44
|
+
- well, not in the Python world
|
|
45
|
+
- maybe in the C world
|
|
46
|
+
|
|
47
|
+
:param args: Objects to iterate over.
|
|
48
|
+
:returns: An iterator of the arguments.
|
|
49
|
+
|
|
50
|
+
"""
|
|
51
|
+
yield from args
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def first_common_ancestor(cls1: type, cls2: type) -> type:
|
|
55
|
+
"""Find the least upper bound in the inheritance graph
|
|
56
|
+
of two classes.
|
|
57
|
+
|
|
58
|
+
.. warning::
|
|
59
|
+
|
|
60
|
+
This function can fail with a TypeError. Some error messages
|
|
61
|
+
seen are
|
|
62
|
+
|
|
63
|
+
- multiple bases have instance lay-out conflict
|
|
64
|
+
- type 'bool' is not an acceptable base type
|
|
65
|
+
|
|
66
|
+
This happens frequently when the function is given
|
|
67
|
+
Python builtin types or in multiple inheritance situations.
|
|
68
|
+
|
|
69
|
+
:param cls1: A class in the inheritance hierarchy.
|
|
70
|
+
:param cls2: A class in the inheritance hierarchy.
|
|
71
|
+
:returns: First common ancestor based on getmro order.
|
|
72
|
+
:raises TypeError: Raised by ``inspect.getmro``.
|
|
73
|
+
|
|
74
|
+
"""
|
|
75
|
+
if issubclass(cls1, cls2):
|
|
76
|
+
return cls2
|
|
77
|
+
if issubclass(cls2, cls1):
|
|
78
|
+
return cls1
|
|
79
|
+
|
|
80
|
+
for common_ancestor in getmro(type('LcaDiamondClass', (cls1, cls2), {})):
|
|
81
|
+
if issubclass(cls1, common_ancestor) and issubclass(cls2, common_ancestor):
|
|
82
|
+
return common_ancestor
|
|
83
|
+
raise TypeError("latest_common_ancestor: no common ancestor found!!!")
|
pythonic_fp/gadgets/box.py
CHANGED
|
@@ -12,13 +12,13 @@
|
|
|
12
12
|
# See the License for the specific language governing permissions and
|
|
13
13
|
# limitations under the License.
|
|
14
14
|
|
|
15
|
-
"""Class for a stateful container that can hold at most one item."""
|
|
16
|
-
|
|
17
15
|
__all__ = ['Box']
|
|
18
16
|
|
|
19
17
|
from collections.abc import Callable, Iterator
|
|
20
|
-
from typing import
|
|
21
|
-
|
|
18
|
+
from typing import cast, Final, overload
|
|
19
|
+
|
|
20
|
+
type _Sentinel = object
|
|
21
|
+
_sentinel: Final[_Sentinel] = object()
|
|
22
22
|
|
|
23
23
|
|
|
24
24
|
class Box[T]:
|
|
@@ -26,8 +26,8 @@ class Box[T]:
|
|
|
26
26
|
|
|
27
27
|
.. note::
|
|
28
28
|
|
|
29
|
-
|
|
30
|
-
|
|
29
|
+
- ``Box(item: T)``: contains at one item of type ``T``
|
|
30
|
+
- ``Box[T]()``: creates empty container
|
|
31
31
|
|
|
32
32
|
Where type ``T`` is some definite type, which
|
|
33
33
|
could be ``None`` or even ``Never``.
|
|
@@ -40,23 +40,19 @@ class Box[T]:
|
|
|
40
40
|
__slots__ = ('_item',)
|
|
41
41
|
__match_args__ = ('_item',)
|
|
42
42
|
|
|
43
|
-
_sentinel: Final[ClassVar[Sentinel[str]]] = Sentinel('_Box')
|
|
44
|
-
|
|
45
|
-
@overload
|
|
46
|
-
def __init__(self) -> None: ...
|
|
47
43
|
@overload
|
|
48
44
|
def __init__(self, item: T) -> None: ...
|
|
45
|
+
@overload
|
|
46
|
+
def __init__(self) -> None: ...
|
|
49
47
|
|
|
50
|
-
def __init__(self, item: T |
|
|
48
|
+
def __init__(self, item: T | _Sentinel = _sentinel) -> None:
|
|
51
49
|
"""
|
|
52
|
-
:param item:
|
|
53
|
-
:returns: New ``Box`` instance.
|
|
54
|
-
|
|
50
|
+
:param item: An "optional" initial contained ``item`` for the ``Box``.
|
|
55
51
|
"""
|
|
56
52
|
self._item = item
|
|
57
53
|
|
|
58
54
|
def __bool__(self) -> bool:
|
|
59
|
-
return self._item is not
|
|
55
|
+
return self._item is not _sentinel
|
|
60
56
|
|
|
61
57
|
def __iter__(self) -> Iterator[T]:
|
|
62
58
|
if self:
|
|
@@ -85,7 +81,7 @@ class Box[T]:
|
|
|
85
81
|
@overload
|
|
86
82
|
def get(self, alt: T) -> T: ...
|
|
87
83
|
|
|
88
|
-
def get(self, alt: T |
|
|
84
|
+
def get(self, alt: T | _Sentinel = _sentinel) -> T:
|
|
89
85
|
"""Return the contained item if it exists, otherwise an alternate item.
|
|
90
86
|
|
|
91
87
|
:param alt: an "optional" item of type ``T`` to return if ``Box`` is empty
|
|
@@ -93,9 +89,9 @@ class Box[T]:
|
|
|
93
89
|
:raises ValueError: when an ``alt`` item is not provided but needed
|
|
94
90
|
|
|
95
91
|
"""
|
|
96
|
-
if self._item is not
|
|
92
|
+
if self._item is not _sentinel:
|
|
97
93
|
return cast(T, self._item)
|
|
98
|
-
if alt is
|
|
94
|
+
if alt is _sentinel:
|
|
99
95
|
msg = 'Box: get from empty Box with no alternate return item provided'
|
|
100
96
|
raise ValueError(msg)
|
|
101
97
|
return cast(T, alt)
|
|
@@ -103,15 +99,15 @@ class Box[T]:
|
|
|
103
99
|
def pop(self) -> T:
|
|
104
100
|
"""Pop the contained item if ``Box`` is not empty.
|
|
105
101
|
|
|
106
|
-
:returns: item contained in the ``Box
|
|
107
|
-
:raises
|
|
102
|
+
:returns: The item contained in the ``Box``.
|
|
103
|
+
:raises ValueError: If Box is empty.
|
|
108
104
|
|
|
109
105
|
"""
|
|
110
|
-
if self._item is
|
|
106
|
+
if self._item is _sentinel:
|
|
111
107
|
msg = 'Box: Trying to pop an item from an empty Box'
|
|
112
108
|
raise ValueError(msg)
|
|
113
109
|
popped = cast(T, self._item)
|
|
114
|
-
self._item =
|
|
110
|
+
self._item = _sentinel
|
|
115
111
|
return popped
|
|
116
112
|
|
|
117
113
|
def push(self, item: T) -> None:
|
|
@@ -121,7 +117,7 @@ class Box[T]:
|
|
|
121
117
|
:raises ValueError: If ``Box`` is not empty.
|
|
122
118
|
|
|
123
119
|
"""
|
|
124
|
-
if self._item is
|
|
120
|
+
if self._item is _sentinel:
|
|
125
121
|
self._item = item
|
|
126
122
|
else:
|
|
127
123
|
msg = 'Box: Trying to push an item in a non-empty Box'
|
|
@@ -139,7 +135,7 @@ class Box[T]:
|
|
|
139
135
|
:returns: Original contents of the ``Box``.
|
|
140
136
|
:raises ValueError: If Box is empty.
|
|
141
137
|
"""
|
|
142
|
-
if self._item is
|
|
138
|
+
if self._item is _sentinel:
|
|
143
139
|
msg = 'Box: Trying to exchange items from an empty Box'
|
|
144
140
|
raise ValueError(msg)
|
|
145
141
|
popped = cast(T, self._item)
|
|
@@ -154,7 +150,7 @@ class Box[T]:
|
|
|
154
150
|
:returns: a new instance
|
|
155
151
|
|
|
156
152
|
"""
|
|
157
|
-
if self._item is
|
|
153
|
+
if self._item is _sentinel:
|
|
158
154
|
return Box()
|
|
159
155
|
return Box(f(cast(T, self._item)))
|
|
160
156
|
|
|
@@ -165,6 +161,6 @@ class Box[T]:
|
|
|
165
161
|
:returns: a new instance
|
|
166
162
|
|
|
167
163
|
"""
|
|
168
|
-
if self._item is
|
|
164
|
+
if self._item is _sentinel:
|
|
169
165
|
return Box()
|
|
170
166
|
return f(cast(T, self._item))
|
pythonic_fp/gadgets/box.pyi
CHANGED
|
@@ -4,12 +4,13 @@ from typing import overload
|
|
|
4
4
|
|
|
5
5
|
__all__ = ['Box']
|
|
6
6
|
|
|
7
|
+
type _Sentinel = object
|
|
7
8
|
class Box[T]:
|
|
8
9
|
__match_args__: Incomplete
|
|
9
10
|
@overload
|
|
10
|
-
def __init__(self) -> None: ...
|
|
11
|
-
@overload
|
|
12
11
|
def __init__(self, item: T) -> None: ...
|
|
12
|
+
@overload
|
|
13
|
+
def __init__(self) -> None: ...
|
|
13
14
|
def __bool__(self) -> bool: ...
|
|
14
15
|
def __iter__(self) -> Iterator[T]: ...
|
|
15
16
|
def __len__(self) -> int: ...
|
|
@@ -12,25 +12,6 @@
|
|
|
12
12
|
# See the License for the specific language governing permissions and
|
|
13
13
|
# limitations under the License.
|
|
14
14
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
__all__ = ['ita']
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
def ita[A](*args: A) -> Iterator[A]:
|
|
23
|
-
"""Function returning an iterator of its arguments.
|
|
24
|
-
|
|
25
|
-
.. note::
|
|
26
|
-
|
|
27
|
-
Does not create an object to iterate over.
|
|
28
|
-
|
|
29
|
-
- well, not in the Python world
|
|
30
|
-
- maybe in the C world
|
|
31
|
-
|
|
32
|
-
:param args: Objects to iterate over.
|
|
33
|
-
:returns: An iterator of the arguments.
|
|
34
|
-
|
|
35
|
-
"""
|
|
36
|
-
yield from args
|
|
15
|
+
__author__ = 'Geoffrey R. Scheller'
|
|
16
|
+
__copyright__ = 'Copyright (c) 2023-2025 Geoffrey R. Scheller'
|
|
17
|
+
__license__ = 'Apache License 2.0'
|
|
File without changes
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
# Copyright 2023-2025 Geoffrey R. Scheller
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
|
|
15
|
+
"""
|
|
16
|
+
**Sentinel values labeled by different (hashable) flavors.**
|
|
17
|
+
|
|
18
|
+
.. note::
|
|
19
|
+
|
|
20
|
+
Can be compared using ``==`` and ``!=``. A flavored sentinel
|
|
21
|
+
value always equals itself and never equals anything else,
|
|
22
|
+
especially other flavored sentinel values.
|
|
23
|
+
|
|
24
|
+
Useful for union types where ``Sentinel[H]`` is one of the
|
|
25
|
+
types making up the union.
|
|
26
|
+
|
|
27
|
+
To ensure that reference equality is used, put the known
|
|
28
|
+
sentinel value first in the comparison.
|
|
29
|
+
|
|
30
|
+
.. note::
|
|
31
|
+
|
|
32
|
+
Threadsafe.
|
|
33
|
+
|
|
34
|
+
"""
|
|
35
|
+
|
|
36
|
+
import threading
|
|
37
|
+
from typing import ClassVar, final, Hashable
|
|
38
|
+
|
|
39
|
+
__all__ = ['Sentinel']
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
@final
|
|
43
|
+
class Sentinel[H: Hashable]:
|
|
44
|
+
__slots__ = ('_flavor',)
|
|
45
|
+
|
|
46
|
+
_flavors: 'dict[H, Sentinel[H]]' = {}
|
|
47
|
+
_lock: ClassVar[threading.Lock] = threading.Lock()
|
|
48
|
+
|
|
49
|
+
def __new__(cls, flavor: H) -> 'Sentinel[H]':
|
|
50
|
+
if flavor not in cls._flavors:
|
|
51
|
+
with cls._lock:
|
|
52
|
+
if flavor not in cls._flavors:
|
|
53
|
+
cls._flavors[flavor] = super().__new__(cls)
|
|
54
|
+
return cls._flavors[flavor]
|
|
55
|
+
|
|
56
|
+
def __init__(self, flavor: H) -> None:
|
|
57
|
+
"""
|
|
58
|
+
:param flavor: Some Hashable value of generic type ``H``.
|
|
59
|
+
:returns: The ``Sentinel`` singleton instance with flavor ``flavor``.
|
|
60
|
+
:rtype: ``Sentinel[H]`` where ``H`` is a subtype of Hashable.
|
|
61
|
+
"""
|
|
62
|
+
if not hasattr(self, '_flavor'):
|
|
63
|
+
self._flavor = flavor
|
|
64
|
+
|
|
65
|
+
def __repr__(self) -> str:
|
|
66
|
+
return "Sentinel('" + repr(self._flavor) + "')"
|
|
67
|
+
|
|
68
|
+
def flavor(self) -> H:
|
|
69
|
+
"""
|
|
70
|
+
:returns: The sentinel's flavor. A ``Hashable`` value of type ``H``.
|
|
71
|
+
"""
|
|
72
|
+
return self._flavor
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
# Copyright 2023-2025 Geoffrey R. Scheller
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
|
|
15
|
+
"""
|
|
16
|
+
**Singleton class representing an actually, not potentially, missing value.**
|
|
17
|
+
|
|
18
|
+
.. note:
|
|
19
|
+
|
|
20
|
+
``NoValue()`` is a singleton object representing a missing value.
|
|
21
|
+
|
|
22
|
+
While ``None`` and ``()`` are frequently used as sentinel values,
|
|
23
|
+
I prefer to think of them as
|
|
24
|
+
|
|
25
|
+
- ``None``: Returns, or returned, no values.
|
|
26
|
+
- ``()``: An empty, possibly typed, iterable collection.
|
|
27
|
+
|
|
28
|
+
.. important::
|
|
29
|
+
|
|
30
|
+
Given variables
|
|
31
|
+
|
|
32
|
+
.. code:: python
|
|
33
|
+
|
|
34
|
+
x: int | NoValue
|
|
35
|
+
y: int | NoValue
|
|
36
|
+
|
|
37
|
+
Equality between ``x`` and ``y`` means both values exist and compare
|
|
38
|
+
as equal. If one or both of theses values are missing, then what is
|
|
39
|
+
there to compare?
|
|
40
|
+
|
|
41
|
+
.. table:: ``x == y``
|
|
42
|
+
|
|
43
|
+
+-----------+-----------+--------+--------+
|
|
44
|
+
| x∖y | NoValue() | 42 | 57 |
|
|
45
|
+
+===========+===========+========+========+
|
|
46
|
+
| NoValue() | false | false | false |
|
|
47
|
+
+-----------+-----------+--------+--------+
|
|
48
|
+
| 42 | false | true | false |
|
|
49
|
+
+-----------+-----------+--------+--------+
|
|
50
|
+
| 57 | false | false | true |
|
|
51
|
+
+-----------+-----------+--------+--------+
|
|
52
|
+
|
|
53
|
+
Similarly for not equals.
|
|
54
|
+
|
|
55
|
+
.. table:: ``x != y``
|
|
56
|
+
|
|
57
|
+
+-----------+-----------+--------+--------+
|
|
58
|
+
| x∖y | NoValue() | 42 | 57 |
|
|
59
|
+
+===========+===========+========+========+
|
|
60
|
+
| NoValue() | false | false | false |
|
|
61
|
+
+-----------+-----------+--------+--------+
|
|
62
|
+
| 42 | false | false | true |
|
|
63
|
+
+-----------+-----------+--------+--------+
|
|
64
|
+
| 57 | false | true | false |
|
|
65
|
+
+-----------+-----------+--------+--------+
|
|
66
|
+
|
|
67
|
+
.. warning::
|
|
68
|
+
|
|
69
|
+
Only use ``==`` or ``!=`` in value comparisons. To directly
|
|
70
|
+
identity the ``NoValue`` singleton, use ``is`` and ``is not``
|
|
71
|
+
instead.
|
|
72
|
+
|
|
73
|
+
.. note::
|
|
74
|
+
|
|
75
|
+
Threadsafe.
|
|
76
|
+
|
|
77
|
+
.. tip::
|
|
78
|
+
|
|
79
|
+
Use as a hidden implementation detail when creating "optional"
|
|
80
|
+
arguments to functions and methods.
|
|
81
|
+
|
|
82
|
+
To help ensure the abstraction does not leak,
|
|
83
|
+
|
|
84
|
+
- Do not export the sentinel value.
|
|
85
|
+
- Use ``@overload`` to keep the NoValue type out of documentation and IDEs.
|
|
86
|
+
|
|
87
|
+
"""
|
|
88
|
+
import threading
|
|
89
|
+
from typing import ClassVar, final
|
|
90
|
+
|
|
91
|
+
__all__ = ['NoValue']
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
@final
|
|
95
|
+
class NoValue():
|
|
96
|
+
__slots__ = ()
|
|
97
|
+
|
|
98
|
+
_instance: 'ClassVar[NoValue | None]' = None
|
|
99
|
+
_lock: ClassVar[threading.Lock] = threading.Lock()
|
|
100
|
+
|
|
101
|
+
def __new__(cls) -> 'NoValue':
|
|
102
|
+
"""
|
|
103
|
+
:returns: The ``NoValue`` singleton instance.
|
|
104
|
+
"""
|
|
105
|
+
if cls._instance is None:
|
|
106
|
+
with cls._lock:
|
|
107
|
+
if cls._instance is None:
|
|
108
|
+
cls._instance = super().__new__(cls)
|
|
109
|
+
return cls._instance
|
|
110
|
+
|
|
111
|
+
def __repr__(self) -> str:
|
|
112
|
+
return 'NoValue()'
|
|
113
|
+
|
|
114
|
+
def __eq__(self, other: object) -> bool:
|
|
115
|
+
"""
|
|
116
|
+
:returns: False
|
|
117
|
+
"""
|
|
118
|
+
return False
|
|
119
|
+
|
|
120
|
+
def __ne__(self, other: object) -> bool:
|
|
121
|
+
"""
|
|
122
|
+
:returns: False
|
|
123
|
+
"""
|
|
124
|
+
return False
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
# Copyright 2023-2025 Geoffrey R. Scheller
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
|
|
15
|
+
__all__ = ['Wrap', 'HWrap']
|
|
16
|
+
|
|
17
|
+
from collections.abc import Callable, Iterator, Hashable
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class Wrap[T]():
|
|
21
|
+
"""Immutablely wrap exactly one value of a given type.
|
|
22
|
+
|
|
23
|
+
.. tip::
|
|
24
|
+
|
|
25
|
+
``Wrap`` objects can be used in Python match statements.
|
|
26
|
+
|
|
27
|
+
"""
|
|
28
|
+
__slots__ = ('_item',)
|
|
29
|
+
__match_args__ = ('_item',)
|
|
30
|
+
|
|
31
|
+
def __init__(self, item: T) -> None:
|
|
32
|
+
self._item = item
|
|
33
|
+
|
|
34
|
+
def __bool__(self) -> bool:
|
|
35
|
+
return bool(self._item)
|
|
36
|
+
|
|
37
|
+
def __iter__(self) -> Iterator[T]:
|
|
38
|
+
if self:
|
|
39
|
+
yield self._item
|
|
40
|
+
|
|
41
|
+
def __repr__(self) -> str:
|
|
42
|
+
return 'Wrap(' + repr(self._item) + ')'
|
|
43
|
+
|
|
44
|
+
def __eq__(self, other: object) -> bool:
|
|
45
|
+
if not isinstance(other, type(self)):
|
|
46
|
+
return False
|
|
47
|
+
|
|
48
|
+
if self._item is other._item:
|
|
49
|
+
return True
|
|
50
|
+
return self._item == other._item
|
|
51
|
+
|
|
52
|
+
def map[U](self, f: Callable[[T], U]) -> 'Wrap[U]':
|
|
53
|
+
"""Map function ``f`` over contents.
|
|
54
|
+
|
|
55
|
+
:param f: Mapping function.
|
|
56
|
+
:returns: A new instance.
|
|
57
|
+
|
|
58
|
+
"""
|
|
59
|
+
return Wrap(f(self._item))
|
|
60
|
+
|
|
61
|
+
def bind[U](self, f: Callable[[T], 'Wrap[U]']) -> 'Wrap[U]':
|
|
62
|
+
"""Flatmap the ``Wrap`` with function ``f``.
|
|
63
|
+
|
|
64
|
+
:param f: Binding function.
|
|
65
|
+
:returns: A new instance.
|
|
66
|
+
|
|
67
|
+
"""
|
|
68
|
+
return f(self._item)
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
class HWrap[T: Hashable](Hashable):
|
|
72
|
+
"""Immutablely wrap exactly one hashable value of a given type.
|
|
73
|
+
|
|
74
|
+
.. tip::
|
|
75
|
+
|
|
76
|
+
``Wrap`` objects can be used in Python match statements.
|
|
77
|
+
|
|
78
|
+
"""
|
|
79
|
+
__slots__ = ('_item', '_hash')
|
|
80
|
+
__match_args__ = ('_item',)
|
|
81
|
+
|
|
82
|
+
def __init__(self, item: T) -> None:
|
|
83
|
+
self._item, self._hash = item, hash(item)
|
|
84
|
+
|
|
85
|
+
def __hash__(self) -> int:
|
|
86
|
+
return self._hash
|
|
87
|
+
|
|
88
|
+
def __bool__(self) -> bool:
|
|
89
|
+
return bool(self._item)
|
|
90
|
+
|
|
91
|
+
def __iter__(self) -> Iterator[T]:
|
|
92
|
+
if self:
|
|
93
|
+
yield self._item
|
|
94
|
+
|
|
95
|
+
def __repr__(self) -> str:
|
|
96
|
+
return 'Wrap(' + repr(self._item) + ')'
|
|
97
|
+
|
|
98
|
+
def __eq__(self, other: object) -> bool:
|
|
99
|
+
if not isinstance(other, type(self)):
|
|
100
|
+
return False
|
|
101
|
+
|
|
102
|
+
if self._hash != other._hash:
|
|
103
|
+
return False
|
|
104
|
+
if self._item is other._item:
|
|
105
|
+
return True
|
|
106
|
+
return self._item == other._item
|
|
107
|
+
|
|
108
|
+
def map[U](self, f: Callable[[T], U]) -> 'HWrap[U]':
|
|
109
|
+
"""Map function ``f`` over contents.
|
|
110
|
+
|
|
111
|
+
:param f: Mapping function.
|
|
112
|
+
:returns: A new instance.
|
|
113
|
+
|
|
114
|
+
"""
|
|
115
|
+
return HWrap(f(self._item))
|
|
116
|
+
|
|
117
|
+
def bind[U](self, f: Callable[[T], 'HWrap[U]']) -> 'HWrap[U]':
|
|
118
|
+
"""Flatmap the ``Wrap`` with function ``f``.
|
|
119
|
+
|
|
120
|
+
:param f: Binding function.
|
|
121
|
+
:returns: A new instance.
|
|
122
|
+
|
|
123
|
+
"""
|
|
124
|
+
return f(self._item)
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
from _typeshed import Incomplete
|
|
2
|
+
from collections.abc import Callable, Hashable, Iterator
|
|
3
|
+
|
|
4
|
+
__all__ = ['Wrap', 'HWrap']
|
|
5
|
+
|
|
6
|
+
class Wrap[T]:
|
|
7
|
+
__match_args__: Incomplete
|
|
8
|
+
def __init__(self, item: T) -> None: ...
|
|
9
|
+
def __bool__(self) -> bool: ...
|
|
10
|
+
def __iter__(self) -> Iterator[T]: ...
|
|
11
|
+
def __eq__(self, other: object) -> bool: ...
|
|
12
|
+
def map[U](self, f: Callable[[T], U]) -> Wrap[U]: ...
|
|
13
|
+
def bind[U](self, f: Callable[[T], 'Wrap[U]']) -> Wrap[U]: ...
|
|
14
|
+
|
|
15
|
+
class HWrap[T: Hashable](Hashable):
|
|
16
|
+
__match_args__: Incomplete
|
|
17
|
+
def __init__(self, item: T) -> None: ...
|
|
18
|
+
def __hash__(self) -> int: ...
|
|
19
|
+
def __bool__(self) -> bool: ...
|
|
20
|
+
def __iter__(self) -> Iterator[T]: ...
|
|
21
|
+
def __eq__(self, other: object) -> bool: ...
|
|
22
|
+
def map[U](self, f: Callable[[T], U]) -> HWrap[U]: ...
|
|
23
|
+
def bind[U](self, f: Callable[[T], 'HWrap[U]']) -> HWrap[U]: ...
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: pythonic-fp-gadgets
|
|
3
|
-
Version:
|
|
4
|
-
Summary:
|
|
3
|
+
Version: 4.0.0
|
|
4
|
+
Summary: **Module level function names.**
|
|
5
5
|
Keywords: gadgets
|
|
6
6
|
Author-email: "Geoffrey R. Scheller" <geoffrey@scheller.com>
|
|
7
7
|
Requires-Python: >=3.13
|
|
@@ -14,11 +14,11 @@ Classifier: Operating System :: OS Independent
|
|
|
14
14
|
Classifier: Programming Language :: Python :: 3.13
|
|
15
15
|
Classifier: Typing :: Typed
|
|
16
16
|
License-File: LICENSE
|
|
17
|
-
Requires-Dist: pythonic-fp-sentinels>=2.1.0
|
|
18
17
|
Requires-Dist: pytest>=8.4.1 ; extra == "test"
|
|
18
|
+
Requires-Dist: pythonic-fp-circulararray>=6.0.0 ; extra == "test"
|
|
19
19
|
Project-URL: Changelog, https://github.com/grscheller/pythonic-fp-gadgets/blob/main/CHANGELOG.rst
|
|
20
|
-
Project-URL: Documentation, https://grscheller.github.io/pythonic-fp/gadgets/development/build/html/
|
|
21
|
-
Project-URL: Homepage, https://github.
|
|
20
|
+
Project-URL: Documentation, https://grscheller.github.io/pythonic-fp/gadgets/development/build/html/
|
|
21
|
+
Project-URL: Homepage, https://grscheller.github.io/pythonic-fp/homepage/build/html/
|
|
22
22
|
Project-URL: Source, https://github.com/grscheller/pythonic-fp-gadgets
|
|
23
23
|
Provides-Extra: test
|
|
24
24
|
|
|
@@ -29,22 +29,25 @@ PyPI project
|
|
|
29
29
|
`pythonic-fp-gadgets
|
|
30
30
|
<https://pypi.org/project/pythonic-fp>`_.
|
|
31
31
|
|
|
32
|
-
Library of simple, but useful,
|
|
32
|
+
Library of simple, but useful, classes and functions with no dependencies
|
|
33
|
+
outside the Python Standard Library.
|
|
33
34
|
|
|
34
|
-
-
|
|
35
|
-
-
|
|
36
|
-
-
|
|
35
|
+
- Gadgets
|
|
36
|
+
- single item box
|
|
37
|
+
- function returning iterator of its arguments
|
|
38
|
+
- find the latest common ancestor of two classes
|
|
37
39
|
|
|
38
|
-
|
|
39
|
-
`pythonic-fp
|
|
40
|
-
<https://github.
|
|
40
|
+
Part of the
|
|
41
|
+
`pythonic-fp
|
|
42
|
+
<https://grscheller.github.io/pythonic-fp>`_
|
|
43
|
+
PyPI projects.
|
|
41
44
|
|
|
42
45
|
Documentation
|
|
43
46
|
-------------
|
|
44
47
|
|
|
45
48
|
Documentation for this project is hosted on
|
|
46
49
|
`GitHub Pages
|
|
47
|
-
<https://grscheller.github.io/pythonic-fp/gadgets
|
|
50
|
+
<https://grscheller.github.io/pythonic-fp/gadgets>`_.
|
|
48
51
|
|
|
49
52
|
Copyright and License
|
|
50
53
|
---------------------
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
pythonic_fp/gadgets/__init__.py,sha256=PXZJRwsDkGrI2_B3nbwU4n3tzeSGONquV-_inWroxsg,2811
|
|
2
|
+
pythonic_fp/gadgets/__init__.pyi,sha256=cAf10WX2ZHMs-Tf93mImmDdZw7lNEmwjorkiVF04s_E,224
|
|
3
|
+
pythonic_fp/gadgets/box.py,sha256=aSR9V_PuAhjqQ8Vs7fj80zgirb8BWzkUFr1pWvSbBDc,4994
|
|
4
|
+
pythonic_fp/gadgets/box.pyi,sha256=UkJBY8Xiv4oe70Q3bhJL1jGHwYmFWwkTIdxvf2zR3v4,837
|
|
5
|
+
pythonic_fp/gadgets/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
6
|
+
pythonic_fp/gadgets/wrap.py,sha256=5FonuzJ6E3SeUoneUoUsjM6x9COZUnyzWQHPKN45cRo,3259
|
|
7
|
+
pythonic_fp/gadgets/wrap.pyi,sha256=TRbBph_517VNCKchwU-pdtPCn73c09gq8EchDFM7Uyo,868
|
|
8
|
+
pythonic_fp/gadgets/sentinels/__init__.py,sha256=81T-HGQ9BgXj57PO_pduo8MpSQpRgZwT8LIXwODhHkI,724
|
|
9
|
+
pythonic_fp/gadgets/sentinels/__init__.pyi,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
10
|
+
pythonic_fp/gadgets/sentinels/flavored.py,sha256=RMqDRmN_ImbRlf_Yaqj7jwnOc6YBeEdAfi8xGCyw1xs,2195
|
|
11
|
+
pythonic_fp/gadgets/sentinels/flavored.pyi,sha256=K3YSyXLyylPII2ahRP0olhzLJruOeR73TFJDC6rCUwk,212
|
|
12
|
+
pythonic_fp/gadgets/sentinels/novalue.py,sha256=RA5foyWFgSHCpaJPJ0N0Td7cldLEbwX_bzkTgs_ZW-o,3573
|
|
13
|
+
pythonic_fp/gadgets/sentinels/novalue.pyi,sha256=zhZdePpWoQ3ZXOq7xQWGR0A4W_XzasdBQKcoywXZEZI,173
|
|
14
|
+
pythonic_fp_gadgets-4.0.0.dist-info/licenses/LICENSE,sha256=psuoW8kuDP96RQsdhzwOqi6fyWv0ct8CR6Jr7He_P_k,10173
|
|
15
|
+
pythonic_fp_gadgets-4.0.0.dist-info/WHEEL,sha256=G2gURzTEtmeR8nrdXUJfNiB3VYVxigPQ-bEQujpNiNs,82
|
|
16
|
+
pythonic_fp_gadgets-4.0.0.dist-info/METADATA,sha256=sbyH2GplSHaJ52be21JwZasacpNr5v2bG72MIvWQMVA,1827
|
|
17
|
+
pythonic_fp_gadgets-4.0.0.dist-info/RECORD,,
|
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
# Copyright 2023-2025 Geoffrey R. Scheller
|
|
2
|
-
#
|
|
3
|
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
-
# you may not use this file except in compliance with the License.
|
|
5
|
-
# You may obtain a copy of the License at
|
|
6
|
-
#
|
|
7
|
-
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
-
#
|
|
9
|
-
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
-
# See the License for the specific language governing permissions and
|
|
13
|
-
# limitations under the License.
|
|
14
|
-
|
|
15
|
-
"""Last Common Ancestor of Two Classes."""
|
|
16
|
-
|
|
17
|
-
from inspect import getmro
|
|
18
|
-
|
|
19
|
-
__all__ = ['lca']
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
def lca(cls1: type, cls2: type) -> type:
|
|
23
|
-
"""Find the least upper bound in the inheritance graph
|
|
24
|
-
of two classes.
|
|
25
|
-
|
|
26
|
-
.. warning::
|
|
27
|
-
|
|
28
|
-
This function can fail with a TypeError. Some error messages
|
|
29
|
-
seen are
|
|
30
|
-
|
|
31
|
-
- multiple bases have instance lay-out conflict
|
|
32
|
-
- type 'bool' is not an acceptable base type
|
|
33
|
-
|
|
34
|
-
This happens frequently when the function is given
|
|
35
|
-
Python builtin types.
|
|
36
|
-
|
|
37
|
-
:param cls1: first class
|
|
38
|
-
:param cls2: second class
|
|
39
|
-
:returns: Least common ancestor base class of ``cls1`` and ``cls2``.
|
|
40
|
-
:raises TypeError: theoretically only from ``inspect.getmto``
|
|
41
|
-
|
|
42
|
-
"""
|
|
43
|
-
if issubclass(cls1, cls2):
|
|
44
|
-
return cls2
|
|
45
|
-
if issubclass(cls2, cls1):
|
|
46
|
-
return cls1
|
|
47
|
-
|
|
48
|
-
for common_ancestor in getmro(type('LcaDiamondClass', (cls1, cls2), {})):
|
|
49
|
-
if issubclass(cls1, common_ancestor) and issubclass(cls2, common_ancestor):
|
|
50
|
-
return common_ancestor
|
|
51
|
-
raise TypeError("latest_common_ancestor: no common ancestor found!!!")
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
pythonic_fp/gadgets/__init__.py,sha256=2nrTFkbHHW2ZbbEwmMypXrU5aO70JwLZrQt131pdF7E,1755
|
|
2
|
-
pythonic_fp/gadgets/box.py,sha256=VR_MOCRDH5tyM6BaM7uFcHW-jKqDJlxfb_WbxKuZ6fs,5244
|
|
3
|
-
pythonic_fp/gadgets/box.pyi,sha256=4TAYuLnnxzXsADX8JwblsH-020pYkXJRsPqeDqkK44Q,813
|
|
4
|
-
pythonic_fp/gadgets/iterate_arguments.py,sha256=7gFQgqn8vpBQIxOPYlpAgW26Fyzfjnux3Y2cxHYZuSM,1057
|
|
5
|
-
pythonic_fp/gadgets/iterate_arguments.pyi,sha256=BXyRZUTToIVQls9jpIbRGAtp8PCmZ-onJeKH29UzQkM,98
|
|
6
|
-
pythonic_fp/gadgets/latest_common_ancestor.py,sha256=CHN5q82p8n05XPuA5rTq9gwtOkvuz9U-QMJEqOjlqqM,1689
|
|
7
|
-
pythonic_fp/gadgets/latest_common_ancestor.pyi,sha256=dDta0kOY7T4mjPknJnMWDV9X4pveW4euxi_0V176hbc,64
|
|
8
|
-
pythonic_fp/gadgets/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
9
|
-
pythonic_fp_gadgets-3.0.1.dist-info/licenses/LICENSE,sha256=psuoW8kuDP96RQsdhzwOqi6fyWv0ct8CR6Jr7He_P_k,10173
|
|
10
|
-
pythonic_fp_gadgets-3.0.1.dist-info/WHEEL,sha256=G2gURzTEtmeR8nrdXUJfNiB3VYVxigPQ-bEQujpNiNs,82
|
|
11
|
-
pythonic_fp_gadgets-3.0.1.dist-info/METADATA,sha256=35w8kJi-GcUlz4Nd8mnBhetFESi5nqbI9AvRzf4hB4w,1828
|
|
12
|
-
pythonic_fp_gadgets-3.0.1.dist-info/RECORD,,
|
|
File without changes
|
{pythonic_fp_gadgets-3.0.1.dist-info → pythonic_fp_gadgets-4.0.0.dist-info}/licenses/LICENSE
RENAMED
|
File without changes
|