Skip to content

Commit c991e10

Browse files
committed
-
1 parent 095405d commit c991e10

File tree

2 files changed

+29
-25
lines changed

2 files changed

+29
-25
lines changed

source_py3/python_toolbox/combi/selection_space.py

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ def __init__(self, sequence):
2828
self.sequence = \
2929
sequence_tools.ensure_iterable_is_immutable_sequence(sequence)
3030
self.sequence_length = len(self.sequence)
31+
self._sequence_set = set(self.sequence)
3132
self.length = 2 ** self.sequence_length
3233

3334

@@ -51,41 +52,37 @@ def __getitem__(self, i):
5152

5253
assert len(binary_i) == self.sequence_length
5354

54-
return tuple(item for (is_included, item) in
55-
zip(map(int, binary_i), self.sequence) if is_included)
55+
return set(item for (is_included, item) in
56+
zip(map(int, binary_i), self.sequence) if is_included)
5657

5758

5859
_reduced = property(lambda self: (type(self), self.sequence))
5960

6061
__eq__ = lambda self, other: (isinstance(other, SelectionSpace) and
6162
self._reduced == other._reduced)
6263

63-
# def __contains__(self, given_sequence):
64-
# try:
65-
# self.index(given_sequence)
66-
# except ValueError:
67-
# return False
68-
# else:
69-
# return True
64+
def __contains__(self, given_iterable):
65+
try:
66+
self.index(given_iterable)
67+
except ValueError:
68+
return False
69+
else:
70+
return True
7071

71-
# def index(self, given_sequence):
72-
# if not isinstance(given_sequence, collections.Sequence) or \
73-
# not len(given_sequence) == len(self.sequences):
74-
# raise ValueError
72+
def index(self, given_iterable):
73+
if not isinstance(given_iterable, collections.Iterable):
74+
raise ValueError
7575

76-
# reverse_indices = []
77-
# current_radix = 1
76+
given_iterable_set = set(given_iterable)
7877

79-
# wip_index = 0
80-
81-
# for item, sequence in reversed(tuple(zip(given_sequence, self.sequences))):
82-
# wip_index += sequence.index(item) # Propagating `ValueError`
83-
# current_radix *= misc.get_length(sequence)
84-
85-
# return wip_index
78+
if not given_iterable_set <= self._sequence_set:
79+
raise ValueError
80+
81+
return sum((2 ** i) for i, item in enumerate(reversed(self.sequence))
82+
if item in given_iterable_set)
8683

8784

88-
# __bool__ = lambda self: bool(self.length)
85+
__bool__ = lambda self: bool(self.length)
8986

9087

9188

source_py3/test_python_toolbox/test_combi/test_selection_space.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,14 @@
77
def test():
88
selection_space = SelectionSpace(range(5))
99
assert len(tuple(selection_space)) == len(selection_space) == 2 ** 5
10-
assert selection_space[0] == ()
11-
assert selection_space[-1] == tuple(range(5))
10+
assert selection_space[0] == set()
11+
assert selection_space[-1] == set(range(5))
1212

13+
for i, selection in selection_space:
14+
assert selection in selection_space
15+
assert selection_space.index(selection) == i
16+
17+
assert (1, 6) not in selection_space
18+
assert 'foo' not in selection_space
19+
assert (1, 3, 5) in selection_space
1320

0 commit comments

Comments
 (0)