Skip to content

Commit 6d59091

Browse files
authored
Update test_LazySequence.py (#3)
* Update test_LazySequence.py add last item test * bugfix * add docstrings * update version test
1 parent 6273b61 commit 6d59091

7 files changed

+221
-144
lines changed

poetry.lock

+158-134
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "ZnSlice"
3-
version = "0.1.1"
3+
version = "0.1.2"
44
description = "Cache, advanced slicing and lazy loading for __getitem__"
55
license = "Apache-2.0"
66
authors = ["zincwarecode <[email protected]>"]
@@ -40,3 +40,16 @@ exclude_lines = [
4040
"if __name__ == .__main__.:"
4141
]
4242
ignore_errors = true
43+
44+
[tool.ruff]
45+
select = ["E", "F", "D", "N", "C"]
46+
extend-ignore = [
47+
"C901",
48+
"D213", "D203",
49+
"D401",
50+
"N802", "N801"
51+
]
52+
53+
exclude = [
54+
"tests",
55+
]

tests/test_LazySequence.py

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ def test_LazySequenceInit():
1111
assert lst[0] == 1
1212
assert lst[[0, 2]].tolist() == [1, 3]
1313
assert len(lst) == 3
14+
assert lst[-1] == 3
1415

1516

1617
def test_LazySequence_empty_Init():

tests/test_znslice.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
def test_version():
99
"""Test 'ZnTrack' version."""
10-
assert znslice.__version__ == "0.1.1"
10+
assert znslice.__version__ == "0.1.2"
1111

1212

1313
class CacheList(collections.abc.Sequence):

znslice/__init__.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
"""The znslice package."""
12
import importlib.metadata
23

4+
from znslice import utils
35
from znslice.znslice import LazySequence, Reset, znslice
46

5-
__all__ = ["znslice", "LazySequence", "Reset"]
7+
__all__ = ["znslice", "LazySequence", "Reset", "utils"]
68
__version__ = importlib.metadata.version("znslice")

znslice/utils.py

+18-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
1+
"""ZnSlice utils module."""
12
import functools
23

34

45
def optional_kwargs_decorator(fn):
6+
"""Decorator to allow optional kwargs."""
7+
58
@functools.wraps(fn)
69
def wrapped_decorator(*args, **kwargs):
10+
"""The wrapped decorator."""
711
if len(args) == 1 and callable(args[0]):
812
return fn(args[0])
913

1014
@functools.wraps(fn)
1115
def real_decorator(decoratee):
16+
"""The real decorator."""
1217
return fn(decoratee, *args, **kwargs)
1318

1419
return real_decorator
@@ -18,30 +23,39 @@ def real_decorator(decoratee):
1823

1924
@functools.singledispatch
2025
def item_to_indices(item, self):
26+
"""Convert item to indices."""
2127
raise ValueError(f"Cannot handle item of type {type(item)}")
2228

2329

2430
@item_to_indices.register
25-
def _(item: int, self):
31+
def _(item: int, self) -> int:
32+
"""Keep int as is."""
2633
return item
2734

2835

2936
@item_to_indices.register
30-
def _(item: list, self):
37+
def _(item: list, self) -> list:
38+
"""Keep list as is."""
3139
return item
3240

3341

3442
@item_to_indices.register
35-
def _(item: tuple, self):
43+
def _(item: tuple, self) -> list:
44+
"""Convert tuple to list."""
3645
return list(item)
3746

3847

3948
@item_to_indices.register
40-
def _(item: slice, self):
49+
def _(item: slice, self) -> list:
50+
"""Convert slice to list using the length of the item."""
4151
return list(range(len(self)))[item]
4252

4353

4454
def handle_item(indices, cache, func, self, advanced_slicing=False) -> list:
55+
"""Handle item.
56+
57+
TODO ...
58+
"""
4559
if advanced_slicing:
4660
if new_indices := [x for x in indices if x not in cache]:
4761
# only if len(new_indices) > 0

znslice/znslice.py

+26-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
"""The main znslice module."""
12
import collections.abc
23
import functools
34
import logging
@@ -13,6 +14,7 @@ class Reset:
1314
"""Reset the cache for the given items."""
1415

1516
def __init__(self, item):
17+
"""Initialize the Reset object."""
1618
self.item = item
1719

1820

@@ -25,6 +27,10 @@ def __init__(
2527
indices: typing.List[typing.Union[list, int]],
2628
lazy_single_item: bool = False,
2729
):
30+
"""Initialize the LazySequence.
31+
32+
Todo: ...
33+
"""
2834
self._obj = obj
2935
self._indices = indices
3036
self._lazy_single_item = lazy_single_item
@@ -40,10 +46,14 @@ def from_obj(cls, obj, /, indices: list = None, lazy_single_item=False):
4046
4147
Parameters
4248
----------
49+
cls: LazySequence
50+
the LazySequence class.
4351
obj: list
4452
list to create a LazySequence from.
4553
indices: list, optional
4654
list of indices to gather from obj. If None, all items in obj are used.
55+
lazy_single_item: bool, optional
56+
currently unused.
4757
4858
Returns
4959
-------
@@ -61,6 +71,14 @@ def _(cls, obj, /, indices: list = None, lazy_single_item=False):
6171
return cls([obj], [indices], lazy_single_item=False)
6272

6373
def __getitem__(self, item):
74+
"""Get item from LazySequence.
75+
76+
Todo ...
77+
"""
78+
if item == -1:
79+
# special case, return last entry
80+
return self[len(self) - 1]
81+
6482
indices = utils.item_to_indices(item, self)
6583
single_item = False
6684
if isinstance(indices, int):
@@ -90,13 +108,16 @@ def __getitem__(self, item):
90108
else lazy_sequence
91109
)
92110

93-
def __repr__(self):
111+
def __repr__(self) -> str:
112+
"""Return the representation of the LazySequence."""
94113
return f"{type(self).__name__}({self._obj}, {self._indices})"
95114

96-
def __len__(self):
115+
def __len__(self) -> int:
116+
"""Return the length of the LazySequence."""
97117
return sum(len(x) for x in self._indices)
98118

99119
def __add__(self, other):
120+
"""Add two LazySequences."""
100121
if isinstance(other, LazySequence):
101122
return self._get_new_instance(
102123
self._obj + other._obj,
@@ -109,7 +130,8 @@ def __add__(self, other):
109130
else:
110131
raise TypeError("Can only add LazySequence to {LazySequence, list}")
111132

112-
def tolist(self):
133+
def tolist(self) -> list:
134+
"""Return the LazySequence as a non-lazy list."""
113135
data = []
114136
for obj, indices in zip(self._obj, self._indices):
115137
if isinstance(indices, int):
@@ -159,6 +181,7 @@ def wrapper(self, item, _resolve: bool = False):
159181
Parameters
160182
----------
161183
self: object
184+
the class instance.
162185
item: int, slice, list, tuple, Reset
163186
The item to get.
164187
_resolve: bool, default=False

0 commit comments

Comments
 (0)