Skip to content

Commit 880b7a1

Browse files
committed
-
1 parent 0b2fb34 commit 880b7a1

File tree

3 files changed

+40
-3
lines changed

3 files changed

+40
-3
lines changed

source_py3/python_toolbox/combi/product_space.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def __init__(self, sequences):
2828
ensure_iterable_is_immutable_sequence(sequences)
2929
self.sequence_lengths = tuple(map(sequence_tools.get_length,
3030
self.sequences))
31-
self.length = misc_tools.general_product(self.sequence_lengths)
31+
self.length = math_tools.product(self.sequence_lengths)
3232

3333
def __repr__(self):
3434
return '<%s: %s>' % (
@@ -58,7 +58,7 @@ def __getitem__(self, i):
5858

5959

6060
_reduced = property(lambda self: (type(self), self.sequences))
61-
61+
__hash__ = lambda self: hash(self._reduced)
6262
__eq__ = lambda self, other: (isinstance(other, ProductSpace) and
6363
self._reduced == other._reduced)
6464

source_py3/python_toolbox/math_tools/misc.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,8 @@ def binomial(big, small):
127127
return (math.factorial(big) // math.factorial(big - small)
128128
// math.factorial(small))
129129

130+
131+
def product(numbers):
132+
from python_toolbox import misc_tools
133+
return misc_tools.general_product(numbers, start=1)
134+

source_py3/test_python_toolbox/test_combi/test_product_space.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
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

@@ -16,4 +18,34 @@ def test_product_spaces():
1618
assert perm_1 in big_perm_space
1719
assert (perm_0, perm_1) in product_space
1820
assert product_space.index((perm_0, perm_1)) == 10 ** 10
19-
assert ( ~ perm_0, ~ perm_1) in product_space
21+
assert (~ perm_0, ~ perm_1) in product_space
22+
assert repr(product_space) == (
23+
'<ProductSpace: 933262154439441526816992388562667004907159682643816214'
24+
'685929638952175999932299156089414639761565182862536979208272237582511'
25+
'85210916864000000000000000000000000 * 208755412068>'
26+
)
27+
28+
assert product_space
29+
assert not ProductSpace(((),))
30+
assert not ProductSpace(((), {}))
31+
with cute_testing.RaiseAssertor(IndexError):
32+
product_space[product_space.length]
33+
with cute_testing.RaiseAssertor(IndexError):
34+
product_space[product_space.length + 7]
35+
with cute_testing.RaiseAssertor(IndexError):
36+
product_space[-product_space.length - 1]
37+
with cute_testing.RaiseAssertor(IndexError):
38+
product_space[-product_space.length - 100]
39+
40+
assert {ProductSpace((range(4), range(3))),
41+
ProductSpace((range(4), range(3))),
42+
ProductSpace((range(3), range(4)))} == {
43+
ProductSpace((range(4), range(3))),
44+
ProductSpace((range(3), range(4)))
45+
}
46+
47+
assert ProductSpace((range(4), range(3))) == \
48+
ProductSpace((range(4), range(3)))
49+
assert ProductSpace((range(4), range(3))) != \
50+
ProductSpace((range(3), range(4)))
51+

0 commit comments

Comments
 (0)