Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix extended slicing on RedisList #335

Merged
merged 3 commits into from
Feb 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pottery/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@


__title__ = 'pottery'
__version__ = '1.1.3'
__version__ = '1.1.4'
__description__, __long_description__ = (
s.strip() for s in __doc__.split(sep='\n\n', maxsplit=1)
)
Expand Down
8 changes: 6 additions & 2 deletions pottery/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,15 @@ def __getitem__(self, index: Union[slice, int]) -> Any:
# our ridiculous hack is to get all of the elements between
# start and stop from Redis, then discard the ones between step
# in Python. More info:
# http://redis.io/commands/lrange
# http://redis.io/commands/lrange
with self._watch() as pipeline:
indices = self.__slice_to_indices(index)
if indices.step >= 0:
start, stop = indices.start, indices.stop-1
else:
start, stop = indices.stop+1, indices.start
pipeline.multi()
pipeline.lrange(self.key, indices[0], indices[-1])
pipeline.lrange(self.key, start, stop)
encoded = pipeline.execute()[0]
encoded = encoded[::index.step]
value: Union[List[JSONTypes], JSONTypes] = [self._decode(value) for value in encoded]
Expand Down
27 changes: 26 additions & 1 deletion tests/test_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def test_mutability_and_append(self):
cubes.append(7**3)
assert cubes == [1, 8, 27, 64, 125, 216, 343]

def test_slices(self):
def test_slicing(self):
letters = RedisList(
('a', 'b', 'c', 'd', 'e', 'f', 'g'),
redis=self.redis,
Expand Down Expand Up @@ -216,3 +216,28 @@ def test_json_dumps(self):
'["foo", "bar", "baz", "qux", "quux", "corge", "grault", "garply", '
'"waldo", "fred", "plugh", "xyzzy", "thud"]'
)

def test_extended_slicing(self):
python_list = [1, 2, 3, 4, 5]
redis_list = RedisList(python_list, redis=self.redis)
assert redis_list[len(redis_list)-1:3-1:-1] == python_list[len(python_list)-1:3-1:-1]

def test_slice_notation(self):
# I got these examples from:
# https://railsware.com/blog/python-for-machine-learning-indexing-and-slicing-for-lists-tuples-strings-and-other-sequential-types/#Slice_Notation
nums = RedisList((10, 20, 30, 40, 50, 60, 70, 80, 90), redis=self.redis)
assert nums[2:7] == [30, 40, 50, 60, 70]
assert nums[0:4] == [10, 20, 30, 40]
assert nums[:5] == [10, 20, 30, 40, 50]
assert nums[-3:] == [70, 80, 90]
assert nums[1:-1] == [20, 30, 40, 50, 60, 70, 80]
assert nums[-3:8] == [70, 80]
assert nums[-5:-1] == [50, 60, 70, 80]
assert nums[:-2] == [10, 20, 30, 40, 50, 60, 70]
assert nums[::2] == [10, 30, 50, 70, 90]
assert nums[1::2] == [20, 40, 60, 80]
assert nums[1:-3:2] == [20, 40, 60]
assert nums[::-1] == [90, 80, 70, 60, 50, 40, 30, 20, 10]
assert nums[-2::-1] == [80, 70, 60, 50, 40, 30, 20, 10]
assert nums[-2:1:-1] == [80, 70, 60, 50, 40, 30]
assert nums[-2:1:-3] == [80, 50]