jetpytools 1.4.0__py3-none-any.whl → 1.6.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.

Potentially problematic release.


This version of jetpytools might be problematic. Click here for more details.

jetpytools/types/file.py CHANGED
@@ -2,30 +2,30 @@ from __future__ import annotations
2
2
 
3
3
  import fnmatch
4
4
  import shutil
5
-
6
- from os import PathLike, listdir, path, walk
5
+ from os import X_OK, PathLike, access, listdir, path, walk
7
6
  from pathlib import Path
8
7
  from sys import version_info
9
8
  from typing import TYPE_CHECKING, Any, Callable, Iterable, Literal, TypeAlias, Union
10
9
 
11
- __all__ = [
12
- 'FilePathType', 'FileDescriptor',
13
- 'FileOpener',
14
-
15
- 'OpenTextModeUpdating',
16
- 'OpenTextModeWriting',
17
- 'OpenTextModeReading',
18
-
19
- 'OpenBinaryModeUpdating',
20
- 'OpenBinaryModeWriting',
21
- 'OpenBinaryModeReading',
22
-
23
- 'OpenTextMode',
24
- 'OpenBinaryMode',
10
+ from typing_extensions import Self
25
11
 
26
- 'SPath', 'SPathLike'
12
+ __all__ = [
13
+ "FileDescriptor",
14
+ "FileOpener",
15
+ "FilePathType",
16
+ "OpenBinaryMode",
17
+ "OpenBinaryModeReading",
18
+ "OpenBinaryModeUpdating",
19
+ "OpenBinaryModeWriting",
20
+ "OpenTextMode",
21
+ "OpenTextModeReading",
22
+ "OpenTextModeUpdating",
23
+ "OpenTextModeWriting",
24
+ "SPath",
25
+ "SPathLike",
27
26
  ]
28
27
 
28
+
29
29
  FileDescriptor: TypeAlias = int
30
30
 
31
31
  FilePathType: TypeAlias = str | bytes | PathLike[str] | PathLike[bytes]
@@ -33,26 +33,70 @@ FilePathType: TypeAlias = str | bytes | PathLike[str] | PathLike[bytes]
33
33
  FileOpener: TypeAlias = Callable[[str, int], int]
34
34
 
35
35
  OpenTextModeUpdating: TypeAlias = Literal[
36
- 'r+', '+r', 'rt+', 'r+t', '+rt', 'tr+', 't+r', '+tr', 'w+', '+w', 'wt+', 'w+t', '+wt', 'tw+', 't+w', '+tw',
37
- 'a+', '+a', 'at+', 'a+t', '+at', 'ta+', 't+a', '+ta', 'x+', '+x', 'xt+', 'x+t', '+xt', 'tx+', 't+x', '+tx',
38
- ]
39
- OpenTextModeWriting: TypeAlias = Literal[
40
- 'w', 'wt', 'tw', 'a', 'at', 'ta', 'x', 'xt', 'tx'
41
- ]
42
- OpenTextModeReading: TypeAlias = Literal[
43
- 'r', 'rt', 'tr', 'U', 'rU', 'Ur', 'rtU', 'rUt', 'Urt', 'trU', 'tUr', 'Utr'
36
+ "r+",
37
+ "+r",
38
+ "rt+",
39
+ "r+t",
40
+ "+rt",
41
+ "tr+",
42
+ "t+r",
43
+ "+tr",
44
+ "w+",
45
+ "+w",
46
+ "wt+",
47
+ "w+t",
48
+ "+wt",
49
+ "tw+",
50
+ "t+w",
51
+ "+tw",
52
+ "a+",
53
+ "+a",
54
+ "at+",
55
+ "a+t",
56
+ "+at",
57
+ "ta+",
58
+ "t+a",
59
+ "+ta",
60
+ "x+",
61
+ "+x",
62
+ "xt+",
63
+ "x+t",
64
+ "+xt",
65
+ "tx+",
66
+ "t+x",
67
+ "+tx",
44
68
  ]
69
+ OpenTextModeWriting: TypeAlias = Literal["w", "wt", "tw", "a", "at", "ta", "x", "xt", "tx"]
70
+ OpenTextModeReading: TypeAlias = Literal["r", "rt", "tr", "U", "rU", "Ur", "rtU", "rUt", "Urt", "trU", "tUr", "Utr"]
45
71
 
46
72
  OpenBinaryModeUpdating: TypeAlias = Literal[
47
- 'rb+', 'r+b', '+rb', 'br+', 'b+r', '+br', 'wb+', 'w+b', '+wb', 'bw+', 'b+w', '+bw',
48
- 'ab+', 'a+b', '+ab', 'ba+', 'b+a', '+ba', 'xb+', 'x+b', '+xb', 'bx+', 'b+x', '+bx'
49
- ]
50
- OpenBinaryModeWriting: TypeAlias = Literal[
51
- 'wb', 'bw', 'ab', 'ba', 'xb', 'bx'
52
- ]
53
- OpenBinaryModeReading: TypeAlias = Literal[
54
- 'rb', 'br', 'rbU', 'rUb', 'Urb', 'brU', 'bUr', 'Ubr'
73
+ "rb+",
74
+ "r+b",
75
+ "+rb",
76
+ "br+",
77
+ "b+r",
78
+ "+br",
79
+ "wb+",
80
+ "w+b",
81
+ "+wb",
82
+ "bw+",
83
+ "b+w",
84
+ "+bw",
85
+ "ab+",
86
+ "a+b",
87
+ "+ab",
88
+ "ba+",
89
+ "b+a",
90
+ "+ba",
91
+ "xb+",
92
+ "x+b",
93
+ "+xb",
94
+ "bx+",
95
+ "b+x",
96
+ "+bx",
55
97
  ]
98
+ OpenBinaryModeWriting: TypeAlias = Literal["wb", "bw", "ab", "ba", "xb", "bx"]
99
+ OpenBinaryModeReading: TypeAlias = Literal["rb", "br", "rbU", "rUb", "Urb", "brU", "bUr", "Ubr"]
56
100
 
57
101
  OpenTextMode: TypeAlias = OpenTextModeUpdating | OpenTextModeWriting | OpenTextModeReading
58
102
  OpenBinaryMode: TypeAlias = OpenBinaryModeUpdating | OpenBinaryModeReading | OpenBinaryModeWriting
@@ -60,12 +104,13 @@ OpenBinaryMode: TypeAlias = OpenBinaryModeUpdating | OpenBinaryModeReading | Ope
60
104
 
61
105
  class SPath(Path):
62
106
  """Modified version of pathlib.Path"""
107
+
63
108
  if version_info < (3, 12):
64
109
  _flavour = type(Path())._flavour # type: ignore
65
110
 
66
111
  if TYPE_CHECKING:
67
- def __new__(cls, *args: SPathLike, **kwargs: Any) -> SPath:
68
- ...
112
+
113
+ def __new__(cls, *args: SPathLike, **kwargs: Any) -> Self: ...
69
114
 
70
115
  def format(self, *args: Any, **kwargs: Any) -> SPath:
71
116
  """Format the path with the given arguments."""
@@ -101,22 +146,19 @@ class SPath(Path):
101
146
  if not missing_ok:
102
147
  raise
103
148
 
104
- def read_lines(
105
- self, encoding: str | None = None, errors: str | None = None, keepends: bool = False
106
- ) -> list[str]:
149
+ def read_lines(self, encoding: str | None = None, errors: str | None = None, keepends: bool = False) -> list[str]:
107
150
  """Read the file and return its lines."""
108
151
 
109
152
  return super().read_text(encoding, errors).splitlines(keepends)
110
153
 
111
154
  def write_lines(
112
- self, data: Iterable[str], encoding: str | None = None,
113
- errors: str | None = None, newline: str | None = None
155
+ self, data: Iterable[str], encoding: str | None = None, errors: str | None = None, newline: str | None = None
114
156
  ) -> int:
115
157
  """Open the file and write the given lines."""
116
158
 
117
- return super().write_text('\n'.join(data), encoding, errors, newline)
159
+ return super().write_text("\n".join(data), encoding, errors, newline)
118
160
 
119
- def append_to_stem(self, suffixes: str | Iterable[str], sep: str = '_') -> SPath:
161
+ def append_to_stem(self, suffixes: str | Iterable[str], sep: str = "_") -> SPath:
120
162
  """Append a suffix to the stem of the path"""
121
163
 
122
164
  from ..functions import to_arr
@@ -149,19 +191,20 @@ class SPath(Path):
149
191
 
150
192
  if not self.is_dir():
151
193
  from ..exceptions import PathIsNotADirectoryError
152
- raise PathIsNotADirectoryError('The given path, \"{self}\" is not a directory!', self.copy_dir)
194
+
195
+ raise PathIsNotADirectoryError('The given path, "{self}" is not a directory!', self.copy_dir)
153
196
 
154
197
  dst.mkdirp()
155
198
  shutil.copytree(self, dst, dirs_exist_ok=True)
156
199
 
157
200
  return SPath(dst)
158
201
 
159
- def lglob(self, pattern: str = '*') -> list[SPath]:
202
+ def lglob(self, pattern: str = "*") -> list[SPath]:
160
203
  """Glob the path and return the list of paths."""
161
204
 
162
205
  return list(map(SPath, self.glob(pattern)))
163
206
 
164
- def fglob(self, pattern: str = '*') -> SPath | None:
207
+ def fglob(self, pattern: str = "*") -> SPath | None:
165
208
  """Glob the path and return the first match."""
166
209
 
167
210
  for root, dirs, files in walk(self):
@@ -171,7 +214,7 @@ class SPath(Path):
171
214
 
172
215
  return None
173
216
 
174
- def find_newest_file(self, pattern: str = '*') -> SPath | None:
217
+ def find_newest_file(self, pattern: str = "*") -> SPath | None:
175
218
  """Find the most recently modified file matching the given pattern in the directory."""
176
219
 
177
220
  matching_files = self.get_folder().glob(pattern)
@@ -188,12 +231,18 @@ class SPath(Path):
188
231
 
189
232
  if not self.exists():
190
233
  from ..exceptions import FileNotExistsError
191
- raise FileNotExistsError('The given path, \"{self}\" is not a file or directory!', self.get_size)
234
+
235
+ raise FileNotExistsError('The given path, "{self}" is not a file or directory!', self.get_size)
192
236
 
193
237
  if self.is_file():
194
238
  return self.stat().st_size
195
239
 
196
- return sum(f.stat().st_size for f in self.rglob('*') if f.is_file())
240
+ return sum(f.stat().st_size for f in self.rglob("*") if f.is_file())
241
+
242
+ def is_executable(self) -> bool:
243
+ """Check if the path is executable."""
244
+
245
+ return access(self.to_str(), X_OK)
197
246
 
198
247
 
199
248
  SPathLike = Union[str, PathLike[str], Path, SPath]
jetpytools/types/funcs.py CHANGED
@@ -1,34 +1,22 @@
1
1
  from __future__ import annotations
2
2
 
3
3
  from functools import wraps
4
- from typing import TYPE_CHECKING, Any, Callable, Iterable, Iterator, SupportsIndex, TypeAlias, overload
4
+ from typing import TYPE_CHECKING, Any, Callable, Iterable, Iterator, SupportsIndex, TypeAlias
5
5
 
6
6
  from typing_extensions import Self, TypeIs
7
7
 
8
8
  from .builtins import P, T
9
9
  from .supports import SupportsString
10
10
 
11
- __all__ = [
12
- 'StrList',
13
- 'Sentinel',
14
- 'SentinelT'
15
- ]
11
+ __all__ = ["Sentinel", "SentinelT", "StrList"]
16
12
 
17
13
 
18
14
  class StrList(list[SupportsString]):
19
15
  """Custom class for representing a recursively "stringable" list."""
20
16
 
21
17
  if TYPE_CHECKING:
22
- @overload
23
- def __init__(self, __iterable: Iterable[SupportsString | None] = []) -> None:
24
- ...
25
18
 
26
- @overload
27
- def __init__(self, __iterable: Iterable[Iterable[SupportsString | None] | None] = []) -> None:
28
- ...
29
-
30
- def __init__(self, __iterable: Any = []) -> None:
31
- ...
19
+ def __init__(self, iterable: Iterable[SupportsString | None] | None = ..., /) -> None: ...
32
20
 
33
21
  @property
34
22
  def string(self) -> str:
@@ -40,21 +28,16 @@ class StrList(list[SupportsString]):
40
28
  def __str__(self) -> str:
41
29
  from ..functions import flatten
42
30
 
43
- return ' '.join(
44
- filter(
45
- None,
46
- (str(x).strip() for x in flatten(self) if x is not None)
47
- )
48
- )
31
+ return " ".join(filter(None, (str(x).strip() for x in flatten(self) if x is not None)))
49
32
 
50
- def __add__(self, __x: list[SupportsString]) -> StrList: # type: ignore[override]
51
- return StrList(super().__add__(__x))
33
+ def __add__(self, x: list[SupportsString]) -> StrList: # type: ignore[override]
34
+ return StrList(super().__add__(x))
52
35
 
53
- def __mul__(self, __n: SupportsIndex) -> StrList:
54
- return StrList(super().__mul__(__n))
36
+ def __mul__(self, n: SupportsIndex) -> StrList:
37
+ return StrList(super().__mul__(n))
55
38
 
56
- def __rmul__(self, __n: SupportsIndex) -> StrList:
57
- return StrList(super().__rmul__(__n))
39
+ def __rmul__(self, n: SupportsIndex) -> StrList:
40
+ return StrList(super().__rmul__(n))
58
41
 
59
42
  @property
60
43
  def mlength(self) -> int:
@@ -98,7 +81,7 @@ class SentinelDispatcher:
98
81
  _sentinels[name] = SentinelDispatcher()
99
82
  return _sentinels[name]
100
83
 
101
- def __setattr__(self, __name: str, __value: Any) -> None:
84
+ def __setattr__(self, name: str, value: Any) -> None:
102
85
  raise NameError
103
86
 
104
87
  def __call__(self) -> SentinelDispatcher:
@@ -6,17 +6,7 @@ from typing import Any, Callable, Literal, TypeAlias, Union
6
6
  from .builtins import F, SingleOrArr, SingleOrArrOpt
7
7
  from .supports import SupportsString
8
8
 
9
- __all__ = [
10
- 'MissingT', 'MISSING',
11
-
12
- 'FuncExceptT',
13
-
14
- 'DataType',
15
-
16
- 'StrArr', 'StrArrOpt',
17
-
18
- 'PassthroughC'
19
- ]
9
+ __all__ = ["MISSING", "DataType", "FuncExceptT", "MissingT", "PassthroughC", "StrArr", "StrArrOpt"]
20
10
 
21
11
 
22
12
  class MissingTBase(Enum):
@@ -2,126 +2,142 @@ from __future__ import annotations
2
2
 
3
3
  from abc import abstractmethod
4
4
  from typing import (
5
- Any, Callable, Iterable, Protocol, SupportsFloat, SupportsIndex, TypeAlias, TypeVar, overload, runtime_checkable
5
+ Any,
6
+ Callable,
7
+ Iterable,
8
+ Protocol,
9
+ SupportsFloat,
10
+ SupportsIndex,
11
+ TypeAlias,
12
+ TypeVar,
13
+ overload,
14
+ runtime_checkable,
6
15
  )
7
16
 
8
- from .builtins import T0, T1, T2, T_contra
17
+ from .builtins import T0, T1, T2, T_co, T_contra
9
18
 
10
19
  __all__ = [
11
- 'SupportsTrunc',
20
+ "ComparatorFunc",
21
+ "SupportsAdd",
22
+ "SupportsAllComparisons",
23
+ "SupportsDunderGE",
24
+ "SupportsDunderGT",
25
+ "SupportsDunderLE",
26
+ "SupportsDunderLT",
27
+ "SupportsFloatOrIndex",
28
+ "SupportsIndexing",
29
+ "SupportsKeysAndGetItem",
30
+ "SupportsRAdd",
31
+ "SupportsRichComparison",
32
+ "SupportsRichComparisonT",
33
+ "SupportsString",
34
+ "SupportsSumNoDefaultT",
35
+ "SupportsTrunc",
36
+ ]
37
+
12
38
 
13
- 'SupportsString',
39
+ _KT = TypeVar("_KT")
40
+ _VT_co = TypeVar("_VT_co", covariant=True)
14
41
 
15
- 'SupportsDunderLT', 'SupportsDunderGT',
16
- 'SupportsDunderLE', 'SupportsDunderGE',
17
42
 
18
- 'SupportsFloatOrIndex',
43
+ @runtime_checkable
44
+ class SupportsAdd(Protocol[T_contra, T_co]):
45
+ def __add__(self, x: T_contra, /) -> T_co: ...
19
46
 
20
- 'SupportsIndexing',
21
- 'SupportsKeysAndGetItem',
22
47
 
23
- 'SupportsAllComparisons',
24
- 'SupportsRichComparison', 'SupportsRichComparisonT',
25
- 'ComparatorFunc'
26
- ]
48
+ @runtime_checkable
49
+ class SupportsRAdd(Protocol[T_contra, T_co]):
50
+ def __radd__(self, x: T_contra, /) -> T_co: ...
51
+
27
52
 
