Skip to content

Commit 0be7903

Browse files
committed
-
1 parent c3b34f4 commit 0be7903

File tree

4 files changed

+111
-26
lines changed

4 files changed

+111
-26
lines changed

source_py3/python_toolbox/combi/perming/_variation_adding_mixin.py

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,12 @@ class _VariationAddingMixin:
1111
'''Mixin for `PermSpace` to add variations to a perm space.'''
1212
def get_rapplied(self, sequence):
1313
'''Get a version of this `PermSpace` that has a range of `sequence`.'''
14-
assert not self.is_rapplied
14+
if self.is_rapplied:
15+
raise TypeError('This space is already rapplied, to rapply it to a '
16+
'different sequence please use `.unrapplied` '
17+
'first.')
1518
sequence = \
16-
sequence_tools.ensure_iterable_is_immutable_sequence(sequence)
19+
sequence_tools.ensure_iterable_is_immutable_sequence(sequence)
1720
if len(sequence) != self.sequence_length:
1821
raise Exception
1922
return PermSpace(
@@ -72,10 +75,14 @@ def combinationed(self):
7275

7376
def get_dapplied(self, domain):
7477
'''Get a version of this `PermSpace` that has a domain of `domain`.'''
78+
from . import variations
79+
7580
if self.is_combination:
76-
raise TypeError("Can't use a domain with combination spaces.")
77-
domain = \
78-
sequence_tools.ensure_iterable_is_immutable_sequence(domain)
81+
raise variations.UnallowedVariationSelectionException(
82+
{variations.Variation.DAPPLIED: True,
83+
variations.Variation.COMBINATION: True,}
84+
)
85+
domain = sequence_tools.ensure_iterable_is_immutable_sequence(domain)
7986
if len(domain) != self.n_elements:
8087
raise Exception
8188
return PermSpace(
@@ -90,8 +97,11 @@ def get_dapplied(self, domain):
9097
def get_fixed(self, fixed_map):
9198
'''Get a fixed version of this `PermSpace`.'''
9299
if self.is_sliced:
93-
raise TypeError("Can't be used on sliced perm spaces. Try "
94-
"`perm_space.unsliced.get_fixed(...)`.")
100+
raise TypeError(
101+
"Can't be used on sliced perm spaces. Try "
102+
"`perm_space.unsliced.get_fixed(...)`. You may then re-slice "
103+
"the resulting space."
104+
)
95105
combined_fixed_map = dict(self.fixed_map)
96106
for key, value in fixed_map.items():
97107
if key in self.fixed_map:
@@ -106,11 +116,19 @@ def get_fixed(self, fixed_map):
106116

107117
def get_degreed(self, degrees):
108118
'''Get a version of this `PermSpace` restricted to certain degrees.'''
119+
from . import variations
120+
109121
if self.is_sliced:
110-
raise TypeError("Can't be used on sliced perm spaces. Try "
111-
"`perm_space.unsliced.get_degreed(...)`.")
122+
raise TypeError(
123+
"Can't be used on sliced perm spaces. Try "
124+
"`perm_space.unsliced.get_degreed(...)`. You may then "
125+
"re-slice the resulting space."
126+
)
112127
if self.is_combination:
113-
raise TypeError("Can't use degrees with combination spaces.")
128+
raise variations.UnallowedVariationSelectionException(
129+
{variations.Variation.DEGREED: True,
130+
variations.Variation.COMBINATION: True,}
131+
)
114132
degrees = sequence_tools.to_tuple(degrees, item_type=int)
115133
if not degrees:
116134
return self

source_py3/python_toolbox/combi/perming/_variation_removing_mixin.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,16 @@ def undegreed(self):
164164
doc='''An unsliced version of this `PermSpace`.'''
165165
)
166166

167+
untyped = caching.CachedProperty(
168+
lambda self: PermSpace(
169+
self.sequence, n_elements=self.n_elements, domain=self.domain,
170+
fixed_map=self.fixed_map, is_combination=self.is_combination,
171+
degrees=self.degrees, slice_=self.slice_,
172+
perm_type=self.default_perm_type
173+
),
174+
doc='''An untyped version of this `PermSpace`.'''
175+
)
176+
167177
###########################################################################
168178
###########################################################################
169179

@@ -183,13 +193,3 @@ def _get_just_fixed(self):
183193
lambda self: self.unsliced.undegreed.unfixed,
184194
)
185195

186-
untyped = caching.CachedProperty(
187-
lambda self: PermSpace(
188-
self.sequence, n_elements=self.n_elements, domain=self.domain,
189-
fixed_map=self.fixed_map, is_combination=self.is_combination,
190-
degrees=self.degrees, slice_=self.slice_,
191-
perm_type=self.default_perm_type
192-
),
193-
doc='''An untyped version of this `PermSpace`.'''
194-
)
195-

