def test_chained_from_chained5(): # Verify that unwrapping does NOT work if the inner chained has all x = chained(int, chained(float, complex, all=True), str) funcs = x.__reduce__()[1] assert funcs[0] is int assert type(funcs[1]) is chained # no unwrapping assert funcs[2] is str
def test_chained_from_chained1(): # Verify that unwrapping works x = chained(int, chained(float, complex), str) funcs = x.__reduce__()[1] assert funcs[0] is int assert funcs[1] is float assert funcs[2] is complex assert funcs[3] is str
def test_chained_from_chained3(): # Verify that unwrapping works x = chained(int, chained(float, complex), str, reverse=True) funcs = x.__reduce__()[1] assert funcs[0] is str assert funcs[1] is float # still second! assert funcs[2] is complex # still third! assert funcs[3] is int
def test_chained_from_chained4(): # Verify that unwrapping works with multiple chained x = chained(chained(int, list), chained(float, complex), chained(str, tuple, set)) funcs = x.__reduce__()[1] assert funcs[0] is int assert funcs[1] is list assert funcs[2] is float assert funcs[3] is complex assert funcs[4] is str assert funcs[5] is tuple assert funcs[6] is set
def test_chained_repr1(): x = chained(int, float) r = repr(x) assert 'chained' in r assert 'int' in r assert 'float' in r assert 'all=False' in r
def test_traverse(): """To test the traverse implementation we call gc.collect() while instances of all the C objects are still valid.""" acc = iteration_utilities.accumulate([]) app = iteration_utilities.applyfunc(lambda x: x, 1) cha = iteration_utilities.chained(int, float) cla = iteration_utilities.clamp([], 0, 1) com = iteration_utilities.complement(int) con = iteration_utilities.constant(1) dee = iteration_utilities.deepflatten([]) dup = iteration_utilities.duplicates([]) fli = iteration_utilities.flip(int) gro = iteration_utilities.grouper([], 2) ine = iteration_utilities.intersperse([], 1) iik = iteration_utilities.ItemIdxKey(10, 2) ite = iteration_utilities.iter_except(int, TypeError) mer = iteration_utilities.merge([]) nth = iteration_utilities.nth(1) pac = iteration_utilities.packed(int) par = iteration_utilities.partial(int, 10) rep = iteration_utilities.replicate([], 3) rou = iteration_utilities.roundrobin([]) see = iteration_utilities.Seen() sid = iteration_utilities.sideeffects([], lambda x: x) spl = iteration_utilities.split([], lambda x: True) sta = iteration_utilities.starfilter(lambda x: True, []) suc = iteration_utilities.successive([]) tab = iteration_utilities.tabulate(int) une = iteration_utilities.unique_everseen([]) unj = iteration_utilities.unique_justseen([]) gc.collect()
def test_chained_pickle3(protocol): cmp = chained(iteration_utilities.square, iteration_utilities.double, all=True) x = pickle.dumps(cmp, protocol=protocol) assert pickle.loads(x)(T(10)) == (T(100), T(20)) assert pickle.loads(x)(T(3)) == (T(9), T(6))
def test_chained_pickle2(protocol): cmp = chained(iteration_utilities.square, iteration_utilities.double, reverse=True) x = pickle.dumps(cmp, protocol=protocol) assert pickle.loads(x)(T(10)) == T(400) assert pickle.loads(x)(T(3)) == T(36)
def test_chained_repr2(): x = chained(int, float, complex, str, reverse=True, all=True) r = repr(x) assert 'chained' in r assert 'int' in r assert 'float' in r assert 'complex' in r assert 'str' in r assert 'all=True' in r
def argsorted(iterable, key=None, reverse=False): """Returns the indices that would sort the `iterable`. Parameters ---------- iterable : iterable The `iterable` to sort. key : callable, None, optional If ``None`` sort the items in the `iterable`, otherwise sort the ``key(items)``. Default is ``None``. reverse : :py:class:`bool`, optional If ``False`` sort the `iterable` in increasing order otherwise in decreasing order. Default is ``False``. Returns ------- sortindices : :py:class:`list` The indices that would sort the `iterable`. Notes ----- See :py:func:`sorted` for more explanations to the parameters. Examples -------- To get the indices that would sort a sequence in increasing order:: >>> from iteration_utilities import argsorted >>> argsorted([3, 1, 2]) [1, 2, 0] It also works when sorting in decreasing order:: >>> argsorted([3, 1, 2], reverse=True) [0, 2, 1] And when applying a `key` function:: >>> argsorted([3, 1, -2], key=abs) [1, 2, 0] """ if key is None: key = itemgetter(1) else: key = chained(itemgetter(1), key) return [ i[0] for i in sorted(enumerate(iterable), key=key, reverse=reverse) ]
def test_c_funcs_signatures(): # Makes sure every user-facing C function has a valid signature. from iteration_utilities import Iterable, chained from itertools import chain from operator import itemgetter from inspect import Signature # Gett all C functions it = (Iterable(chain(_iteration_utilities.__dict__.items())) # only include those that do not start with an underscore, # we only need user-facing functions/classes .filterfalse(lambda x: x[0].startswith(('_'))) # only include those that have a __module__, to exclude things # like "return_None", "first" which do not have a signature .filter(lambda x: hasattr(x[1], '__module__')) # only include those that are really part of the package .filter(lambda x: x[1].__module__.startswith('iteration_utilities')) # remove duplicates .unique_everseen(itemgetter(0)) # get the signature, fails if it can't .map(lambda x: (x[0], x[1], Signature.from_callable(x[1])))) # Just need to trigger evaluation, use sorted because it's nice for manual # debugging! it.get_sorted(key=chained(itemgetter(0), str.lower))
def test_chained_normal1(): double_increment = chained(lambda x: x * 2, lambda x: x + 1) assert double_increment(T(10)) == T(21) assert double_increment(T(2)) == T(5)
def test_chained_attributes1(): chd = chained(bool, int) assert chd.funcs == (bool, int) assert not chd.all
def test_chained_all1(): double_increment = chained(lambda x: x * 2, lambda x: x + 1, all=True) assert double_increment(T(10)) == (T(20), T(11)) assert double_increment(T(2)) == (T(4), T(3))
def test_chained_failure2(): with pytest.raises(TypeError): # kwarg not accepted chained(lambda x: x + 1, invalidkwarg=lambda x: x * 2)
def test_chained_failure1(): with pytest.raises(TypeError): # at least one func must be present chained()
def test_chained_from_chained2(): # Verify that unwrapping works (chained from chained) x = chained(chained(float, complex)) funcs = x.__reduce__()[1] assert funcs[0] is float assert funcs[1] is complex
def test_chained_failure3(): with pytest.raises(TypeError): # func fails chained(lambda x: x + 1)(T('a'))
def test_chained_failure5(): with pytest.raises(TypeError): # second func fails chained(lambda x: x * 2, lambda x: x + 1, all=True)(T('a'))
def test_chained_failure_setstate1(): helper_funcs.iterator_setstate_list_fail(chained(lambda x: x))
def test_chained_failure_setstate2(): helper_funcs.iterator_setstate_empty_fail(chained(lambda x: x))
def test_chained_pickle1(protocol): cmp = chained(iteration_utilities.square, iteration_utilities.reciprocal) x = pickle.dumps(cmp, protocol=protocol) assert pickle.loads(x)(T(10)) == T(1 / 100) assert pickle.loads(x)(T(2)) == T(1 / 4)
def test_chained_reverse1(): double_increment = chained(lambda x: x * 2, lambda x: x + 1, reverse=True) assert double_increment(T(10)) == T(22) assert double_increment(T(2)) == T(6)