53
+ class _SupportsSumWithNoDefaultGiven(SupportsAdd[Any, Any], SupportsRAdd[int, Any], Protocol): ...
28
54
 
29
- _KT = TypeVar('_KT')
30
- _VT_co = TypeVar('_VT_co', covariant=True)
55
+
56
+ SupportsSumNoDefaultT = TypeVar("SupportsSumNoDefaultT", bound=_SupportsSumWithNoDefaultGiven)
31
57
 
32
58
 
33
59
  @runtime_checkable
34
60
  class SupportsTrunc(Protocol):
35
- def __trunc__(self) -> int:
36
- ...
61
+ def __trunc__(self) -> int: ...
37
62
 
38
63
 
39
64
  @runtime_checkable
40
65
  class SupportsString(Protocol):
41
66
  @abstractmethod
42
- def __str__(self) -> str:
43
- ...
67
+ def __str__(self) -> str: ...
44
68
 
45
69
 
46
70
  @runtime_checkable
47
71
  class SupportsDunderLT(Protocol[T_contra]):
48
- def __lt__(self, __other: T_contra) -> bool:
49
- ...
72
+ def __lt__(self, other: T_contra) -> bool: ...
50
73
 
51
74
 
52
75
  @runtime_checkable
53
76
  class SupportsDunderGT(Protocol[T_contra]):