source_py3/python_toolbox/combi/perming/perm_space.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -334,9 +334,11 @@ def __init__(self, iterable_or_length, n_elements=None, *, domain=None,
334334
{variations.Variation.DEGREED: True,
335335
variations.Variation.RECURRENT: True,}
336336
)
337-
self.degrees = tuple(
337+
# The space is degreed; we canonicalize `degrees` into a sorted
338+
# tuple.
339+
self.degrees = tuple(sorted(
338340
degree for degree in degrees if degree in all_degrees
339-
)
341+
))
340342

341343
# #
342344
### Finished figuring out degrees. ####################################
@@ -735,10 +737,14 @@ def __getitem__(self, i):
735737
__iter__ = lambda self: (self[i] for i in
736738
sequence_tools.CuteRange(self.length))
737739
_reduced = property(
738-
lambda self: (type(self), self.sequence, self.domain,
739-
tuple(sorted(self.fixed_map.items())),
740-
self.canonical_slice, self.perm_type)
740+
lambda self: (
741+
type(self), self.sequence, self.domain,
742+
tuple(sorted(self.fixed_map.items())), self.degrees,
743+
self.canonical_slice, self.perm_type
744+
)
741745
)
746+
# (No need to include `n_degrees` because it's implied by `domain`. No need
747+
# to include `is_combination` because it's implied by `type(self)`.)
742748

743749
__eq__ = lambda self, other: (isinstance(other, PermSpace) and
744750
self._reduced == other._reduced)

source_py3/test_python_toolbox/test_combi/test_perm_space.py

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -683,4 +683,65 @@ def stupid_score(self):
683683
assert PokerHand(card_comb_sequence[::-1], poker_hand_space) \
684684
not in poker_hand_space
685685
assert PokerHand(card_comb_sequence, poker_hand_space).stupid_score == \
686-
(1, 1, 1, 1, 1)
686+
(1, 1, 1, 1, 1)
687+
688+
689+
def test_variations_make_unequal():
690+
691+
class BluePerm(Perm): pass
692+
class RedPerm(Perm): pass
693+
694+
695+
perm_space = PermSpace(4)
696+
697+
assert perm_space == perm_space
698+
699+
assert perm_space != perm_space.get_rapplied('meow') != \
700+
perm_space.get_rapplied('woof')
701+
assert perm_space.get_rapplied('meow') == perm_space.get_rapplied('meow')
702+
assert perm_space.get_rapplied('woof') == perm_space.get_rapplied('woof')
703+
704+
# We're intentionally comparing partial spaces with 1 and 3 elements,
705+
# because they have the same length, and we want to be sure that they're
706+
# unequal despite of that, and thus that `PermSpace.__eq__` doesn't rely on
707+
# length alone but actually checks `n_elements`.
708+
assert perm_space != perm_space.get_partialled(1) != \
709+
perm_space.get_partialled(3)
710+
assert perm_space.get_partialled(1) == perm_space.get_partialled(1)
711+
assert perm_space.get_partialled(3) == perm_space.get_partialled(3)
712+
713+
assert perm_space != perm_space.combinationed
714+
assert perm_space != perm_space.get_dapplied('loud') != \
715+
perm_space.get_dapplied('blue')
716+
assert perm_space.get_dapplied('loud') == perm_space.get_dapplied('loud')
717+
assert perm_space.get_dapplied('blue') == perm_space.get_dapplied('blue')
718+
719+
assert perm_space != perm_space.get_fixed({1: 2,}) != \
720+
perm_space.get_fixed({3: 2,})
721+
assert perm_space.get_fixed({1: 2,}) == perm_space.get_fixed({1: 2,})
722+
assert perm_space.get_fixed({3: 2,}) == perm_space.get_fixed({3: 2,})
723+
724+
# We're intentionally comparing spaces with degrees 1 and 3, because they
725+
# have the same length, and we want to be sure that they're unequal despite
726+
# of that, and thus that `PermSpace.__eq__` doesn't rely on length alone
727+
# but actually checks the degrees.
728+
assert perm_space != perm_space.get_degreed(1) != \
729+
perm_space.get_degreed(3) != perm_space.get_degreed((1, 3)) != perm_space
730+
assert perm_space.get_degreed(2) == perm_space.get_degreed(2)
731+
assert perm_space.get_degreed(3) == perm_space.get_degreed(3)
732+
assert perm_space.get_degreed((1, 3)) == \
733+
perm_space.get_degreed((3, 1)) == perm_space.get_degreed((1, 3))
734+
735+
assert perm_space != perm_space[:-1] != perm_space[1:]
736+
assert perm_space[:-1] == perm_space[:-1]
737+
assert perm_space[1:] == perm_space[1:]
738+
739+
assert perm_space != perm_space.get_typed(BluePerm) != \
740+
perm_space.get_typed(RedPerm)
741+
assert perm_space.get_typed(BluePerm) == perm_space.get_typed(BluePerm)
742+
assert perm_space.get_typed(RedPerm) == perm_space.get_typed(RedPerm)
743+
744+
745+
746+
747+

0 commit comments

Comments
 (0)