Skip to content

Commit 565f370

Browse files
committed
-
1 parent 29a8882 commit 565f370

File tree

3 files changed

+19
-7
lines changed

3 files changed

+19
-7
lines changed

source_py3/python_toolbox/misc_tools/misc_tools.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,4 +369,7 @@ def __len__(self):
369369
raise OverflowError("Due to CPython limitation, you'll have to "
370370
"use `.length` rather than `len`")
371371

372+
def __bool__(self):
373+
from python_toolbox import sequence_tools
374+
return bool(sequence_tools.get_length(self))
372375

source_py3/python_toolbox/sequence_tools/cute_range.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ def length(self):
139139
The length of the `CuteRange`.
140140
141141
We're using a property `.length` rather than the built-in `__len__`
142-
because `__len__` can't handle infinite values.
142+
because `__len__` can't handle infinite values or floats.
143143
'''
144144
from python_toolbox import math_tools
145145

@@ -200,23 +200,27 @@ def __getitem__(self, i):
200200
raise TypeError
201201

202202
def __len__(self):
203-
# Sadly Python doesn't allow infinity here.
204-
return self.length if (self.length not in infinities) else 0
203+
# Sadly Python doesn't allow infinity or floats here.
204+
return self.length if isinstance(self.length, numbers.Integral) else 0
205205

206206
def __contains__(self, i):
207207
try: self.index(i)
208208
except ValueError: return False
209209
else: return True
210210

211211
def index(self, i):
212+
from python_toolbox import math_tools
212213
if not isinstance(i, numbers.Number):
213214
raise ValueError
214215
else:
215216
distance = i - self.start
217+
if distance == 0 and self:
218+
return True
216219
if math_tools.get_sign(distance) != math_tools.get_sign(self.step):
217220
raise ValueError
218-
index, remainder = divmod(distance, self.step)
219-
if remainder == 0:
221+
index, remainder = math_tools.cute_divmod(distance, self.step)
222+
if remainder == 0 and (0 <= index < self.length or
223+
index == self.length == infinity):
220224
return index
221225
else:
222226
raise ValueError

source_py3/test_python_toolbox/test_sequence_tools/test_cute_range.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,13 @@ def test_illegal():
5959
def test_float():
6060
cr = CuteRange(10, 20, 1.5)
6161
assert list(cr) == [10, 11.5, 13, 14.5, 16, 17.5, 19]
62-
assert len(cr) == len(list(cr)) == 7
63-
assert list(map(range(7), cr.__getitem__)) == list(cr)
62+
for item in list(cr):
63+
assert item in cr
64+
assert 20 not in cr
65+
assert 20.5 not in cr
66+
assert 8.5 not in cr
67+
assert cr.length == len(list(cr)) == 7
68+
assert list(map(cr.__getitem__, range(7))) == list(cr)
6469

6570
float_range_arguments_tuples = (
6671
(10, 20, 1.5), (20, 10.5, -0.33), (10.3, infinity, 2.5),

0 commit comments

Comments
 (0)