54
- def __gt__(self, __other: T_contra) -> bool:
55
- ...
77
+ def __gt__(self, other: T_contra) -> bool: ...
56
78
 
57
79
 
58
80
  @runtime_checkable
59
81
  class SupportsDunderLE(Protocol[T_contra]):
60
- def __le__(self, __other: T_contra) -> bool:
61
- ...
82
+ def __le__(self, other: T_contra) -> bool: ...
62
83
 
63
84
 
64
85
  @runtime_checkable
65
86
  class SupportsDunderGE(Protocol[T_contra]):
66
- def __ge__(self, __other: T_contra) -> bool:
67
- ...
87
+ def __ge__(self, other: T_contra) -> bool: ...
68
88
 
69
89
 
70
90
  @runtime_checkable
71
91
  class SupportsAllComparisons(
72
92
  SupportsDunderLT[Any], SupportsDunderGT[Any], SupportsDunderLE[Any], SupportsDunderGE[Any], Protocol
73
- ):
74
- ...
93
+ ): ...
75
94
 
76
95
 
77
96
  SupportsRichComparison: TypeAlias = SupportsDunderLT[Any] | SupportsDunderGT[Any]
78
- SupportsRichComparisonT = TypeVar('SupportsRichComparisonT', bound=SupportsRichComparison)
97
+ SupportsRichComparisonT = TypeVar("SupportsRichComparisonT", bound=SupportsRichComparison)
79
98
 
