vt-commons 0.0.1__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.
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env python3
2
+ # coding=utf-8
3
+
4
+ """
5
+ Reusable common utilities, interfaces and implementations for python projects.
6
+ """
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env python3
2
+ # coding=utf-8
3
+
4
+ """
5
+ Reusable common utilities, interfaces and implementations for python projects related to collections.
6
+ """
7
+
8
+ from vt.utils.commons.commons.collections.utils import get_first_true as get_first_true
9
+ from vt.utils.commons.commons.collections.utils import get_last_true as get_last_true
@@ -0,0 +1,110 @@
1
+ #!/usr/bin/env python3
2
+ # coding=utf-8
3
+
4
+ """
5
+ Reusable common utilities for python projects related to collections.
6
+ """
7
+
8
+ from collections.abc import Sequence, Callable, Iterator
9
+
10
+
11
+ def get_first_true[T](
12
+ ids: Sequence[T],
13
+ default_val: T,
14
+ predicate: Callable[[T], bool],
15
+ iter_provider: Callable[[Sequence[T]], Iterator[T]] = iter,
16
+ ) -> T:
17
+ """
18
+ Get the first id which returns ``True`` from the supplied ``predicate`` else get the ``default_val``.
19
+
20
+ Examples:
21
+
22
+ * First id is returned if all ``ids`` are inferred as ``True``::
23
+
24
+ >>> get_first_true([1, 2, 3], -1, lambda x: True)
25
+ 1
26
+
27
+ * Default id is returned if no ``ids`` are inferred as ``True``::
28
+
29
+ >>> get_first_true([1, 2, 3], -1, lambda x: False)
30
+ -1
31
+
32
+ Error scenarios:
33
+
34
+ * supplied predicate is not a callable::
35
+
36
+ >>> get_first_true([1, 2, 3], -1,
37
+ ... object()) # noqa: to avoid typecheck warnings, expects (x)->bool Callable
38
+ Traceback (most recent call last):
39
+ TypeError: predicate must be a (x) -> bool Callable. Supplied <class 'object'>.
40
+
41
+ * suppplied predicate is a non-conforming callable::
42
+
43
+ >>> get_first_true([1, 2, 3], -1,
44
+ ... lambda x, y: True) # noqa: to avoid typecheck warnings, expects (x)->bool Callable.
45
+ Traceback (most recent call last):
46
+ TypeError: <lambda>() missing 1 required positional argument: 'y'
47
+
48
+ :param ids: sequence of id(s) from which the first ever ``predicate`` determined truthy id is to be found.
49
+ :param default_val: value returned if no id is found as truthy from the ``ids`` list according to the supplied
50
+ ``predicate``.
51
+ :param predicate: predicate to determine whether an id is inferred as satisfying and hence returning ``True`` for
52
+ that id.
53
+ :param iter_provider: iterator provider for the ``ids`` sequence.
54
+ :return: the first id from the list of ``ids`` which returns ``True`` by the ``predicate`` or ``default_val`` if
55
+ the ``predicate`` returns ``True`` for no id(s).
56
+ """
57
+ if not callable(predicate):
58
+ raise TypeError(
59
+ f"predicate must be a (x) -> bool Callable. Supplied {type(predicate)}."
60
+ )
61
+
62
+ for _id in iter_provider(ids):
63
+ if predicate(_id):
64
+ return _id
65
+ return default_val
66
+
67
+
68
+ def get_last_true[T](
69
+ ids: Sequence[T], default_val: T, predicate: Callable[[T], bool]
70
+ ) -> T:
71
+ """
72
+ Get the last id which returns ``True`` from the supplied ``predicate`` else get the ``default_val``.
73
+
74
+ Examples:
75
+
76
+ * Last id is returned if all ``ids`` are inferred as ``True``::
77
+
78
+ >>> get_last_true([1, 2, 3], -1, lambda x: True)
79
+ 3
80
+
81
+ * Default id is returned if no ``ids`` are inferred as ``True``::
82
+
83
+ >>> get_last_true([1, 2, 3], -1, lambda x: False)
84
+ -1
85
+
86
+ Error scenarios:
87
+
88
+ * supplied predicate is not a callable::
89
+
90
+ >>> get_last_true([1, 2, 3], -1,
91
+ ... object()) # noqa: to avoid typecheck warnings, expects (x)->bool Callable
92
+ Traceback (most recent call last):
93
+ TypeError: predicate must be a (x) -> bool Callable. Supplied <class 'object'>.
94
+
95
+ * suppplied predicate is a non-conforming callable::
96
+
97
+ >>> get_last_true([1, 2, 3], -1,
98
+ ... lambda x, y: True) # noqa: to avoid typecheck warnings, expects (x)->bool Callable.
99
+ Traceback (most recent call last):
100
+ TypeError: <lambda>() missing 1 required positional argument: 'y'
101
+
102
+ :param ids: sequence of id(s) from which the last ``predicate`` determined truthy id is to be found.
103
+ :param default_val: value returned if no id is found as truthy from the ``ids`` list according to the supplied
104
+ ``predicate``.
105
+ :param predicate: predicate to determine whether an id is inferred as satisfying and hence returning ``True`` for
106
+ that id.
107
+ :return: the last id from the list of ``ids`` which returns ``True`` by the ``predicate`` or ``default_val`` if
108
+ the ``predicate`` returns ``True`` for no id(s).
109
+ """
110
+ return get_first_true(ids, default_val, predicate, reversed)
@@ -0,0 +1,42 @@
1
+ #!/usr/bin/env python3
2
+ # coding=utf-8
3
+
4
+ """
5
+ Reusable interfaces related to core python.
6
+ """
7
+
8
+ # region Base interfaces
9
+ from vt.utils.commons.commons.core_py.base import MISSING as MISSING
10
+ from vt.utils.commons.commons.core_py.base import Missing as Missing
11
+ from vt.utils.commons.commons.core_py.base import UNSET as UNSET
12
+ from vt.utils.commons.commons.core_py.base import Unset as Unset
13
+ # endregion
14
+
15
+ # region utility functions
16
+ from vt.utils.commons.commons.core_py.utils import (
17
+ has_atleast_one_arg as has_atleast_one_arg,
18
+ )
19
+ from vt.utils.commons.commons.core_py.utils import (
20
+ ensure_atleast_one_arg as ensure_atleast_one_arg,
21
+ )
22
+ from vt.utils.commons.commons.core_py.utils import (
23
+ not_none_not_unset as not_none_not_unset,
24
+ )
25
+ from vt.utils.commons.commons.core_py.utils import is_unset as is_unset
26
+ from vt.utils.commons.commons.core_py.utils import alt_if_unset as alt_if_unset
27
+ from vt.utils.commons.commons.core_py.utils import alt_if_missing as alt_if_missing
28
+ from vt.utils.commons.commons.core_py.utils import is_missing as is_missing
29
+ from vt.utils.commons.commons.core_py.utils import alt_if_ellipses as alt_if_ellipses
30
+ from vt.utils.commons.commons.core_py.utils import is_ellipses as is_ellipses
31
+ from vt.utils.commons.commons.core_py.utils import fallback_on_none as fallback_on_none
32
+ from vt.utils.commons.commons.core_py.utils import (
33
+ fallback_on_none_strict as fallback_on_none_strict,
34
+ )
35
+ from vt.utils.commons.commons.core_py.utils import (
36
+ not_none_not_missing as not_none_not_missing,
37
+ )
38
+ from vt.utils.commons.commons.core_py.utils import (
39
+ not_none_not_sentinel as not_none_not_sentinel,
40
+ )
41
+ from vt.utils.commons.commons.core_py.utils import strictly_int as strictly_int
42
+ # endregion
@@ -0,0 +1,47 @@
1
+ #!/usr/bin/env python3
2
+ # coding=utf-8
3
+
4
+ """
5
+ Reusable interfaces and sentinel objects related to core python.
6
+ """
7
+
8
+ from typing import Final
9
+
10
+
11
+ class Sentinel:
12
+ """
13
+ Class denoting sentinel values.
14
+ """
15
+
16
+ pass
17
+
18
+
19
+ class Missing(Sentinel):
20
+ """
21
+ A Sentinel type to represent a missing value. Can be used:
22
+
23
+ * as default value for a parameter which has ``None`` as a valid value.
24
+ """
25
+
26
+ pass
27
+
28
+
29
+ class Unset(Sentinel):
30
+ """
31
+ Sentinel type that can be used to unset a previously set value.
32
+ """
33
+
34
+ pass
35
+
36
+
37
+ MISSING: Final[Missing] = Missing()
38
+ """
39
+ Sentinel to represent a missing value. Can be used:
40
+
41
+ * as default value for a parameter which has ``None`` as a valid value.
42
+ """
43
+
44
+ UNSET: Final[Unset] = Unset()
45
+ """
46
+ Sentinel that can be used to unset a previously set value.
47
+ """