kaparoo-python 0.2.0__py3-none-any.whl → 0.2.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.
kaparoo/data/sequence.py CHANGED
@@ -1,39 +1,39 @@
1
- from __future__ import annotations
2
-
3
- __all__ = ("DataSequence",)
4
-
5
- from abc import abstractmethod
6
- from collections.abc import Sequence
7
- from typing import TYPE_CHECKING, overload
8
-
9
- if TYPE_CHECKING:
10
- from kaparoo.filesystem.types import StrPath
11
-
12
-
13
- class DataSequence[T](Sequence[T]):
14
- @abstractmethod
15
- def __init__(self, path: StrPath) -> None:
16
- raise NotImplementedError
17
-
18
- @abstractmethod
19
- def __len__(self) -> int:
20
- raise NotImplementedError
21
-
22
- @overload
23
- def __getitem__(self, index: int, /) -> T: ...
24
-
25
- @overload
26
- def __getitem__(self, index: slice, /) -> Sequence[T]: ...
27
-
28
- def __getitem__(self, index: int | slice, /) -> T | Sequence[T]:
29
- if isinstance(index, slice):
30
- start, stop, step = index.indices(len(self))
31
- return self.by_indices(range(start, stop, step))
32
- return self.by_index(index)
33
-
34
- @abstractmethod
35
- def by_index(self, index: int) -> T:
36
- raise NotImplementedError
37
-
38
- def by_indices(self, indices: Sequence[int]) -> Sequence[T]:
39
- return [self.by_index(index) for index in indices]
1
+ from __future__ import annotations
2
+
3
+ __all__ = ("DataSequence",)
4
+
5
+ from abc import abstractmethod
6
+ from collections.abc import Sequence
7
+ from typing import TYPE_CHECKING, overload
8
+
9
+ if TYPE_CHECKING:
10
+ from kaparoo.filesystem.types import StrPath
11
+
12
+
13
+ class DataSequence[T](Sequence[T]):
14
+ @abstractmethod
15
+ def __init__(self, path: StrPath) -> None:
16
+ raise NotImplementedError
17
+
18
+ @abstractmethod
19
+ def __len__(self) -> int:
20
+ raise NotImplementedError
21
+
22
+ @overload
23
+ def __getitem__(self, index: int, /) -> T: ...
24
+
25
+ @overload
26
+ def __getitem__(self, index: slice, /) -> Sequence[T]: ...
27
+
28
+ def __getitem__(self, index: int | slice, /) -> T | Sequence[T]:
29
+ if isinstance(index, slice):
30
+ start, stop, step = index.indices(len(self))
31
+ return self.by_indices(range(start, stop, step))
32
+ return self.by_index(index)
33
+
34
+ @abstractmethod
35
+ def by_index(self, index: int) -> T:
36
+ raise NotImplementedError
37
+
38
+ def by_indices(self, indices: Sequence[int]) -> Sequence[T]:
39
+ return [self.by_index(index) for index in indices]
kaparoo/data/utils.py CHANGED
@@ -1,46 +1,46 @@
1
- from __future__ import annotations
2
-
3
- __all__ = ("generate_batches",)
4
-
5
- from typing import TYPE_CHECKING
6
-
7
- from kaparoo.utils.optional import replace_if_none
8
-
9
- if TYPE_CHECKING:
10
- from collections.abc import Iterator, Sequence
11
-
12
-
13
- def generate_batches[T](
14
- sequence: Sequence[T],
15
- size: int,
16
- step: int = 1,
17
- skip: int = 1,
18
- start: int = 0,
19
- stop: int | None = None,
20
- *,
21
- drop_last: bool = True,
22
- ) -> Iterator[Sequence[T]]:
23
- def die_if_not_positive(name: str, value: int) -> None:
24
- if value <= 0:
25
- msg = f"{name} must be positive (got {value})"
26
- raise ValueError(msg)
27
-
28
- die_if_not_positive("size", size)
29
- die_if_not_positive("step", step)
30
- die_if_not_positive("skip", skip)
31
-
32
- stop = replace_if_none(stop, len_ := len(sequence))
33
- if not (start < stop <= len_ and start >= 0):
34
- msg = f"invalid range [{start}, {stop}) for sequence of length {len_}"
35
- raise ValueError(msg)
36
-
37
- head = start
38
- tail = head + (size - 1) * skip + 1
39
-
40
- while tail <= stop:
41
- yield sequence[head:tail:skip]
42
- head += step
43
- tail += step
44
-
45
- if not drop_last and head < stop:
46
- yield sequence[head:tail:skip]
1
+ from __future__ import annotations
2
+
3
+ __all__ = ("generate_batches",)
4
+
5
+ from typing import TYPE_CHECKING
6
+
7
+ from kaparoo.utils.optional import replace_if_none
8
+
9
+ if TYPE_CHECKING:
10
+ from collections.abc import Iterator, Sequence
11
+
12
+
13
+ def generate_batches[T](
14
+ sequence: Sequence[T],
15
+ size: int,
16
+ step: int = 1,
17
+ skip: int = 1,
18
+ start: int = 0,
19
+ stop: int | None = None,
20
+ *,
21
+ drop_last: bool = True,
22
+ ) -> Iterator[Sequence[T]]:
23
+ def die_if_not_positive(name: str, value: int) -> None:
24
+ if value <= 0:
25
+ msg = f"{name} must be positive (got {value})"
26
+ raise ValueError(msg)
27
+
28
+ die_if_not_positive("size", size)
29
+ die_if_not_positive("step", step)
30
+ die_if_not_positive("skip", skip)
31
+
32
+ stop = replace_if_none(stop, len_ := len(sequence))
33
+ if not (start < stop <= len_ and start >= 0):
34
+ msg = f"invalid range [{start}, {stop}) for sequence of length {len_}"
35
+ raise ValueError(msg)
36
+
37
+ head = start
38
+ tail = head + (size - 1) * skip + 1
39
+
40
+ while tail <= stop:
41
+ yield sequence[head:tail:skip]
42
+ head += step
43
+ tail += step
44
+
45
+ if not drop_last and head < stop:
46
+ yield sequence[head:tail:skip]
@@ -1,73 +1,73 @@
1
- __all__ = (
2
- "DirectoryNotFoundError",
3
- "NotAFileError",
4
- "dir_empty",
5
- "dir_empty_unsafe",
6
- "dir_exists",
7
- "dirs_empty",
8
- "dirs_empty_unsafe",
9
- "dirs_exist",
10
- "ensure_dir_exists",
11
- "ensure_dirs_exist",
12
- "ensure_file_exists",
13
- "ensure_files_exist",
14
- "ensure_path_exists",
15
- "ensure_paths_exist",
16
- "file_exists",
17
- "files_exist",
18
- "get_dirs",
19
- "get_files",
20
- "get_paths",
21
- "make_dir",
22
- "make_dirs",
23
- "path_exists",
24
- "paths_exist",
25
- "search_dirs",
26
- "search_files",
27
- "search_paths",
28
- "stringify_path",
29
- "stringify_paths",
30
- "wrap_path",
31
- "wrap_paths",
32
- )
33
-
34
- from kaparoo.filesystem.directory import (
35
- dir_empty,
36
- dir_empty_unsafe,
37
- dirs_empty,
38
- dirs_empty_unsafe,
39
- make_dir,
40
- make_dirs,
41
- )
42
- from kaparoo.filesystem.exceptions import (
43
- DirectoryNotFoundError,
44
- NotAFileError,
45
- )
46
- from kaparoo.filesystem.existence import (
47
- dir_exists,
48
- dirs_exist,
49
- ensure_dir_exists,
50
- ensure_dirs_exist,
51
- ensure_file_exists,
52
- ensure_files_exist,
53
- ensure_path_exists,
54
- ensure_paths_exist,
55
- file_exists,
56
- files_exist,
57
- path_exists,
58
- paths_exist,
59
- )
60
- from kaparoo.filesystem.search import (
61
- get_dirs,
62
- get_files,
63
- get_paths,
64
- search_dirs,
65
- search_files,
66
- search_paths,
67
- )
68
- from kaparoo.filesystem.utils import (
69
- stringify_path,
70
- stringify_paths,
71
- wrap_path,
72
- wrap_paths,
73
- )
1
+ __all__ = (
2
+ "DirectoryNotFoundError",
3
+ "NotAFileError",
4
+ "dir_empty",
5
+ "dir_empty_unsafe",
6
+ "dir_exists",
7
+ "dirs_empty",
8
+ "dirs_empty_unsafe",
9
+ "dirs_exist",
10
+ "ensure_dir_exists",
11
+ "ensure_dirs_exist",
12
+ "ensure_file_exists",
13
+ "ensure_files_exist",
14
+ "ensure_path_exists",
15
+ "ensure_paths_exist",
16
+ "file_exists",
17
+ "files_exist",
18
+ "get_dirs",
19
+ "get_files",
20
+ "get_paths",
21
+ "make_dir",
22
+ "make_dirs",
23
+ "path_exists",
24
+ "paths_exist",
25
+ "search_dirs",
26
+ "search_files",
27
+ "search_paths",
28
+ "stringify_path",
29
+ "stringify_paths",
30
+ "wrap_path",
31
+ "wrap_paths",
32
+ )
33
+
34
+ from kaparoo.filesystem.directory import (
35
+ dir_empty,
36
+ dir_empty_unsafe,
37
+ dirs_empty,
38
+ dirs_empty_unsafe,
39
+ make_dir,
40
+ make_dirs,
41
+ )
42
+ from kaparoo.filesystem.exceptions import (
43
+ DirectoryNotFoundError,
44
+ NotAFileError,
45
+ )
46
+ from kaparoo.filesystem.existence import (
47
+ dir_exists,
48
+ dirs_exist,
49
+ ensure_dir_exists,
50
+ ensure_dirs_exist,
51
+ ensure_file_exists,
52
+ ensure_files_exist,
53
+ ensure_path_exists,
54
+ ensure_paths_exist,
55
+ file_exists,
56
+ files_exist,
57
+ path_exists,
58
+ paths_exist,
59
+ )
60
+ from kaparoo.filesystem.search import (
61
+ get_dirs,
62
+ get_files,
63
+ get_paths,
64
+ search_dirs,
65
+ search_files,
66
+ search_paths,
67
+ )
68
+ from kaparoo.filesystem.utils import (
69
+ stringify_path,
70
+ stringify_paths,
71
+ wrap_path,
72
+ wrap_paths,
73
+ )