Skip to content

Commit ae47923

Browse files
committed
-
1 parent c597d67 commit ae47923

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

source_py3/python_toolbox/combi/product_space.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,27 @@
88

99

1010
class ProductSpace(sequence_tools.CuteSequenceMixin, collections.Sequence):
11+
'''
12+
A product space between sequences.
13+
14+
This is similar to Python's builtin `itertools.product`, except that it
15+
behaves like a sequence rather than an iterable. (Though it's also
16+
iterable.) You can access any item by its index number.
17+
18+
Example:
19+
20+
>>> product_space = ProductSpace(('abc', range(4)))
21+
>>> product_space
22+
<ProductSpace: 3 * 4>
23+
>>> product_space.length
24+
12
25+
>>> product_space[10]
26+
('c', 2)
27+
>>> tuple(product_space)
28+
(('a', 0), ('a', 1), ('a', 2), ('a', 3), ('b', 0), ('b', 1), ('b', 2),
29+
('b', 3), ('c', 0), ('c', 1), ('c', 2), ('c', 3))
30+
31+
'''
1132
def __init__(self, sequences):
1233
self.sequences = sequence_tools. \
1334
ensure_iterable_is_immutable_sequence(sequences)
@@ -48,6 +69,7 @@ def __getitem__(self, i):
4869
self._reduced == other._reduced)
4970

5071
def index(self, given_sequence):
72+
'''Get the index number of `given_sequence` in this product space.'''
5173
if not isinstance(given_sequence, collections.Sequence) or \
5274
not len(given_sequence) == len(self.sequences):
5375
raise ValueError

0 commit comments

Comments
 (0)