kaparoo-python 0.1.12__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.
@@ -1,45 +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
- from kaparoo.utils.types import T_co
10
-
11
- if TYPE_CHECKING:
12
- from typing import Self
13
-
14
- from kaparoo.filesystem.types import StrPath
15
-
16
-
17
- class DataSequence(Sequence[T_co]):
18
- @abstractmethod
19
- def __init__(self: Self, path: StrPath) -> None:
20
- raise NotImplementedError
21
-
22
- @abstractmethod
23
- def __len__(self: Self) -> int:
24
- raise NotImplementedError
25
-
26
- @overload
27
- def __getitem__(self: Self, index: int, /) -> T_co:
28
- pass
29
-
30
- @overload
31
- def __getitem__(self: Self, index: slice, /) -> Sequence[T_co]:
32
- pass
33
-
34
- def __getitem__(self: Self, index: int | slice, /) -> T_co | Sequence[T_co]:
35
- if isinstance(index, slice):
36
- start, stop, step = index.indices(len(self))
37
- return self.by_indices(range(start, stop, step))
38
- return self.by_index(index)
39
-
40
- @abstractmethod
41
- def by_index(self: Self, index: int) -> T_co:
42
- raise NotImplementedError
43
-
44
- def by_indices(self: Self, indices: Sequence[int]) -> Sequence[T_co]:
45
- 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,49 +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 Generator, Sequence
11
- from typing import Any
12
-
13
- from kaparoo.utils.types import T_co
14
-
15
-
16
- def generate_batches(
17
- sequence: Sequence[T_co],
18
- size: int,
19
- step: int = 1,
20
- skip: int = 1,
21
- start: int = 0,
22
- stop: int | None = None,
23
- *,
24
- drop_last: bool = True,
25
- ) -> Generator[Sequence[T_co], Any, None]:
26
- def die_if_not_positive(name: str, value: int) -> None:
27
- if value <= 0:
28
- raise ValueError(f"{name} must be positive (got {value})")
29
-
30
- die_if_not_positive("size", size)
31
- die_if_not_positive("step", step)
32
- die_if_not_positive("skip", skip)
33
-
34
- stop = replace_if_none(stop, len_ := len(sequence))
35
- if not (start < stop <= len_ and start >= 0):
36
- raise ValueError(
37
- f"invalid range [{start}, {stop}) for sequence of length {len_}"
38
- )
39
-
40
- head = start
41
- tail = head + (size - 1) * skip + 1
42
-
43
- while tail <= stop:
44
- yield sequence[head:tail:skip]
45
- head += step
46
- tail += step
47
-
48
- if not drop_last and head < stop:
49
- 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,56 +1,73 @@
1
- __all__ = (
2
- # utils
3
- "prepend_path",
4
- "prepend_paths",
5
- "stringify_path",
6
- "stringify_paths",
7
- # existence
8
- "ensure_dir_exists",
9
- "ensure_dirs_exist",
10
- "ensure_path_exists",
11
- "ensure_paths_exist",
12
- "ensure_file_exists",
13
- "ensure_files_exist",
14
- "dir_exists",
15
- "dirs_exist",
16
- "file_exists",
17
- "files_exist",
18
- "path_exists",
19
- "paths_exist",
20
- # directory
21
- "dir_empty",
22
- "dirs_empty",
23
- "get_dirs",
24
- "get_files",
25
- "get_paths",
26
- "make_dirs",
27
- )
28
-
29
- from kaparoo.filesystem.directory import (
30
- dir_empty,
31
- dirs_empty,
32
- get_dirs,
33
- get_files,
34
- get_paths,
35
- make_dirs,
36
- )
37
- from kaparoo.filesystem.existence import (
38
- dir_exists,
39
- dirs_exist,
40
- ensure_dir_exists,
41
- ensure_dirs_exist,
42
- ensure_file_exists,
43
- ensure_files_exist,
44
- ensure_path_exists,
45
- ensure_paths_exist,
46
- file_exists,
47
- files_exist,
48
- path_exists,
49
- paths_exist,
50
- )
51
- from kaparoo.filesystem.utils import (
52
- prepend_path,
53
- prepend_paths,
54
- stringify_path,
55
- stringify_paths,
56
- )
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
+ )