def test_fill_value(): '''Test `fill_value` keyword arguemnt for `partitions`.''' r = range(5) assert partitions(r, 3) == [[0, 1, 2], [3, 4]] assert partitions(r, 3, fill_value=None) == [[0, 1, 2], [3, 4, None]] assert partitions([], 3, fill_value=None) == []
def test_fill_value(): '''Test `fill_value` keyword arguemnt for `partitions`.''' r = list(range(5)) assert partitions(r, 3) == [[0, 1, 2], [3, 4]] assert partitions(r, 3, fill_value=None) == [[0, 1, 2], [3, 4, None]] with cute_testing.RaiseAssertor(text='fill_value'): partitions(r, 2, fill_value=None, allow_remainder=False) assert partitions([], 3, fill_value=None) == []
def random_partitions(sequence, partition_size=None, n_partitions=None, allow_remainder=True): ''' Randomly partition `sequence` into partitions of size `partition_size`. If the sequence can't be divided into precisely equal partitions, the last partition will contain less members than all the other partitions. Example: >>> random_partitions([0, 1, 2, 3, 4], 2) [[0, 2], [1, 4], [3]] (You need to give *either* a `partition_size` *or* an `n_partitions` argument, not both.) Specify `allow_remainder=False` to enforce that the all the partition sizes be equal; if there's a remainder while `allow_remainder=False`, an exception will be raised. ''' shuffled_sequence = shuffled(sequence) return sequence_tools.partitions( shuffled_sequence, partition_size=partition_size, n_partitions=n_partitions, allow_remainder=allow_remainder )
def test_larger_on_remainder(): r = list(range(9)) assert partitions(r, 1, larger_on_remainder=True) == \ partitions(r, n_partitions=9, larger_on_remainder=True) == \ [[0], [1], [2], [3], [4], [5], [6], [7], [8]] assert partitions(r, 2, larger_on_remainder=True) == \ partitions(r, n_partitions=4, larger_on_remainder=True) == \ partitions(r, n_partitions=4, larger_on_remainder=True, fill_value='gurr') == \ [[0, 1], [2, 3], [4, 5], [6, 7, 8]] assert partitions(r, 3, larger_on_remainder=True) == \ partitions(r, n_partitions=3, larger_on_remainder=True, fill_value='gurr') == \ [[0, 1, 2], [3, 4, 5], [6, 7, 8]] assert partitions(tuple(r), 4, larger_on_remainder=True) == \ [(0, 1, 2, 3), (4, 5, 6, 7, 8)] assert partitions(tuple(r), n_partitions=3, larger_on_remainder=True) == \ [(0, 1, 2), (3, 4, 5), (6, 7, 8)] assert partitions([1], 1, larger_on_remainder=True) == \ partitions([1], 2, larger_on_remainder=True) == \ partitions([1], n_partitions=1, larger_on_remainder=True) == \ partitions([1], 3, larger_on_remainder=True) == \ partitions([1], 4, larger_on_remainder=True) == \ partitions([1], 1000, larger_on_remainder=True) == \ partitions([1], 1000, larger_on_remainder=True, fill_value='meow') == \ [[1]] with cute_testing.RaiseAssertor(text='remainder of 1'): partitions([1], 1000, larger_on_remainder=True, allow_remainder=False, fill_value='meow')
def test_allow_remainder(): '''Test `partitions` complains when there's an unallowed remainder.''' r = range(9) # 9 divides by 1, 3 and 9, so no problems here: assert partitions(r, 1, allow_remainder=False) == \ partitions(r, n_partitions=9, allow_remainder=False) == \ [[0], [1], [2], [3], [4], [5], [6], [7], [8]] assert partitions(r, 3, allow_remainder=False) == \ partitions(r, n_partitions=3, allow_remainder=False) == \ [[0, 1, 2], [3, 4, 5], [6, 7, 8]] # ...But now we try 2, 4 and 5 and get exceptions: with cute_testing.RaiseAssertor(text='remainder'): partitions(r, 2, allow_remainder=False) with cute_testing.RaiseAssertor(text='remainder'): partitions(r, 4, allow_remainder=False) with cute_testing.RaiseAssertor(text='remainder'): partitions(r, 5, allow_remainder=False) with cute_testing.RaiseAssertor(text='remainder'): partitions(r, n_partitions=2, allow_remainder=False) with cute_testing.RaiseAssertor(text='remainder'): partitions(r, n_partitions=4, allow_remainder=False) with cute_testing.RaiseAssertor(text='remainder'): partitions(r, n_partitions=5, allow_remainder=False)
def test_too_many_arguments(): '''Test `partitions` complains when too many arguments are given.''' with cute_testing.RaiseAssertor(text='*either*'): partitions([1, 2, 3], 2, n_partitions=2)
def test(): '''Test the basic workings of `partitions`.''' r = range(8) assert partitions(r, 1) == partitions(r, n_partitions=8) == \ [[0], [1], [2], [3], [4], [5], [6], [7]] assert partitions(r, 2) == partitions(r, n_partitions=4) == \ [[0, 1], [2, 3], [4, 5], [6, 7]] assert partitions(r, 3) == partitions(r, n_partitions=3) == \ [[0, 1, 2], [3, 4, 5], [6, 7]] assert partitions(r, 4) == partitions(r, n_partitions=2) == \ [[0, 1, 2, 3], [4, 5, 6, 7]] assert partitions(r, 5) == [[0, 1, 2, 3, 4], [5, 6, 7]] assert partitions(r, 6) == [[0, 1, 2, 3, 4, 5], [6, 7]] assert partitions(r, 7) == [[0, 1, 2, 3, 4, 5, 6], [7]] assert partitions(r, 8) == partitions(r, 9) == partitions(r, 100) == \ [[0, 1, 2, 3, 4, 5, 6, 7]]
else: perm_type_options = (NO_ARGUMENT,) product_space_ = combi.ProductSpace( ((variation_selection,), perm_space_type_options, iterable_or_length_and_sequence_options, domain_to_cut_options, n_elements_options, is_combination_options, purified_fixed_map_options, degrees_options, slice_options, perm_type_options) ) for i in range(len(product_space_)): fucking_globals = dict(globals()) fucking_globals.update(locals()) yield eval( 'lambda: _check_variation_selection(*product_space_[%s])' % i, fucking_globals, locals() ) # We use this shit because Nose can't parallelize generator tests: lambdas = [] for i, f in enumerate(_iterate_tests()): f.name = 'f_%s' % i locals()[f.name] = f lambdas.append(f) for i, partition in enumerate(sequence_tools.partitions(lambdas, 500)): exec('def test_%s(): return (%s)' % (i, ', '.join('%s()'% f.name for f in partition)))
def test_too_many_arguments(): """Test `partitions` complains when too many arguments are given.""" with cute_testing.RaiseAssertor(text="*either*"): partitions([1, 2, 3], 2, 2)
perm_type_options = (FruityComb, ) else: perm_type_options = (FruityPerm, ) else: perm_type_options = (NO_ARGUMENT, ) product_space_ = combi.ProductSpace( ((variation_selection, ), perm_space_type_options, iterable_or_length_and_sequence_options, domain_to_cut_options, n_elements_options, is_combination_options, purified_fixed_map_options, degrees_options, slice_options, perm_type_options)) for i in range(len(product_space_)): fucking_globals = dict(globals()) fucking_globals.update(locals()) yield eval( 'lambda: _check_variation_selection(*product_space_[%s])' % i, fucking_globals, locals()) # We use this shit because Nose can't parallelize generator tests: lambdas = [] for i, f in enumerate(_iterate_tests()): f.name = 'f_%s' % i locals()[f.name] = f lambdas.append(f) for i, partition in enumerate(sequence_tools.partitions(lambdas, 500)): exec('def test_%s(): return (%s)' % (i, ', '.join('%s()' % f.name for f in partition)))