80
99
 
81
100
  class ComparatorFunc(Protocol):
82
101
  @overload
83
102
  def __call__(
84
- self, __arg1: SupportsRichComparisonT, __arg2: SupportsRichComparisonT,
85
- *_args: SupportsRichComparisonT, key: None = ...
86
- ) -> SupportsRichComparisonT:
87
- ...
103
+ self,
104
+ arg1: SupportsRichComparisonT,
105
+ arg2: SupportsRichComparisonT,
106
+ /,
107
+ *args: SupportsRichComparisonT,
108
+ key: None = ...,
109
+ ) -> SupportsRichComparisonT: ...
88
110
 
89
111
  @overload
90
- def __call__(self, __arg1: T0, __arg2: T0, *_args: T0, key: Callable[[T0], SupportsRichComparison]) -> T0:
91
- ...
112
+ def __call__(self, arg1: T0, arg2: T0, /, *_args: T0, key: Callable[[T0], SupportsRichComparison]) -> T0: ...
92
113
 
93
114
  @overload
94
- def __call__(self, __iterable: Iterable[SupportsRichComparisonT], *, key: None = ...) -> SupportsRichComparisonT:
95
- ...
115
+ def __call__(
116
+ self, iterable: Iterable[SupportsRichComparisonT], /, *, key: None = ...
117
+ ) -> SupportsRichComparisonT: ...
96
118
 
