Skip to content

Commit 50c57e5

Browse files
authored
ZnSlice v0.1.1 (#2)
* add `LazySequence.from_obj` and update project * add CI * poetry update * update coverage config * test __repr__
1 parent 2b1ea6f commit 50c57e5

File tree

8 files changed

+372
-183
lines changed

8 files changed

+372
-183
lines changed

.github/workflows/pytest.yaml

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# This workflow will install Python dependencies, run tests and lint with a variety of Python versions
2+
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions
3+
4+
name: pytest
5+
6+
on:
7+
push:
8+
9+
jobs:
10+
test:
11+
runs-on: ubuntu-latest
12+
strategy:
13+
fail-fast: false
14+
15+
steps:
16+
- uses: actions/checkout@v2
17+
- name: Set up Python ${{ matrix.python-version }}
18+
uses: actions/setup-python@v2
19+
with:
20+
python-version: "3.10"
21+
- name: Install Poetry
22+
uses: abatilo/actions-poetry@v2
23+
with:
24+
poetry-version: 1.3.2
25+
- name: Install package
26+
run: |
27+
poetry install --no-interaction
28+
- name: Pytest
29+
run: |
30+
poetry run coverage run -m pytest
31+
poetry run coverage lcov
32+
- name: Coveralls
33+
uses: coverallsapp/github-action@master
34+
with:
35+
github-token: ${{ secrets.GITHUB_TOKEN }}
36+
path-to-lcov: coverage.lcov

README.md

+11
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
[![PyPI version](https://badge.fury.io/py/znslice.svg)](https://badge.fury.io/py/znslice)
2+
[![Coverage Status](https://coveralls.io/repos/github/zincware/ZnSlice/badge.svg?branch=main)](https://coveralls.io/github/zincware/ZnSlice?branch=main)
13
# ZnSlice
24

35
A lightweight library (without external dependencies) for:
@@ -14,6 +16,15 @@ pip install znslice
1416
# Usage
1517

1618
## Advanced Slicing and Cache
19+
Convert List to `znslice.LazySequence` to allow advanced slicing.
20+
```python
21+
import znslice
22+
23+
lst = znslice.LazySequence.from_obj([1, 2, 3], indices=[0, 2])
24+
print(lst[[0, 1]].tolist()) # [1, 3]
25+
```
26+
27+
1728
```python
1829
import znslice
1930
import collections.abc

poetry.lock

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

pyproject.toml

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "ZnSlice"
3-
version = "0.1.0"
3+
version = "0.1.1"
44
description = "Cache, advanced slicing and lazy loading for __getitem__"
55
license = "Apache-2.0"
66
authors = ["zincwarecode <[email protected]>"]
@@ -25,3 +25,18 @@ coverage = "^7.0.5"
2525
[build-system]
2626
requires = ["poetry-core"]
2727
build-backend = "poetry.core.masonry.api"
28+
29+
[tool.coverage.run]
30+
relative_files = true
31+
branch = true
32+
# omit the tests themselves
33+
omit = ["*/tests/*", "*/tmp/*", "*/interface/*"]
34+
35+
[tool.coverage.report]
36+
exclude_lines = [
37+
"raise AssertionError",
38+
"raise NotImplementedError",
39+
"if 0:",
40+
"if __name__ == .__main__.:"
41+
]
42+
ignore_errors = true

tests/test_LazySequence.py

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import pytest
2+
3+
import znslice
4+
5+
6+
def test_LazySequenceInit():
7+
lst = znslice.LazySequence.from_obj([1, 2, 3])
8+
assert lst._obj == [[1, 2, 3]]
9+
assert lst._indices == [[0, 1, 2]]
10+
assert lst[:].tolist() == [1, 2, 3]
11+
assert lst[0] == 1
12+
assert lst[[0, 2]].tolist() == [1, 3]
13+
assert len(lst) == 3
14+
15+
16+
def test_LazySequence_empty_Init():
17+
lst = znslice.LazySequence.from_obj([])
18+
assert len(lst) == 0
19+
lst += znslice.LazySequence.from_obj([1, 2, 3])
20+
assert len(lst) == 3
21+
assert lst[:].tolist() == [1, 2, 3]
22+
23+
24+
def test_LazySequence_indices():
25+
lst = znslice.LazySequence.from_obj([1, 2, 3], indices=[0, 2])
26+
assert len(lst) == 2
27+
assert lst[:].tolist() == [1, 3]
28+
assert lst[0] == 1
29+
assert lst[[0, 1]].tolist() == [1, 3]
30+
assert lst[1] == 3
31+
32+
lst += znslice.LazySequence.from_obj([4, 5, 6], indices=[1, 2])
33+
assert len(lst) == 4
34+
assert lst[:].tolist() == [1, 3, 5, 6]
35+
assert lst[0] == 1
36+
assert lst[[0, 2]].tolist() == [1, 5]
37+
38+
39+
def test_add_LazySequence_list():
40+
lst = znslice.LazySequence.from_obj([1, 2, 3], indices=[0, 2])
41+
lst += [4, 5, 6]
42+
43+
assert len(lst) == 5
44+
assert lst[:] == [1, 3, 4, 5, 6]
45+
assert isinstance(lst, list)
46+
47+
48+
def test_from_LazySequence():
49+
lst = znslice.LazySequence.from_obj(list(range(10)), indices=[0, 2, 4, 6, 8])
50+
lst2 = znslice.LazySequence.from_obj(lst, indices=[0, 2, 4])
51+
assert lst2[:].tolist() == [0, 4, 8]
52+
assert lst2[0] == 0
53+
assert lst[[0, 2]].tolist() == [0, 4]
54+
55+
56+
def test_from_LazySequence_err():
57+
with pytest.raises(TypeError):
58+
znslice.LazySequence.from_obj("Lorem Ipsum")
59+
60+
61+
def test_LazySequence_repr():
62+
lst = znslice.LazySequence.from_obj([1, 2, 3], indices=[0, 2])
63+
assert repr(lst) == "LazySequence([[1, 2, 3]], [[0, 2]])"

tests/test_znslice.py

+18-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
import znslice
66

77

8+
def test_version():
9+
"""Test 'ZnTrack' version."""
10+
assert znslice.__version__ == "0.1.1"
11+
12+
813
class CacheList(collections.abc.Sequence):
914
def __init__(self, data):
1015
self.data = data
@@ -266,7 +271,7 @@ def test_LazyCacheList_addition_sliced():
266271
def test_LazyCacheList_addition_error():
267272
lst = LazyCacheList(list(range(10)))
268273
with pytest.raises(TypeError):
269-
lst[:] + [1, 2, 3]
274+
lst[:] + (1, 2, 3)
270275

271276

272277
def test_LazyCacheListLazySingle():
@@ -276,6 +281,7 @@ def test_LazyCacheListLazySingle():
276281
assert len(lstc) == 10
277282
assert len(lstc._obj) == 2
278283
assert len(lstc._indices) == 2
284+
assert lstc._indices == [[0, 2, 4, 6, 8], [0, 2, 4, 6, 8]]
279285
assert lstc[:].tolist() == list(range(0, 20, 2))
280286
assert lstc[::2].tolist() == [0, 4, 8, 12, 16]
281287
assert lstc[[9]].tolist() == [18]
@@ -285,3 +291,14 @@ def test_LazyCacheListLazySingle():
285291
assert list(lstc)[8].tolist() == [16]
286292
with pytest.raises(IndexError):
287293
_ = lstc[10]
294+
295+
296+
def test_iter_LazyCacheList():
297+
lst = LazyCacheList(list(range(10)))
298+
for i, v in enumerate(lst):
299+
assert i == v
300+
301+
302+
def test_add_lists():
303+
lst = LazyCacheList(list(range(10)))[:]
304+
assert lst + [1, 2, 3] == list(range(10)) + [1, 2, 3]

0 commit comments

Comments
 (0)