<s> import pickle <EOL> import itertools <EOL> import collections <EOL> import ast <EOL> import nose <EOL> from python_toolbox import nifty_collections <EOL> from python_toolbox import context_management <EOL> from python_toolbox import cute_iter_tools <EOL> from python_toolbox import cute_testing <EOL> from python_toolbox import misc_tools <EOL> from python_toolbox import sequence_tools <EOL> from python_toolbox import combi <EOL> from python_toolbox . combi import * <EOL> infinity = float ( '<STR_LIT>' ) <EOL> infinities = ( infinity , - infinity ) <EOL> class _NO_ARGUMENT_TYPE ( type ) : <EOL> __repr__ = lambda cls : '<STR_LIT>' % cls . __name__ <EOL> class NO_ARGUMENT ( object ) : <EOL> __metaclass__ = _NO_ARGUMENT_TYPE <EOL> class BrutePermSpace ( object ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , iterable_or_length , domain = None , n_elements = None , <EOL> fixed_map = { } , degrees = None , is_combination = False , <EOL> slice_ = None , perm_type = None ) : <EOL> self . sequence = tuple ( iterable_or_length ) if isinstance ( iterable_or_length , collections . Iterable ) else sequence_tools . CuteRange ( iterable_or_length ) <EOL> self . sequence_length = len ( self . sequence ) <EOL> self . _sequence_frozen_bag = nifty_collections . FrozenBag ( self . sequence ) <EOL> self . is_recurrent = len ( set ( self . sequence ) ) < len ( self . sequence ) <EOL> self . n_elements = n_elements if n_elements is not None else len ( self . sequence ) <EOL> self . domain = ( domain or <EOL> sequence_tools . CuteRange ( self . sequence_length ) ) [ : self . n_elements ] <EOL> self . fixed_map = dict ( ( key , value ) for key , value in fixed_map . items ( ) <EOL> if key in self . domain ) <EOL> self . degrees = degrees or sequence_tools . CuteRange ( self . sequence_length ) <EOL> self . is_combination = is_combination <EOL> self . is_degreed = ( self . degrees != <EOL> sequence_tools . CuteRange ( self . sequence_length ) ) <EOL> self . slice_ = slice_ <EOL> if perm_type is None : <EOL> self . perm_type = tuple <EOL> self . is_typed = False <EOL> else : <EOL> self . perm_type = FruityTuple <EOL> self . is_typed = True <EOL> def __iter__ ( self ) : <EOL> if ( self . is_recurrent and self . is_combination ) : <EOL> def make_iterator ( ) : <EOL> crap = set ( ) <EOL> for item in itertools . imap ( self . perm_type , self . _iter ( ) ) : <EOL> fc = nifty_collections . FrozenBag ( item ) <EOL> if fc in crap : <EOL> continue <EOL> else : <EOL> yield item <EOL> crap . add ( fc ) <EOL> iterator = make_iterator ( ) <EOL> else : <EOL> iterator = iter ( itertools . imap ( self . perm_type , self . _iter ( ) ) ) <EOL> if self . slice_ : <EOL> return itertools . islice ( iterator , self . slice_ . start , <EOL> self . slice_ . stop ) <EOL> else : <EOL> return iterator <EOL> def _iter ( self ) : <EOL> yielded_candidates = set ( ) <EOL> for candidate in itertools . permutations ( self . sequence , self . n_elements ) : <EOL> if candidate in yielded_candidates : <EOL> continue <EOL> if any ( candidate [ self . domain . index ( key ) ] != value for <EOL> key , value in self . fixed_map . items ( ) ) : <EOL> continue <EOL> if self . is_combination : <EOL> i = - <NUM_LIT:1> <EOL> rule_out_because_of_bad_comb_order = False <EOL> for item in candidate : <EOL> try : <EOL> i = self . sequence . index ( item , i + <NUM_LIT:1> ) <EOL> except ValueError : <EOL> rule_out_because_of_bad_comb_order = True <EOL> if rule_out_because_of_bad_comb_order : <EOL> continue <EOL> if self . is_degreed : <EOL> unvisited_items = set ( sequence_tools . CuteRange ( self . sequence_length ) ) <EOL> n_cycles = <NUM_LIT:0> <EOL> while unvisited_items : <EOL> starting_item = current_item = next ( iter ( unvisited_items ) ) <EOL> while current_item in unvisited_items : <EOL> unvisited_items . remove ( current_item ) <EOL> current_item = self . sequence . index ( <EOL> candidate [ current_item ] <EOL> ) <EOL> if current_item == starting_item : <EOL> n_cycles += <NUM_LIT:1> <EOL> degree = self . sequence_length - n_cycles <EOL> if degree not in self . degrees : <EOL> continue <EOL> yielded_candidates . add ( candidate ) <EOL> yield candidate <EOL> class FruityMixin ( object ) : pass <EOL> class FruityPerm ( FruityMixin , Perm ) : pass <EOL> class FruityComb ( FruityMixin , Comb ) : pass <EOL> class FruityTuple ( FruityMixin , tuple ) : pass <EOL> def _check_variation_selection ( variation_selection , perm_space_type , <EOL> iterable_or_length_and_sequence , domain_to_cut , <EOL> n_elements , is_combination , purified_fixed_map , <EOL> degrees , slice_ , perm_type ) : <EOL> assert isinstance ( variation_selection , <EOL> combi . perming . variations . VariationSelection ) <EOL> kwargs = { } <EOL> iterable_or_length , sequence = iterable_or_length_and_sequence <EOL> kwargs [ '<STR_LIT>' ] = iterable_or_length <EOL> sequence_set = set ( sequence ) <EOL> if domain_to_cut != NO_ARGUMENT : <EOL> kwargs [ '<STR_LIT>' ] = actual_domain = domain_to_cut [ : len ( sequence ) ] <EOL> else : <EOL> actual_domain = sequence_tools . CuteRange ( len ( sequence ) ) <EOL> if n_elements != NO_ARGUMENT : <EOL> kwargs [ '<STR_LIT>' ] = n_elements <EOL> actual_n_elements = n_elements if ( n_elements != NO_ARGUMENT ) else <NUM_LIT:0> <EOL> if is_combination != NO_ARGUMENT : <EOL> kwargs [ '<STR_LIT>' ] = is_combination <EOL> if purified_fixed_map != NO_ARGUMENT : <EOL> kwargs [ '<STR_LIT>' ] = actual_fixed_map = dict ( <EOL> ( actual_domain [ key ] , sequence [ value ] ) for key , value <EOL> in purified_fixed_map . items ( ) if key < len ( sequence ) <EOL> ) <EOL> else : <EOL> actual_fixed_map = { } <EOL> if variation_selection . is_degreed : <EOL> kwargs [ '<STR_LIT>' ] = degrees = ( <NUM_LIT:0> , <NUM_LIT:2> , <NUM_LIT:4> , <NUM_LIT:5> ) <EOL> if perm_type != NO_ARGUMENT : <EOL> kwargs [ '<STR_LIT>' ] = perm_type <EOL> try : <EOL> perm_space = perm_space_type ( ** kwargs ) <EOL> except ( combi . UnallowedVariationSelectionException , TypeError ) : <EOL> if not variation_selection . is_allowed : <EOL> return <EOL> else : <EOL> raise <EOL> if slice_ != NO_ARGUMENT : <EOL> perm_space = perm_space [ slice_ ] <EOL> else : <EOL> if not variation_selection . is_allowed : <EOL> raise TypeError ( <EOL> "<STR_LIT>" <EOL> ) <EOL> brute_perm_space = BrutePermSpace ( <EOL> slice_ = ( perm_space . canonical_slice if variation_selection . is_sliced else <EOL> None ) , <EOL> ** kwargs <EOL> ) <EOL> assert perm_space . variation_selection == variation_selection <EOL> assert perm_space . sequence_length == len ( sequence ) <EOL> assert ( perm_space . domain == perm_space . sequence ) == ( <EOL> not variation_selection . is_dapplied and <EOL> not variation_selection . is_rapplied and <EOL> not variation_selection . is_partial <EOL> ) <EOL> if perm_space . length : <EOL> assert perm_space . index ( perm_space [ - <NUM_LIT:1> ] ) == perm_space . length - <NUM_LIT:1> <EOL> assert perm_space . index ( perm_space [ <NUM_LIT:0> ] ) == <NUM_LIT:0> <EOL> if variation_selection . is_partial : <EOL> assert <NUM_LIT:0> < perm_space . n_unused_elements == len ( sequence ) - actual_n_elements <EOL> else : <EOL> assert perm_space . n_unused_elements == <NUM_LIT:0> <EOL> assert perm_space == PermSpace ( ** kwargs ) [ perm_space . canonical_slice ] <EOL> assert ( not perm_space != PermSpace ( ** kwargs ) [ perm_space . canonical_slice ] ) <EOL> assert hash ( perm_space ) == hash ( PermSpace ( ** kwargs ) [ perm_space . canonical_slice ] ) <EOL> typed_perm_space = perm_space . get_typed ( FruityComb if <EOL> variation_selection . is_combination else FruityPerm ) <EOL> assert typed_perm_space . is_typed <EOL> assert variation_selection . is_typed is perm_space . is_typed is ( perm_space != perm_space . untyped ) is ( perm_space == typed_perm_space ) <EOL> if perm_space . is_sliced and perm_space . length >= <NUM_LIT:2> : <EOL> assert perm_space [ <NUM_LIT:0> ] == perm_space . unsliced [ <NUM_LIT:2> ] <EOL> assert perm_space [ <NUM_LIT:1> ] == perm_space . unsliced [ <NUM_LIT:3> ] <EOL> assert perm_space [ - <NUM_LIT:1> ] == perm_space . unsliced [ - <NUM_LIT:3> ] <EOL> assert perm_space [ - <NUM_LIT:2> ] == perm_space . unsliced [ - <NUM_LIT:4> ] <EOL> assert perm_space . unsliced [ <NUM_LIT:0> ] not in perm_space <EOL> assert perm_space . unsliced [ <NUM_LIT:1> ] not in perm_space <EOL> assert perm_space . unsliced [ <NUM_LIT:2> ] in perm_space <EOL> assert perm_space . unsliced [ - <NUM_LIT:1> ] not in perm_space <EOL> assert perm_space . unsliced [ - <NUM_LIT:2> ] not in perm_space <EOL> assert perm_space . unsliced [ - <NUM_LIT:3> ] in perm_space <EOL> if perm_space : <EOL> next ( iter ( brute_perm_space ) ) <EOL> for i , ( perm , brute_perm_tuple ) in enumerate ( <EOL> itertools . islice ( itertools . izip ( perm_space , brute_perm_space ) , <NUM_LIT:10> ) ) : <EOL> assert tuple ( perm ) == brute_perm_tuple <EOL> assert perm in perm_space <EOL> assert tuple ( perm ) in perm_space <EOL> assert iter ( list ( perm ) ) in perm_space <EOL> assert set ( perm ) not in perm_space <EOL> assert isinstance ( perm , combi . Perm ) <EOL> assert perm . is_rapplied == variation_selection . is_rapplied <EOL> assert perm . is_dapplied == variation_selection . is_dapplied <EOL> assert perm . is_partial == variation_selection . is_partial <EOL> assert perm . is_combination == variation_selection . is_combination <EOL> assert perm . is_pure == ( not ( variation_selection . is_rapplied or <EOL> variation_selection . is_dapplied or <EOL> variation_selection . is_partial or <EOL> variation_selection . is_combination ) ) <EOL> assert isinstance ( perm , FruityMixin ) is variation_selection . is_typed <EOL> if variation_selection . is_rapplied : <EOL> assert perm != perm . unrapplied <EOL> if not variation_selection . is_recurrent : <EOL> perm . unrapplied == perm_space . unrapplied [ i ] <EOL> else : <EOL> assert perm == perm . unrapplied == perm_space . unrapplied [ i ] <EOL> if not variation_selection . is_dapplied : <EOL> sample_domain = '<STR_LIT>' [ : len ( sequence ) ] <EOL> assert perm . apply ( sample_domain ) == sample_domain * perm <EOL> assert tuple ( sample_domain * perm ) == tuple ( <EOL> perm_space . get_rapplied ( sample_domain ) [ i ] . _perm_sequence <EOL> ) <EOL> if variation_selection . is_dapplied : <EOL> assert perm != perm . undapplied == perm_space . undapplied [ i ] <EOL> else : <EOL> assert perm == perm . undapplied == perm_space . undapplied [ i ] <EOL> if variation_selection . is_combination : <EOL> if variation_selection . is_typed : <EOL> with cute_testing . RaiseAssertor ( TypeError ) : <EOL> perm . uncombinationed <EOL> else : <EOL> assert perm != perm . uncombinationed <EOL> else : <EOL> assert perm == perm . uncombinationed <EOL> if variation_selection . is_combination : <EOL> if variation_selection . is_typed : <EOL> assert type ( perm ) == FruityComb <EOL> else : <EOL> assert type ( perm ) == Comb <EOL> else : <EOL> if variation_selection . is_typed : <EOL> assert type ( perm ) == FruityPerm <EOL> else : <EOL> assert type ( perm ) == Perm <EOL> if variation_selection . variations <= set ( ( <EOL> perming . variations . Variation . DAPPLIED , <EOL> perming . variations . Variation . RAPPLIED , <EOL> perming . variations . Variation . COMBINATION ) ) : <EOL> assert perm . nominal_perm_space == perm_space <EOL> assert perm . nominal_perm_space == perm_space . _nominal_perm_space_of_perms == perm_space . unsliced . undegreed . unfixed <EOL> if not variation_selection . is_fixed and not variation_selection . is_degreed : <EOL> assert perm_space . index ( perm ) == i <EOL> assert type ( perm ) ( iter ( perm ) , perm_space = perm_space ) == perm <EOL> assert type ( perm ) ( perm . _perm_sequence , perm_space = perm_space ) == perm <EOL> assert perm . length == perm_space . n_elements <EOL> if variation_selection . is_partial or variation_selection . is_rapplied or variation_selection . is_dapplied : <EOL> with cute_testing . RaiseAssertor ( TypeError ) : <EOL> ~ perm <EOL> with cute_testing . RaiseAssertor ( TypeError ) : <EOL> perm . inverse <EOL> with cute_testing . RaiseAssertor ( TypeError ) : <EOL> perm ** - <NUM_LIT:1> <EOL> else : <EOL> assert ~ perm == perm . inverse == perm ** - <NUM_LIT:1> <EOL> assert ~ ~ perm == perm . inverse . inverse == perm == perm ** <NUM_LIT:1> <EOL> assert ( perm * ~ perm ) == ( ~ perm * perm ) == perm . nominal_perm_space [ <NUM_LIT:0> ] <EOL> assert isinstance ( perm ** <NUM_LIT:4> , Perm ) <EOL> assert isinstance ( perm ** - <NUM_LIT:7> , Perm ) <EOL> perm_set = set ( perm ) <EOL> if variation_selection . is_partial : <EOL> assert len ( perm ) == actual_n_elements <EOL> if variation_selection . is_recurrent : <EOL> assert perm_set <= sequence_set <EOL> else : <EOL> assert perm_set < sequence_set <EOL> assert len ( perm_set ) == actual_n_elements <EOL> else : <EOL> assert perm_set == sequence_set <EOL> assert len ( perm ) == len ( sequence ) <EOL> for j , ( value , key , ( key__ , value__ ) ) in enumerate ( <EOL> zip ( perm , perm . as_dictoid , perm . items ) ) : <EOL> assert key == key__ <EOL> assert value == perm . as_dictoid [ key ] == value__ <EOL> assert perm . items [ j ] == ( key , value ) <EOL> if not variation_selection . is_recurrent : <EOL> assert perm . index ( value ) == key <EOL> assert perm [ key ] == value <EOL> assert key in perm . domain <EOL> assert value in perm <EOL> if variation_selection . is_degreed : <EOL> assert perm . degree == degrees or perm . degree in degrees <EOL> elif variation_selection . is_partial : <EOL> assert perm . degree == NotImplemented <EOL> else : <EOL> assert <NUM_LIT:0> <= perm . degree <= len ( sequence ) <EOL> if variation_selection . is_combination or variation_selection . is_recurrent or variation_selection . is_partial : <EOL> with cute_testing . RaiseAssertor ( NotImplementedError ) : <EOL> neighbors = perm . get_neighbors ( perm_space = perm_space ) <EOL> else : <EOL> neighbors = perm . get_neighbors ( perm_space = perm_space ) <EOL> if variation_selection . is_degreed and perm . degree in ( <NUM_LIT:0> , <NUM_LIT:2> ) : <EOL> assert not neighbors <EOL> else : <EOL> if perm_space . length >= <NUM_LIT:5> : <EOL> assert neighbors <EOL> for neigbhor in itertools . islice ( neighbors , <NUM_LIT:0> , <NUM_LIT:10> ) : <EOL> assert neigbhor in perm_space <EOL> assert len ( cute_iter_tools . zip_non_equal ( ( perm , neigbhor ) , <EOL> lazy_tuple = True ) ) == <NUM_LIT:2> <EOL> perm_repr = repr ( perm ) <EOL> def _iterate_tests ( ) : <EOL> for variation_selection in combi . perming . variations . variation_selection_space : <EOL> kwargs = { } <EOL> if variation_selection . is_recurrent and not variation_selection . is_rapplied : <EOL> assert not variation_selection . is_allowed <EOL> continue <EOL> if variation_selection . is_recurrent : <EOL> iterable_or_length_and_sequence_options = ( <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:5> , <NUM_LIT:4> , <NUM_LIT:3> ) , <EOL> ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:5> , <NUM_LIT:4> , <NUM_LIT:3> ) ) <EOL> ) <EOL> elif variation_selection . is_rapplied : <EOL> iterable_or_length_and_sequence_options = ( <EOL> ( [ <NUM_LIT:1> , <NUM_LIT:4> , <NUM_LIT:2> , <NUM_LIT:5> , <NUM_LIT:3> , <NUM_LIT:7> ] , <EOL> ( <NUM_LIT:1> , <NUM_LIT:4> , <NUM_LIT:2> , <NUM_LIT:5> , <NUM_LIT:3> , <NUM_LIT:7> ) ) , <EOL> ) <EOL> else : <EOL> iterable_or_length_and_sequence_options = ( <EOL> ( <NUM_LIT:7> , sequence_tools . CuteRange ( <NUM_LIT:7> ) ) , <EOL> ( sequence_tools . CuteRange ( <NUM_LIT:9> ) , sequence_tools . CuteRange ( <NUM_LIT:9> ) ) <EOL> ) <EOL> if variation_selection . is_dapplied : <EOL> domain_to_cut_options = ( <EOL> '<STR_LIT>' , <EOL> [ <NUM_LIT:7> + i ** <NUM_LIT:2> for i in range ( <NUM_LIT:20> ) ] <EOL> ) <EOL> else : <EOL> domain_to_cut_options = ( NO_ARGUMENT , ) <EOL> if variation_selection . is_partial : <EOL> n_elements_options = ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:5> ) <EOL> else : <EOL> n_elements_options = ( NO_ARGUMENT , ) <EOL> perm_space_type_options = ( PermSpace , ) <EOL> if variation_selection . is_combination : <EOL> is_combination_options = ( True , ) <EOL> else : <EOL> is_combination_options = ( NO_ARGUMENT , ) <EOL> if variation_selection . is_fixed : <EOL> purified_fixed_map_options = ( <EOL> { <NUM_LIT:0> : <NUM_LIT:1> , <NUM_LIT:4> : <NUM_LIT:3> , } , <EOL> { <NUM_LIT:0> : <NUM_LIT:0> , <NUM_LIT:1> : - <NUM_LIT:2> , - <NUM_LIT:2> : - <NUM_LIT:3> , } , <EOL> ) <EOL> else : <EOL> purified_fixed_map_options = ( NO_ARGUMENT , ) <EOL> if variation_selection . is_degreed : <EOL> degrees_options = ( <EOL> ( <NUM_LIT:0> , <NUM_LIT:2> , <NUM_LIT:4> , <NUM_LIT:5> ) , <EOL> <NUM_LIT:1> , <EOL> ) <EOL> else : <EOL> degrees_options = ( NO_ARGUMENT , ) <EOL> if variation_selection . is_sliced : <EOL> slice_options = ( <EOL> slice ( <NUM_LIT:2> , - <NUM_LIT:2> ) , <EOL> slice ( <NUM_LIT:3> , <NUM_LIT:4> ) <EOL> ) <EOL> else : <EOL> slice_options = ( NO_ARGUMENT , ) <EOL> if variation_selection . is_typed : <EOL> if variation_selection . is_combination : <EOL> perm_type_options = ( FruityComb , ) <EOL> else : <EOL> perm_type_options = ( FruityPerm , ) <EOL> else : <EOL> perm_type_options = ( NO_ARGUMENT , ) <EOL> product_space_ = combi . ProductSpace ( <EOL> ( ( variation_selection , ) , perm_space_type_options , <EOL> iterable_or_length_and_sequence_options , domain_to_cut_options , <EOL> n_elements_options , is_combination_options , <EOL> purified_fixed_map_options , degrees_options , slice_options , <EOL> perm_type_options ) <EOL> ) <EOL> for i in range ( len ( product_space_ ) ) : <EOL> fucking_globals = dict ( globals ( ) ) <EOL> fucking_globals . update ( locals ( ) ) <EOL> yield eval ( <EOL> '<STR_LIT>' % i , <EOL> fucking_globals , locals ( ) <EOL> ) <EOL> lambdas = [ ] <EOL> for i , f in enumerate ( _iterate_tests ( ) ) : <EOL> f . name = '<STR_LIT>' % i <EOL> locals ( ) [ f . name ] = f <EOL> lambdas . append ( f ) <EOL> for i , partition in enumerate ( sequence_tools . partitions ( lambdas , <NUM_LIT> ) ) : <EOL> exec ( '<STR_LIT>' % <EOL> ( i , '<STR_LIT:U+002CU+0020>' . join ( '<STR_LIT>' % f . name for f in partition ) ) ) </s>
