Ejemplo n.º 1
0
def test_empty_pipe():
    with pytest.raises(functional_generic.PipeNotGivenAnyFunctions):
        functional_generic.pipe([
            1,
            2,
            3,
        ], )
Ejemplo n.º 2
0
async def _async_graph_traverse_many_inner(
    seen: Set[_Node],
    get_neighbors: Callable[[_Node], Awaitable[Iterable[_Node]]],
    process_node: Callable[[_Node], None],
    roots: Iterable[_Node],
):
    assert set(roots).isdisjoint(seen)

    functional_generic.pipe(
        roots,
        functional_generic.juxt(
            seen.update,
            functional_generic.compose_left(
                functional_generic.curried_map(process_node),
                tuple,
            ),
        ),
    )

    await functional_generic.pipe(
        roots,
        functional_generic.mapcat(get_neighbors),
        functional_generic.remove(functional.contains(seen)),
        frozenset,
        functional_generic.unless(
            functional.empty,
            _async_graph_traverse_many_inner(seen, get_neighbors,
                                             process_node),
        ),
    )
Ejemplo n.º 3
0
def test_latency():
    start_time = time.time()
    for _ in range(1000):
        functional_generic.pipe(
            True,
            functional_generic.juxt(functional.equals(True),
                                    functional.equals(False)),
            functional.head,
            functional.just("bla"),
            functional.attrgetter("lower"),
        )
    assert time.time() - start_time < 0.1
Ejemplo n.º 4
0
def groupby_many(f: Callable, it: Iterable) -> Dict[Text, Any]:
    """Return a mapping `{y: {x s.t. y in f(x)}}, where x in it. `

    Parameters:
    Key function (gets an object in collection and outputs tuple of keys).
    A Collection.

    Returns a dictionary where key has been computed by the `f` key function.

    >>> names = ['alice', 'bob', 'charlie', 'dan', 'edith', 'frank']
    >>> groupby_many(lambda name: (name[0], name[-1]), names)
    {'a': frozenset({'alice'}),
     'e': frozenset({'alice', 'charlie', 'edith'}),
     'b': frozenset({'bob'}),
     'c': frozenset({'charlie'}),
     'd': frozenset({'dan'}),
     'n': frozenset({'dan'}),
     'h': frozenset({'edith'}),
     'f': frozenset({'frank'}),
     'k': frozenset({'frank'})}"""
    return functional_generic.pipe(
        it,
        functional_generic.mapcat(
            functional_generic.compose_left(
                lambda element: (f(element), [element]),
                sync.star(itertools.product),
            ), ),
        edges_to_graph,
    )
Ejemplo n.º 5
0
def _has_cycle(sourced, get_neighbors, visited, node):
    if node in sourced:
        return True
    if node in visited:
        return False
    visited.add(node)
    return functional_generic.pipe(
        node,
        get_neighbors,
        functional_generic.anymap(
            _has_cycle(sourced | {node}, get_neighbors, visited)),
    )
Ejemplo n.º 6
0
def test_explode():
    assert functional_generic.pipe(
        [
            "x",
            [
                "y1",
                "y2",
                "y3",
            ],
            "z",
        ],
        data.explode(1),
        tuple,
    ) == (
        ("x", "y1", "z"),
        ("x", "y2", "z"),
        ("x", "y3", "z"),
    )
Ejemplo n.º 7
0
def has_cycle(graph):
    """Gets a graph, returns True if it contains a cycle, False else.

    >>> has_cycle({1: [2], 2: [3], 3: [1]})
    True
    >>> has_cycle({1: [2], 2: [3]})
    False"""
    return functional_generic.pipe(
        graph,
        dict.keys,
        functional_generic.curried_map(
            _has_cycle(
                frozenset(),
                dict_utils.dict_to_getter_with_default((), graph),
                set(),
            ), ),
        any,
    )
Ejemplo n.º 8
0
def tuple_of_tuples_to_csv(
    tuple_of_tuples: Tuple[Tuple[Any], ...],
    separator: Text = "\t",
) -> Text:
    """Return a CSV formatted string given a tuple of tuples. Each element is separated by the character "separator" (default is \t).

    >>> tuple_of_tuples_to_csv((("name", "age"), ("David", "23"), ("Itay", "26")))
    'name\\tage\\nDavid\\t23\\nItay\\t26'
    >>> tuple_of_tuples_to_csv((("name", "age"), ("David", "23"), ("Itay", "26")), " ")
    'name age\\nDavid 23\\nItay 26'
    """
    return functional_generic.pipe(
        tuple_of_tuples,
        functional_generic.curried_map(
            functional_generic.compose_left(
                functional_generic.curried_map(str),
                tuple,
                separator.join,
            ),
        ),
        "\n".join,
    )
Ejemplo n.º 9
0
def test_wrap_str():
    assert functional_generic.pipe("john",
                                   functional.wrap_str("hi {}")) == "hi john"
Ejemplo n.º 10
0
async def test_anymap_in_pipe():
    assert not functional_generic.pipe(
        [True, True, False],
        functional_generic.allmap(operator.not_),
    )