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.
- vt/utils/commons/commons/__init__.py +6 -0
- vt/utils/commons/commons/collections/__init__.py +9 -0
- vt/utils/commons/commons/collections/utils.py +110 -0
- vt/utils/commons/commons/core_py/__init__.py +42 -0
- vt/utils/commons/commons/core_py/base.py +47 -0
- vt/utils/commons/commons/core_py/utils.py +827 -0
- vt/utils/commons/commons/op/__init__.py +11 -0
- vt/utils/commons/commons/op/base.py +121 -0
- vt/utils/commons/commons/os/__init__.py +18 -0
- vt/utils/commons/commons/os/_base_utils.py +17 -0
- vt/utils/commons/commons/os/linux.py +29 -0
- vt/utils/commons/commons/os/mac.py +29 -0
- vt/utils/commons/commons/os/posix.py +30 -0
- vt/utils/commons/commons/os/windows.py +29 -0
- vt/utils/commons/commons/py.typed +0 -0
- vt/utils/commons/commons/state/__init__.py +13 -0
- vt/utils/commons/commons/state/done.py +163 -0
- vt_commons-0.0.1.dist-info/METADATA +112 -0
- vt_commons-0.0.1.dist-info/RECORD +22 -0
- vt_commons-0.0.1.dist-info/WHEEL +5 -0
- vt_commons-0.0.1.dist-info/licenses/LICENSE +201 -0
- vt_commons-0.0.1.dist-info/top_level.txt +1 -0
@@ -0,0 +1,11 @@
|
|
1
|
+
#!/usr/bin/env python3
|
2
|
+
# coding=utf-8
|
3
|
+
|
4
|
+
"""
|
5
|
+
Reusable common utilities, interfaces and implementations for python projects related to operations.
|
6
|
+
"""
|
7
|
+
|
8
|
+
from vt.utils.commons.commons.op.base import RootDirOp as RootDirOp
|
9
|
+
from vt.utils.commons.commons.op.base import CWDRootDirOp as CWDRootDirOp
|
10
|
+
from vt.utils.commons.commons.op.base import RootDirOps as RootDirOps
|
11
|
+
from vt.utils.commons.commons.op.base import ReversibleOp as ReversibleOp
|
@@ -0,0 +1,121 @@
|
|
1
|
+
#!/usr/bin/env python3
|
2
|
+
# coding=utf-8
|
3
|
+
|
4
|
+
"""
|
5
|
+
Reusable interfaces for python projects related to operations.
|
6
|
+
"""
|
7
|
+
|
8
|
+
from __future__ import annotations
|
9
|
+
from abc import abstractmethod
|
10
|
+
from pathlib import Path
|
11
|
+
from typing import Protocol, override, final
|
12
|
+
|
13
|
+
|
14
|
+
class ReversibleOp(Protocol):
|
15
|
+
"""
|
16
|
+
Operation that can be reversed or act in the reversed mode.
|
17
|
+
"""
|
18
|
+
|
19
|
+
@property
|
20
|
+
@abstractmethod
|
21
|
+
def rev(self) -> bool:
|
22
|
+
"""
|
23
|
+
:return: whether current operation is operating in the reverse mode.
|
24
|
+
"""
|
25
|
+
...
|
26
|
+
|
27
|
+
|
28
|
+
# region Root dir related operations
|
29
|
+
class RootDirOp(Protocol):
|
30
|
+
"""
|
31
|
+
Perform operations on the ``root_dir``.
|
32
|
+
"""
|
33
|
+
|
34
|
+
@property
|
35
|
+
@abstractmethod
|
36
|
+
def root_dir(self) -> Path:
|
37
|
+
"""
|
38
|
+
:return: Path to the ``root_dir`` root directory for this operation.
|
39
|
+
"""
|
40
|
+
...
|
41
|
+
|
42
|
+
|
43
|
+
class CWDRootDirOp(RootDirOp):
|
44
|
+
def __init__(self, root_dir=Path.cwd()):
|
45
|
+
"""
|
46
|
+
Perform operations on the root_dir.
|
47
|
+
|
48
|
+
:param root_dir: the path to the root directory.
|
49
|
+
"""
|
50
|
+
self._root_dir = root_dir
|
51
|
+
|
52
|
+
@override
|
53
|
+
@property
|
54
|
+
def root_dir(self) -> Path:
|
55
|
+
return self._root_dir
|
56
|
+
|
57
|
+
|
58
|
+
@final
|
59
|
+
class RootDirOps:
|
60
|
+
"""
|
61
|
+
A factory-like class for ``RootDirOp``.
|
62
|
+
"""
|
63
|
+
|
64
|
+
@staticmethod
|
65
|
+
def strictly_one_required(
|
66
|
+
root_dir: Path | None = None,
|
67
|
+
root_dir_op: RootDirOp | None = None,
|
68
|
+
*,
|
69
|
+
root_dir_str: str = "root_dir",
|
70
|
+
root_dir_op_str: str = "root_dir_op",
|
71
|
+
):
|
72
|
+
"""
|
73
|
+
Convenience method to raise ``ValueError`` when both ``root_dir`` and ``root_dir_op`` are supplied.
|
74
|
+
|
75
|
+
Examples:
|
76
|
+
|
77
|
+
* OK: only root-dir supplied:
|
78
|
+
|
79
|
+
>>> RootDirOps.strictly_one_required(Path.cwd())
|
80
|
+
|
81
|
+
* OK: only root-dir-op supplied:
|
82
|
+
|
83
|
+
>>> RootDirOps.strictly_one_required(root_dir_op=RootDirOps.from_path(Path('tmp')))
|
84
|
+
|
85
|
+
* At least one of ``root_dir`` or ``root_dir_op`` must be provided:
|
86
|
+
|
87
|
+
>>> RootDirOps.strictly_one_required(None, None)
|
88
|
+
Traceback (most recent call last):
|
89
|
+
ValueError: Either root_dir or root_dir_op is required.
|
90
|
+
|
91
|
+
* Both ``root_dir`` or ``root_dir_op`` cannot be provided:
|
92
|
+
|
93
|
+
>>> RootDirOps.strictly_one_required(root_dir=Path.cwd(), root_dir_op=RootDirOps.from_path(Path('tmp')))
|
94
|
+
Traceback (most recent call last):
|
95
|
+
ValueError: root_dir and root_dir_op are not allowed together.
|
96
|
+
|
97
|
+
:param root_dir: path to the root directory.
|
98
|
+
:param root_dir_op: object that has path to the root directory.
|
99
|
+
:param root_dir_str: variable name string for overriding the default ``root_op`` variable name in error
|
100
|
+
messages.
|
101
|
+
:param root_dir_op_str: variable name string for overriding the default ``root_dir_op`` variable name in error
|
102
|
+
messages.
|
103
|
+
:raises ValueError: when both ``root_dir`` and ``root_dir_op`` are supplied.
|
104
|
+
"""
|
105
|
+
if root_dir is None and root_dir_op is None:
|
106
|
+
raise ValueError(f"Either {root_dir_str} or {root_dir_op_str} is required.")
|
107
|
+
if root_dir and root_dir_op:
|
108
|
+
raise ValueError(
|
109
|
+
f"{root_dir_str} and {root_dir_op_str} are not allowed together."
|
110
|
+
)
|
111
|
+
|
112
|
+
@staticmethod
|
113
|
+
def from_path(root_dir: Path = Path.cwd()) -> CWDRootDirOp:
|
114
|
+
"""
|
115
|
+
:param root_dir: path to root-dir.
|
116
|
+
:return: a root dir operation for the supplied path.
|
117
|
+
"""
|
118
|
+
return CWDRootDirOp(root_dir)
|
119
|
+
|
120
|
+
|
121
|
+
# endregion
|
@@ -0,0 +1,18 @@
|
|
1
|
+
#!/usr/bin/env python3
|
2
|
+
# coding=utf-8
|
3
|
+
|
4
|
+
"""
|
5
|
+
Reusable common utilities, interfaces and implementations for python projects related to operating systems.
|
6
|
+
"""
|
7
|
+
|
8
|
+
from vt.utils.commons.commons.os.windows import is_windows as is_windows
|
9
|
+
from vt.utils.commons.commons.os.windows import not_windows as not_windows
|
10
|
+
|
11
|
+
from vt.utils.commons.commons.os.linux import is_linux as is_linux
|
12
|
+
from vt.utils.commons.commons.os.linux import not_linux as not_linux
|
13
|
+
|
14
|
+
from vt.utils.commons.commons.os.mac import is_mac as is_mac
|
15
|
+
from vt.utils.commons.commons.os.mac import not_mac as not_mac
|
16
|
+
|
17
|
+
from vt.utils.commons.commons.os.posix import is_posix as is_posix
|
18
|
+
from vt.utils.commons.commons.os.posix import not_posix as not_posix
|
@@ -0,0 +1,17 @@
|
|
1
|
+
#!/usr/bin/env python3
|
2
|
+
# coding=utf-8
|
3
|
+
|
4
|
+
"""
|
5
|
+
Internal reusable common utilities, interfaces and implementations for python projects related to operating systems.
|
6
|
+
"""
|
7
|
+
|
8
|
+
import os
|
9
|
+
import platform
|
10
|
+
|
11
|
+
|
12
|
+
def _is_platform(platform_name: str) -> bool:
|
13
|
+
return platform.system() == platform_name
|
14
|
+
|
15
|
+
|
16
|
+
def _is_os_name(os_name: str) -> bool:
|
17
|
+
return os.name == os_name
|
@@ -0,0 +1,29 @@
|
|
1
|
+
#!/usr/bin/env python3
|
2
|
+
# coding=utf-8
|
3
|
+
|
4
|
+
"""
|
5
|
+
Reusable common utilities, interfaces and implementations for python projects related to Linux operating system.
|
6
|
+
"""
|
7
|
+
|
8
|
+
from vt.utils.commons.commons.os._base_utils import _is_platform
|
9
|
+
|
10
|
+
LINUX_ID_STR = "Linux"
|
11
|
+
"""
|
12
|
+
Linux OS determined by python using this string.
|
13
|
+
|
14
|
+
https://docs.python.org/3/library/platform.html#platform.system
|
15
|
+
"""
|
16
|
+
|
17
|
+
|
18
|
+
def not_linux() -> bool:
|
19
|
+
"""
|
20
|
+
:return: ``True`` if system is not linux. ``False`` otherwise.
|
21
|
+
"""
|
22
|
+
return not is_linux()
|
23
|
+
|
24
|
+
|
25
|
+
def is_linux() -> bool:
|
26
|
+
"""
|
27
|
+
:return: ``True`` if system is linux. ``False`` otherwise.
|
28
|
+
"""
|
29
|
+
return _is_platform(LINUX_ID_STR)
|
@@ -0,0 +1,29 @@
|
|
1
|
+
#!/usr/bin/env python3
|
2
|
+
# coding=utf-8
|
3
|
+
|
4
|
+
"""
|
5
|
+
Reusable common utilities, interfaces and implementations for python projects related to Mac operating system.
|
6
|
+
"""
|
7
|
+
|
8
|
+
from vt.utils.commons.commons.os._base_utils import _is_platform
|
9
|
+
|
10
|
+
MAC_ID_STR = "Darwin"
|
11
|
+
"""
|
12
|
+
Mac OS determined by python using this string.
|
13
|
+
|
14
|
+
https://docs.python.org/3/library/platform.html#platform.system
|
15
|
+
"""
|
16
|
+
|
17
|
+
|
18
|
+
def not_mac() -> bool:
|
19
|
+
"""
|
20
|
+
:return: ``True`` if system is not mac. ``False`` otherwise.
|
21
|
+
"""
|
22
|
+
return not is_mac()
|
23
|
+
|
24
|
+
|
25
|
+
def is_mac() -> bool:
|
26
|
+
"""
|
27
|
+
:return: ``True`` if system is mac. ``False`` otherwise.
|
28
|
+
"""
|
29
|
+
return _is_platform(MAC_ID_STR)
|
@@ -0,0 +1,30 @@
|
|
1
|
+
#!/usr/bin/env python3
|
2
|
+
# coding=utf-8
|
3
|
+
|
4
|
+
"""
|
5
|
+
Reusable common utilities, interfaces and implementations for python projects related to POSIX operating system
|
6
|
+
families.
|
7
|
+
"""
|
8
|
+
|
9
|
+
from vt.utils.commons.commons.os._base_utils import _is_os_name
|
10
|
+
|
11
|
+
POSIX_ID_STR = "posix"
|
12
|
+
"""
|
13
|
+
POSIX OS families are determined by python using this string.
|
14
|
+
|
15
|
+
https://docs.python.org/3/library/os.html#os.name
|
16
|
+
"""
|
17
|
+
|
18
|
+
|
19
|
+
def not_posix() -> bool:
|
20
|
+
"""
|
21
|
+
:return: ``True`` if system is not POSIX. ``False`` otherwise.
|
22
|
+
"""
|
23
|
+
return not is_posix()
|
24
|
+
|
25
|
+
|
26
|
+
def is_posix() -> bool:
|
27
|
+
"""
|
28
|
+
:return: ``True`` if system is POSIX. ``False`` otherwise.
|
29
|
+
"""
|
30
|
+
return _is_os_name(POSIX_ID_STR)
|
@@ -0,0 +1,29 @@
|
|
1
|
+
#!/usr/bin/env python3
|
2
|
+
# coding=utf-8
|
3
|
+
|
4
|
+
"""
|
5
|
+
Reusable common utilities, interfaces and implementations for python projects related to Windows operating system.
|
6
|
+
"""
|
7
|
+
|
8
|
+
from vt.utils.commons.commons.os._base_utils import _is_platform
|
9
|
+
|
10
|
+
WINDOWS_ID_STR = "Windows"
|
11
|
+
"""
|
12
|
+
Windows OS determined by python using this string.
|
13
|
+
|
14
|
+
https://docs.python.org/3/library/platform.html#platform.system
|
15
|
+
"""
|
16
|
+
|
17
|
+
|
18
|
+
def not_windows() -> bool:
|
19
|
+
"""
|
20
|
+
:return: ``True`` if system is not windows. ``False`` otherwise.
|
21
|
+
"""
|
22
|
+
return not is_windows()
|
23
|
+
|
24
|
+
|
25
|
+
def is_windows() -> bool:
|
26
|
+
"""
|
27
|
+
:return: ``True`` if system is windows. ``False`` otherwise.
|
28
|
+
"""
|
29
|
+
return _is_platform(WINDOWS_ID_STR)
|
File without changes
|
@@ -0,0 +1,13 @@
|
|
1
|
+
#!/usr/bin/env python3
|
2
|
+
# coding=utf-8
|
3
|
+
|
4
|
+
"""
|
5
|
+
Reusable common utilities, interfaces and implementations for python projects related to states and state variables.
|
6
|
+
"""
|
7
|
+
|
8
|
+
from vt.utils.commons.commons.state.done import DoneEnquirer as DoneEnquirer
|
9
|
+
from vt.utils.commons.commons.state.done import DoneMarker as DoneMarker
|
10
|
+
from vt.utils.commons.commons.state.done import DoneVisitor as DoneVisitor
|
11
|
+
from vt.utils.commons.commons.state.done import (
|
12
|
+
DelegatingDoneVisitor as DelegatingDoneVisitor,
|
13
|
+
)
|
@@ -0,0 +1,163 @@
|
|
1
|
+
#!/usr/bin/env python3
|
2
|
+
# coding=utf-8
|
3
|
+
|
4
|
+
"""
|
5
|
+
Reusable common utilities, interfaces and implementations for python projects related to states and state variables
|
6
|
+
marking and enquiring ``done`` state.
|
7
|
+
"""
|
8
|
+
|
9
|
+
from abc import abstractmethod
|
10
|
+
from collections.abc import Sequence
|
11
|
+
from typing import Protocol, override
|
12
|
+
|
13
|
+
from vt.utils.commons.commons.collections import get_first_true, get_last_true
|
14
|
+
|
15
|
+
|
16
|
+
class DoneMarker[T](Protocol):
|
17
|
+
"""
|
18
|
+
Interface to facilitate marking as operation as ``done``.
|
19
|
+
"""
|
20
|
+
|
21
|
+
@abstractmethod
|
22
|
+
def mark_done(self, _id: T) -> bool:
|
23
|
+
"""
|
24
|
+
Mark an ``_id`` as ``done`` in the system. Returns ``True`` when ``_id`` is marked ``done`` to represent a
|
25
|
+
logical truthy and done state.
|
26
|
+
|
27
|
+
:param _id: any id.
|
28
|
+
:return: ``True`` if marking is done, ``False`` if marking is not to be done for a dry-run or if ``_id``
|
29
|
+
is already marked done.
|
30
|
+
:raise Exception: on underlying system error.
|
31
|
+
"""
|
32
|
+
...
|
33
|
+
|
34
|
+
|
35
|
+
class DoneEnquirer[T](Protocol):
|
36
|
+
"""
|
37
|
+
Interface to facilitate checkin whether an operation is marked as ``done``.
|
38
|
+
"""
|
39
|
+
|
40
|
+
@abstractmethod
|
41
|
+
def is_done(self, _id: T) -> bool:
|
42
|
+
"""
|
43
|
+
Check whether the supplied ``_id`` is marked ``done`` in the system. Returns ``True`` when ``_id`` is marked
|
44
|
+
``done`` to represent a logical truthy and done state.
|
45
|
+
|
46
|
+
:param _id: any id.
|
47
|
+
:return: ``True`` if marking is done, ``False`` if marking is not done.
|
48
|
+
:raise Exception: on underlying system error.
|
49
|
+
"""
|
50
|
+
...
|
51
|
+
|
52
|
+
def get_first_done(self, ids: Sequence[T], default_val: T) -> T:
|
53
|
+
"""
|
54
|
+
Get the first id which is marked as ``done`` else get the ``default_val``.
|
55
|
+
|
56
|
+
Examples:
|
57
|
+
|
58
|
+
* Doctest setup:
|
59
|
+
>>> from typing import Any
|
60
|
+
>>> class AlwaysDone(DoneEnquirer[Any]):
|
61
|
+
... def __init__(self, marking: bool):
|
62
|
+
... self.marking = marking
|
63
|
+
...
|
64
|
+
... def is_done(self, _id: Any) -> bool:
|
65
|
+
... return self.marking
|
66
|
+
|
67
|
+
* First id is returned if all are marked done::
|
68
|
+
|
69
|
+
>>> AlwaysDone(True).get_first_done([1, 2, 3], -1)
|
70
|
+
1
|
71
|
+
|
72
|
+
* Default id is returned if none are marked done::
|
73
|
+
|
74
|
+
>>> AlwaysDone(False).get_first_done([1, 2, 3], -1)
|
75
|
+
-1
|
76
|
+
|
77
|
+
:param ids: sequence of id(s) from which the first ever ``done`` id is to be found.
|
78
|
+
:param default_val: value returned if no id is marked as ``done`` from the ``ids`` list.
|
79
|
+
:return: the first id from the list of ``ids`` which is marked ``done`` by the ``done_enquirer`` or
|
80
|
+
``default_val`` if no id was identified as ``done``.
|
81
|
+
"""
|
82
|
+
return get_first_true(ids, default_val, self.is_done)
|
83
|
+
|
84
|
+
def get_last_done(self, ids: Sequence[T], default_val: T) -> T:
|
85
|
+
"""
|
86
|
+
Get the last id which is marked as ``done`` else get the ``default_val``.
|
87
|
+
|
88
|
+
Examples:
|
89
|
+
|
90
|
+
* Doctest setup:
|
91
|
+
|
92
|
+
>>> from typing import Any
|
93
|
+
>>> class AlwaysDone(DoneEnquirer[Any]):
|
94
|
+
... def __init__(self, marking: bool):
|
95
|
+
... self.marking = marking
|
96
|
+
...
|
97
|
+
... def is_done(self, _id: Any) -> bool:
|
98
|
+
... return self.marking
|
99
|
+
|
100
|
+
* Last id is returned if all are marked done::
|
101
|
+
|
102
|
+
>>> AlwaysDone(True).get_last_done([1, 2, 3], -1)
|
103
|
+
3
|
104
|
+
|
105
|
+
* Default id is returned if none are marked done::
|
106
|
+
|
107
|
+
>>> AlwaysDone(False).get_last_done([1, 2, 3], -1)
|
108
|
+
-1
|
109
|
+
|
110
|
+
:param ids: sequence of id(s) from which the last ``done`` id is to be found.
|
111
|
+
:param default_val: value returned if no id is marked as ``done`` from the ``ids`` list.
|
112
|
+
:return: the last id from the list of ``ids`` which is marked ``done`` by the ``done_enquirer`` or
|
113
|
+
``default_val`` if no id was identified as ``done``.
|
114
|
+
"""
|
115
|
+
return get_last_true(ids, default_val, self.is_done)
|
116
|
+
|
117
|
+
|
118
|
+
class DoneVisitor[T](DoneMarker[T], DoneEnquirer[T], Protocol):
|
119
|
+
"""
|
120
|
+
Interface for:
|
121
|
+
|
122
|
+
* Marking the operation as done. Supplied by ``DoneMarker``.
|
123
|
+
* Checking whether an operation is marked as done. Supplied by ``DoneEnquirer``.
|
124
|
+
"""
|
125
|
+
|
126
|
+
pass
|
127
|
+
|
128
|
+
|
129
|
+
class DelegatingDoneVisitor[T](DoneVisitor[T], Protocol):
|
130
|
+
"""
|
131
|
+
A ``DoneVisitor`` that stores references to the supplied ``DoneMarker`` and ``DoneEnquirer``.
|
132
|
+
|
133
|
+
Created to facilitate composition over inheritance and hence each of its components, i.e.:
|
134
|
+
|
135
|
+
* ``done_marker``
|
136
|
+
* ``done_enquirer``
|
137
|
+
|
138
|
+
Can be initialised at runtime.
|
139
|
+
"""
|
140
|
+
|
141
|
+
@property
|
142
|
+
@abstractmethod
|
143
|
+
def done_marker(self) -> DoneMarker[T]:
|
144
|
+
"""
|
145
|
+
:return: stored ``DoneMarker``.
|
146
|
+
"""
|
147
|
+
...
|
148
|
+
|
149
|
+
@property
|
150
|
+
@abstractmethod
|
151
|
+
def done_enquirer(self) -> DoneEnquirer[T]:
|
152
|
+
"""
|
153
|
+
:return: stored ``DoneEnquirer``.
|
154
|
+
"""
|
155
|
+
...
|
156
|
+
|
157
|
+
@override
|
158
|
+
def mark_done(self, _id: T) -> bool:
|
159
|
+
return self.done_marker.mark_done(_id)
|
160
|
+
|
161
|
+
@override
|
162
|
+
def is_done(self, _id: T) -> bool:
|
163
|
+
return self.done_enquirer.is_done(_id)
|
@@ -0,0 +1,112 @@
|
|
1
|
+
Metadata-Version: 2.4
|
2
|
+
Name: vt-commons
|
3
|
+
Version: 0.0.1
|
4
|
+
Summary: Reusable common utilities, interfaces and implementations for python projects.
|
5
|
+
Author-email: Suhas Krishna Srivastava <suhas.srivastava@vaastav.tech>
|
6
|
+
Maintainer-email: Suhas Krishna Srivastava <suhas.srivastava@vaastav.tech>
|
7
|
+
License-Expression: Apache-2.0
|
8
|
+
Project-URL: homepage, https://github.com/Vaastav-Technologies/py-commons
|
9
|
+
Project-URL: source, https://github.com/Vaastav-Technologies/py-commons
|
10
|
+
Project-URL: issues, https://github.com/Vaastav-Technologies/py-commons/issues
|
11
|
+
Classifier: Development Status :: 1 - Planning
|
12
|
+
Classifier: Intended Audience :: Developers
|
13
|
+
Classifier: Intended Audience :: Education
|
14
|
+
Classifier: Topic :: Education
|
15
|
+
Classifier: Topic :: Software Development :: Libraries
|
16
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
17
|
+
Classifier: Topic :: Utilities
|
18
|
+
Classifier: Programming Language :: Python :: 3.12
|
19
|
+
Classifier: Programming Language :: Python :: 3.13
|
20
|
+
Classifier: Programming Language :: Python :: 3.14
|
21
|
+
Classifier: Programming Language :: Python :: 3.15
|
22
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
23
|
+
Classifier: Typing :: Typed
|
24
|
+
Requires-Python: >=3.12
|
25
|
+
Description-Content-Type: text/markdown
|
26
|
+
License-File: LICENSE
|
27
|
+
Dynamic: license-file
|
28
|
+
|
29
|
+
# vt-commons
|
30
|
+
|
31
|
+
[](https://github.com/Vaastav-Technologies/py-commons/actions/workflows/test.yml)
|
32
|
+
[](https://github.com/Vaastav-Technologies/py-commons/actions/workflows/typecheck.yml)
|
33
|
+
[](https://github.com/Vaastav-Technologies/py-commons/actions/workflows/lint.yml)
|
34
|
+
[](https://codecov.io/gh/Vaastav-Technologies/py-commons)
|
35
|
+
[](https://github.com/Vaastav-Technologies/py-commons/actions/workflows/python-publish.yml)
|
36
|
+
|
37
|
+
---
|
38
|
+
Commons methods, utils, interfaces and implementations for python projects.
|
39
|
+
|
40
|
+
### Install
|
41
|
+
|
42
|
+
```shell
|
43
|
+
pip install vt-commons
|
44
|
+
```
|
45
|
+
|
46
|
+
#### Usage examples
|
47
|
+
|
48
|
+
- Check for OS
|
49
|
+
```python
|
50
|
+
from vt.utils.commons.commons.os import is_windows
|
51
|
+
|
52
|
+
is_windows()
|
53
|
+
```
|
54
|
+
Check in `vt.utils.commons.commons.os` and documentation for more functions and utilities related to OS.
|
55
|
+
- Perform some operation on a root directory
|
56
|
+
```python
|
57
|
+
from vt.utils.commons.commons.op import RootDirOp, CWDRootDirOp, RootDirOps
|
58
|
+
from pathlib import Path
|
59
|
+
|
60
|
+
class MyRootDirectoryOperation(RootDirOp):
|
61
|
+
...
|
62
|
+
@property
|
63
|
+
def root_dir(self)-> Path:
|
64
|
+
return Path('path', 'to', 'my', 'root-directory')
|
65
|
+
|
66
|
+
certain_root_dir_operation: CWDRootDirOp = RootDirOps.from_path(Path('path', 'to', 'my', 'root-directory'))
|
67
|
+
```
|
68
|
+
Check in `vt.utils.commons.commons.op` and documentation for more functions and utilities related to operations.
|
69
|
+
- Perform state operations
|
70
|
+
```python
|
71
|
+
from vt.utils.commons.commons.state import DoneMarker
|
72
|
+
|
73
|
+
# Track state by marking done
|
74
|
+
class MyStateManager(DoneMarker[int]):
|
75
|
+
def __init__(self, *args, **kwargs):
|
76
|
+
self.id_state = {1: False}
|
77
|
+
...
|
78
|
+
|
79
|
+
def mark_done(self, _id: int)-> bool:
|
80
|
+
# mark done for _id
|
81
|
+
if self.id_state[_id] is True:
|
82
|
+
return False
|
83
|
+
self.id_state[_id] = True
|
84
|
+
return True
|
85
|
+
```
|
86
|
+
Check in `vt.utils.commons.commons.state` and documentation for more functions and utilities related to tracking state.
|
87
|
+
- Check if a value is `MISSING`
|
88
|
+
```python
|
89
|
+
from vt.utils.commons.commons.core_py import MISSING, Missing, is_missing
|
90
|
+
|
91
|
+
def some_operation(arg: Missing = MISSING):
|
92
|
+
"""
|
93
|
+
``MISSING`` can be used as a default value sentinel when ``None`` is a valid value for arg.
|
94
|
+
"""
|
95
|
+
arg = 10 if is_missing(arg) else arg
|
96
|
+
...
|
97
|
+
```
|
98
|
+
Check in `vt.utils.commons.commons.core_py` and documentation for more functions and utilities related to function management.
|
99
|
+
- Query and operate on iterables
|
100
|
+
```python
|
101
|
+
from vt.utils.commons.commons.collections import get_first_true
|
102
|
+
|
103
|
+
assert 3 == get_first_true([1, 3, 5, 7, 2, 1], 8, lambda x: x>2)
|
104
|
+
```
|
105
|
+
Check in `vt.utils.commons.commons.collections` and documentation for more functions and utilities related to collection management.
|
106
|
+
|
107
|
+
|
108
|
+
### Contribute
|
109
|
+
|
110
|
+
Want to contribute?
|
111
|
+
|
112
|
+
Checkout [Guidelines for contributions](CONTRIBUTING.md).
|
@@ -0,0 +1,22 @@
|
|
1
|
+
vt/utils/commons/commons/__init__.py,sha256=xHXyqVRavmIrOdLyWUtprNyQSp4QO_kK-_rqS0rg8Nk,126
|
2
|
+
vt/utils/commons/commons/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
3
|
+
vt/utils/commons/commons/collections/__init__.py,sha256=eg1ur18wdljA6lfttw48z2qJNpKKGWXuqgH_xRMC8Gw,324
|
4
|
+
vt/utils/commons/commons/collections/utils.py,sha256=x3uwXOk2MdaASYqbyMpZAGqahR6NV1tZGQtC81bjziw,4152
|
5
|
+
vt/utils/commons/commons/core_py/__init__.py,sha256=fCs6ZGwRLH_y2d6Mzvyqd5IA013zY_SLt68AxPEKrTg,1717
|
6
|
+
vt/utils/commons/commons/core_py/base.py,sha256=yNwgTgbVqoF8Obx8jR0BdvZE57drXGmsBCed057udRA,799
|
7
|
+
vt/utils/commons/commons/core_py/utils.py,sha256=4ru1xvcHFyxNriL8ivELWkiKsDzVFNypMv7t1nB5t90,27512
|
8
|
+
vt/utils/commons/commons/op/__init__.py,sha256=UH7ADJcCFHsCfRKzyijmax1UlmcY3FaFTggtZZFX-i0,435
|
9
|
+
vt/utils/commons/commons/op/base.py,sha256=Cv5fYOamCG3S_VbpUoTQ-0lZmo9hypy2LOd4LU3QZyQ,3509
|
10
|
+
vt/utils/commons/commons/os/__init__.py,sha256=dxmmjYwJxKQ2TbTihWM46kPm7dMCPvVA90tBqQhRWbA,703
|
11
|
+
vt/utils/commons/commons/os/_base_utils.py,sha256=HOrGP7Yp7YjgAOvZNpe02KUqH7s_v8f7V_tA_Vm-MyI,356
|
12
|
+
vt/utils/commons/commons/os/linux.py,sha256=JRlr2Uy2WMoVIlmiB3oq6sVjFrhOwy8kiv7hcAKfiOc,651
|
13
|
+
vt/utils/commons/commons/os/mac.py,sha256=xPDWyWHdgID8SSUUs4Zh997ihNsR-KRvSG5lM6n2XoY,634
|
14
|
+
vt/utils/commons/commons/os/posix.py,sha256=V8kij7pGDXj6uJWWf4gUhjzYlBMDMKvWqcVJALNx3ec,657
|
15
|
+
vt/utils/commons/commons/os/windows.py,sha256=fJeVn0cdNplzPOiJbeGY8yyriug28Jei1WGKaLBFJFw,671
|
16
|
+
vt/utils/commons/commons/state/__init__.py,sha256=sKFo9_VSnv_VyFlnzUnET7_QkKnqXYrj9cEU1u3w83I,494
|
17
|
+
vt/utils/commons/commons/state/done.py,sha256=fGs0yNWgwRwm44gzmR6FNX7NKq7-39rEnsri9ztyZ4o,5146
|
18
|
+
vt_commons-0.0.1.dist-info/licenses/LICENSE,sha256=pOzr5bMWS6mHi3vro8d5vw0qW1i14rVq2XFrDuystVY,11372
|
19
|
+
vt_commons-0.0.1.dist-info/METADATA,sha256=M-_m3IVJCzejea3o2OAm0uOmWMivNE8JXBF1nFIuyHQ,4718
|
20
|
+
vt_commons-0.0.1.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
21
|
+
vt_commons-0.0.1.dist-info/top_level.txt,sha256=aN5KWgJq84W0MDifCX3yRJAp04FH7pd5PDh7qa2P8sM,3
|
22
|
+
vt_commons-0.0.1.dist-info/RECORD,,
|