Skip to content

Commit

Permalink
len_slice: Support sequences of integer indices.
Browse files Browse the repository at this point in the history
Adds support for sequences of integer indices. Normalizes their values
to ensure they are all valid first. Then simply counts how many integer
indices are present to get their length.
  • Loading branch information
jakirkham committed Dec 7, 2016
1 parent 12d1f63 commit 7b6cb72
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions kenjutsu/kenjutsu.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,18 +317,22 @@ def len_slice(a_slice, a_length=None):

new_slice = reformat_slice(a_slice, a_length)

if new_slice.stop is None:
if new_slice.step > 0:
raise UnknownSliceLengthException(
"Cannot determine slice length without a defined end point. " +
"The reformatted slice was " + repr(new_slice) + "."
)
else:
new_slice = slice(new_slice.start, -1, new_slice.step)
new_slice_size = 0
if isinstance(new_slice, slice):
if new_slice.stop is None:
if new_slice.step > 0:
raise UnknownSliceLengthException(
"Cannot determine slice length without a defined end"
" point. The reformatted slice was %s." % repr(new_slice)
)
else:
new_slice = slice(new_slice.start, -1, new_slice.step)

new_slice_diff = float(new_slice.stop - new_slice.start)
new_slice_diff = float(new_slice.stop - new_slice.start)

new_slice_size = int(math.ceil(new_slice_diff / new_slice.step))
new_slice_size = int(math.ceil(new_slice_diff / new_slice.step))
else:
new_slice_size = len(new_slice)

return(new_slice_size)

Expand Down

0 comments on commit 7b6cb72

Please sign in to comment.