Skip to content

Commit 0dadd07

Browse files
committed
-
1 parent 1180b1f commit 0dadd07

File tree

6 files changed

+43
-19
lines changed

6 files changed

+43
-19
lines changed

source_py3/python_toolbox/combi/chain_space.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,13 @@ def __getitem__(self, i):
6565
if isinstance(i, slice):
6666
raise NotImplementedError
6767
assert isinstance(i, int)
68+
if i <= -1:
69+
i += self.length
70+
if i < 0:
71+
raise IndexError
72+
if self.accumulated_lengths.is_exhausted and i >= self.length:
73+
raise IndexError
74+
# Todo: Can't have a binary search here, it exhausts all the sequences.
6875
sequence_index = binary_search.binary_search_by_index(
6976
self.accumulated_lengths, lambda x: x,
7077
i, rounding=binary_search.LOW_IF_BOTH
@@ -93,9 +100,14 @@ def index(self, item):
93100
for sequence, accumulated_length in zip(self.sequences,
94101
self.accumulated_lengths):
95102
try:
96-
return sequence.index(item) + accumulated_length
103+
index_in_sequence = sequence.index(item)
97104
except ValueError:
98105
pass
106+
except TypeError:
107+
assert isinstance(sequence, (str, bytes)) and \
108+
(not isinstance(item, (str, bytes)))
109+
else:
110+
return index_in_sequence + accumulated_length
99111
else:
100112
raise ValueError
101113

source_py3/python_toolbox/nifty_collections/lazy_tuple.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def __init__(self, iterable, definitely_infinite=False):
8383
was_given_a_sequence = isinstance(iterable, collections.Sequence) and \
8484
not isinstance(iterable, LazyTuple)
8585

86-
self.exhausted = True if was_given_a_sequence else False
86+
self.is_exhausted = True if was_given_a_sequence else False
8787
'''Flag saying whether the internal iterator is totally exhausted.'''
8888

8989
self.collected_data = iterable if was_given_a_sequence else []
@@ -143,7 +143,7 @@ def exhaust(self, i=infinity):
143143
'''
144144
from python_toolbox import sequence_tools
145145

146-
if self.exhausted:
146+
if self.is_exhausted:
147147
return
148148

149149
elif isinstance(i, int) or i == infinity:
@@ -170,7 +170,7 @@ def exhaust(self, i=infinity):
170170
with self.lock:
171171
self.collected_data.append(next(self._iterator))
172172
except StopIteration:
173-
self.exhausted = True
173+
self.is_exhausted = True
174174
break
175175

176176

@@ -252,7 +252,7 @@ def __repr__(self):
252252
253253
The '...' denotes a non-exhausted lazy tuple.
254254
'''
255-
if self.exhausted:
255+
if self.is_exhausted:
256256
inner = repr(self.collected_data)
257257

258258
else: # not self.exhausted

source_py3/test_python_toolbox/test_collection_tools/test_get_all_contained_counters.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def test():
2222

2323
# Now we'll exhaust `all_contained_counters`:
2424
assert len(all_contained_counters) == 6 * 3 * 3 * 2 * 2
25-
assert all_contained_counters.exhausted
25+
assert all_contained_counters.is_exhausted
2626

2727
assert counter in all_contained_counters
2828
assert collections.Counter({'a': 0, 'b': 0, 'r': 0, 'c': 0, 'd': 0,}) in \

source_py3/test_python_toolbox/test_combi/test_chain_space.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
# Copyright 2009-2014 Ram Rachum.
22
# This program is distributed under the MIT license.
33

4+
from python_toolbox import cute_testing
5+
46
from python_toolbox.combi import *
57

68

79
def test_chain_spaces():
8-
chain_space = ChainSpace((range(3), 'meow', range(2, -1, -1)))
9-
assert tuple(chain_space) == (0, 1, 2, 'm', 'e', 'o', 'w', 2, 1, 0)
10+
chain_space = ChainSpace((range(3), 'meow', range(22, 19, -1)))
11+
assert tuple(chain_space) == (0, 1, 2, 'm', 'e', 'o', 'w', 22, 21, 20)
1012
assert len(chain_space) == chain_space.length == 10
1113
assert bool(chain_space) is True
1214
for i, item in enumerate(chain_space):
@@ -19,6 +21,16 @@ def test_chain_spaces():
1921
assert 'm' in chain_space
2022
assert [] not in chain_space
2123

24+
with cute_testing.RaiseAssertor(ValueError): chain_space.index('nope')
25+
with cute_testing.RaiseAssertor(IndexError): chain_space[-11]
26+
with cute_testing.RaiseAssertor(IndexError): chain_space[-110]
27+
with cute_testing.RaiseAssertor(IndexError): chain_space[11]
28+
with cute_testing.RaiseAssertor(IndexError): chain_space[1100]
29+
30+
assert chain_space[-1] == 20
31+
assert chain_space[-2] == 21
32+
assert chain_space[-10] == 0
33+
2234
assert not ChainSpace(())
2335

2436

source_py3/test_python_toolbox/test_cute_iter_tools/test_enumerate.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,4 @@ def test():
2929
for i, j in lazy_tuple:
3030
assert i == j
3131

32-
assert lazy_tuple.exhausted
32+
assert lazy_tuple.is_exhausted

source_py3/test_python_toolbox/test_nifty_collections/test_lazy_tuple/test_lazy_tuple.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def test():
3030
self_aware_uuid_iterator = SelfAwareUuidIterator()
3131
lazy_tuple = LazyTuple(self_aware_uuid_iterator)
3232
assert len(self_aware_uuid_iterator.data) == 0
33-
assert not lazy_tuple.exhausted
33+
assert not lazy_tuple.is_exhausted
3434
assert repr(lazy_tuple) == '<LazyTuple: (...)>'
3535

3636
first = lazy_tuple[0]
@@ -49,7 +49,7 @@ def test():
4949
assert len(self_aware_uuid_iterator.data) == 16
5050
assert len(weird_slice) == 4
5151
assert weird_slice[2] == first_ten[-1] == lazy_tuple[9]
52-
assert not lazy_tuple.exhausted
52+
assert not lazy_tuple.is_exhausted
5353

5454
iterator_twenty = cute_iter_tools.shorten(lazy_tuple, 20)
5555
assert len(self_aware_uuid_iterator.data) == 16
@@ -90,7 +90,7 @@ def test_string():
9090
'''Test a `LazyTuple` built from a string.'''
9191
string = 'meow'
9292
lazy_tuple = LazyTuple(string)
93-
assert lazy_tuple.exhausted
93+
assert lazy_tuple.is_exhausted
9494
assert repr(lazy_tuple) == "<LazyTuple: 'meow'>"
9595
assert ''.join(lazy_tuple) == string
9696
assert ''.join(lazy_tuple[1:-1]) == string[1:-1]
@@ -104,10 +104,10 @@ def test_string():
104104
def test_infinite():
105105
'''Test an infinite `LazyTuple`.'''
106106
lazy_tuple = LazyTuple(itertools.count())
107-
assert not lazy_tuple.exhausted
107+
assert not lazy_tuple.is_exhausted
108108
lazy_tuple[100]
109109
assert len(lazy_tuple.collected_data) == 101
110-
assert not lazy_tuple.exhausted
110+
assert not lazy_tuple.is_exhausted
111111

112112

113113
def test_factory_decorator():
@@ -128,15 +128,15 @@ def test_finite_iterator():
128128
'''Test `LazyTuple` on a finite iterator.'''
129129
my_finite_iterator = iter(range(5))
130130
lazy_tuple = LazyTuple(my_finite_iterator)
131-
assert not lazy_tuple.exhausted
131+
assert not lazy_tuple.is_exhausted
132132

133133
assert list(itertools.islice(lazy_tuple, 0, 2)) == [0, 1]
134-
assert not lazy_tuple.exhausted
134+
assert not lazy_tuple.is_exhausted
135135
assert repr(lazy_tuple) == '<LazyTuple: [0, 1]...>'
136136

137137
second_to_last = lazy_tuple[-2]
138138
assert second_to_last == 3
139-
assert lazy_tuple.exhausted
139+
assert lazy_tuple.is_exhausted
140140
assert len(lazy_tuple) == lazy_tuple.known_length == \
141141
len(lazy_tuple.collected_data)
142142
assert repr(lazy_tuple) == '<LazyTuple: [0, 1, 2, 3, 4]>'
@@ -151,10 +151,10 @@ def test_finite_iterator():
151151

152152

153153
identical_lazy_tuple = LazyTuple(iter(range(5)))
154-
assert not identical_lazy_tuple.exhausted
154+
assert not identical_lazy_tuple.is_exhausted
155155
my_dict = {}
156156
my_dict[identical_lazy_tuple] = 'flugzeug'
157-
assert identical_lazy_tuple.exhausted
157+
assert identical_lazy_tuple.is_exhausted
158158
assert my_dict[lazy_tuple] == 'flugzeug'
159159
assert len(my_dict) == 1
160160
assert lazy_tuple == identical_lazy_tuple

0 commit comments

Comments
 (0)