97
119
  @overload
98
- def __call__(self, __iterable: Iterable[T0], *, key: Callable[[T0], SupportsRichComparison]) -> T0:
99
- ...
120
+ def __call__(self, iterable: Iterable[T0], /, *, key: Callable[[T0], SupportsRichComparison]) -> T0: ...
100
121
 
101
122
  @overload
102
123
  def __call__(
103
- self, __iterable: Iterable[SupportsRichComparisonT], *, key: None = ..., default: T0
104
- ) -> SupportsRichComparisonT | T0:
105
- ...
124
+ self, iterable: Iterable[SupportsRichComparisonT], /, *, key: None = ..., default: T0
125
+ ) -> SupportsRichComparisonT | T0: ...
106
126
 
107
127
  @overload
108
128
  def __call__(
109
- self, __iterable: Iterable[T1], *, key: Callable[[T1], SupportsRichComparison], default: T2
110
- ) -> T1 | T2:
111
- ...
129
+ self, iterable: Iterable[T1], /, *, key: Callable[[T1], SupportsRichComparison], default: T2
130
+ ) -> T1 | T2: ...
112
131
 
113
132
 
114
133
  class SupportsIndexing(Protocol[_VT_co]):
115
- def __getitem__(self, __k: int) -> _VT_co:
116
- ...
134
+ def __getitem__(self, k: int) -> _VT_co: ...
117
135
 
118
136
 
119
137
  class SupportsKeysAndGetItem(Protocol[_KT, _VT_co]):
120
- def keys(self) -> Iterable[_KT]:
121
- ...
138
+ def keys(self) -> Iterable[_KT]: ...
122
139
 
123
- def __getitem__(self, __k: _KT) -> _VT_co:
124
- ...
140
+ def __getitem__(self, k: _KT) -> _VT_co: ...
125
141
 
126
142
 
127
143
  SupportsFloatOrIndex: TypeAlias = SupportsFloat | SupportsIndex