Python toolz.curry() Examples

The following are 7 code examples of toolz.curry(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module toolz , or try the search function .
Example #1
Source File: core.py    From catalyst with Apache License 2.0 6 votes vote down vote up
def ensure_doctest(f, name=None):
    """Ensure that an object gets doctested. This is useful for instances
    of objects like curry or partial which are not discovered by default.

    Parameters
    ----------
    f : any
        The thing to doctest.
    name : str, optional
        The name to use in the doctest function mapping. If this is None,
        Then ``f.__name__`` will be used.

    Returns
    -------
    f : any
       ``f`` unchanged.
    """
    _getframe(2).f_globals.setdefault('__test__', {})[
        f.__name__ if name is None else name
    ] = f
    return f 
Example #2
Source File: plugin_registry.py    From altair with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def get(self) -> Optional[PluginType]:
        """Return the currently active plugin."""
        if self._options:
            return curry(self._active, **self._options)
        else:
            return self._active 
Example #3
Source File: test_pipeline.py    From fklearn with Apache License 2.0 5 votes vote down vote up
def test_build_pipeline_learner_assertion(has_repeated_learners):
    @fp.curry
    def learner(df, a, b, c=3):
        return lambda dataset: dataset + a + b + c, df, {}

    learner_fn = learner(b=2)

    with pytest.raises(ValueError):
        build_pipeline(learner_fn, has_repeated_learners=has_repeated_learners)

    learner_fn = learner(a=1, b=2)

    build_pipeline(learner_fn) 
Example #4
Source File: test_pipeline.py    From fklearn with Apache License 2.0 5 votes vote down vote up
def test_build_pipeline_predict_arguments_assertion(has_repeated_learners):
    test_df = pd.DataFrame({"x": [1, 2, 3, 4, 5], "y": [2, 4, 6, 8, 10]})

    @fp.curry
    def invalid_learner(df):
        def p(dataset, *a, **b):
            return dataset + len(a) + len(b)

        return p, df, {}

    with pytest.raises(ValueError):
        build_pipeline(invalid_learner, has_repeated_learners=has_repeated_learners)(test_df) 
Example #5
Source File: test_pipeline.py    From fklearn with Apache License 2.0 5 votes vote down vote up
def test_build_pipeline_serialisation():
    df_train = pd.DataFrame({
        'id': ["id1"],
        'x1': [10.0],
        'y': [2.3]
    })

    fn = lambda x: x

    @fp.curry
    def dummy_learner(df, fn, call):
        return fn, df, {f"dummy_learner_{call}": {}}

    @fp.curry
    def dummy_learner_2(df, fn, call):
        return dummy_learner(df, fn, call)

    @fp.curry
    def dummy_learner_3(df, fn, call):
        return fn, df, {f"dummy_learner_{call}": {}, "obj": "a"}

    train_fn = build_pipeline(
        dummy_learner(fn=fn, call=1),
        dummy_learner_2(fn=fn, call=2),
        dummy_learner_3(fn=fn, call=3))

    predict_fn, pred_train, log = train_fn(df_train)

    fkml = {"pipeline": ["dummy_learner", "dummy_learner_2", "dummy_learner_3"],
            "output_columns": ['id', 'x1', 'y'],
            "features": ['id', 'x1', 'y'],
            "learners": {"dummy_learner": {"fn": fn, "log": {"dummy_learner_1": {}}},
                         "dummy_learner_2": {"fn": fn, "log": {"dummy_learner_2": {}}},
                         "dummy_learner_3": {"fn": fn, "log": {"dummy_learner_3": {}}, "obj": "a"}}}

    assert log["__fkml__"] == fkml
    assert "obj" not in log.keys() 
Example #6
Source File: test_pipeline.py    From fklearn with Apache License 2.0 5 votes vote down vote up
def test_build_pipeline_repeated_learners_serialisation():
    df_train = pd.DataFrame({
        'id': ["id1"],
        'x1': [10.0],
        'y': [2.3]
    })

    fn = lambda x: x

    @fp.curry
    def dummy_learner(df, fn, call):
        return fn, df, {f"dummy_learner_{call}": {}}

    @fp.curry
    def dummy_learner_2(df, fn, call):
        return dummy_learner(df, fn, call)

    train_fn = build_pipeline(
        dummy_learner(fn=fn, call=1),
        dummy_learner_2(fn=fn, call=2),
        dummy_learner(fn=fn, call=3),
        has_repeated_learners=True)

    predict_fn, pred_train, log = train_fn(df_train)

    fkml = {"pipeline": ["dummy_learner", "dummy_learner_2", "dummy_learner"],
            "output_columns": ['id', 'x1', 'y'],
            "features": ['id', 'x1', 'y'],
            "learners": {
                "dummy_learner": [
                    {"fn": fn, "log": {"dummy_learner_1": {}}},
                    {"fn": fn, "log": {"dummy_learner_3": {}}}],
                "dummy_learner_2": [{"fn": fn, "log": {"dummy_learner_2": {}}}]}}

    assert log["__fkml__"] == fkml
    assert "obj" not in log.keys() 
Example #7
Source File: test_curried.py    From eth-utils with MIT License 4 votes vote down vote up
def test_curried_namespace():
    def should_curry(value):
        if not callable(value) or isinstance(value, curry) or isinstance(value, type):
            return False
        if isinstance(value, type) and issubclass(value, Exception):
            return False
        nargs = enhanced_num_required_args(value)
        if nargs is None or nargs > 1:
            return True
        else:
            return nargs == 1 and enhanced_has_keywords(value)

    def curry_namespace(ns):
        return dict(
            (name, curry(f) if should_curry(f) else f)
            for name, f in ns.items()
            if "__" not in name
        )

    all_auto_curried = curry_namespace(vars(eth_utils))

    inferred_namespace = valfilter(callable, all_auto_curried)
    curried_namespace = valfilter(callable, eth_utils.curried.__dict__)

    if inferred_namespace != curried_namespace:
        missing = set(inferred_namespace) - set(curried_namespace)
        if missing:
            to_insert = sorted("%s," % f for f in missing)
            raise AssertionError(
                "There are missing functions in eth_utils.curried:\n"
                + "\n".join(to_insert)
            )
        extra = set(curried_namespace) - set(inferred_namespace)
        if extra:
            raise AssertionError(
                "There are extra functions in eth_utils.curried:\n"
                + "\n".join(sorted(extra))
            )
        unequal = merge_with(list, inferred_namespace, curried_namespace)
        unequal = valfilter(lambda x: x[0] != x[1], unequal)
        to_curry = keyfilter(lambda x: should_curry(getattr(eth_utils, x)), unequal)
        if to_curry:
            to_curry_formatted = sorted("{0} = curry({0})".format(f) for f in to_curry)
            raise AssertionError(
                "There are missing functions to curry in eth_utils.curried:\n"
                + "\n".join(to_curry_formatted)
            )
        elif unequal:
            not_to_curry_formatted = sorted(unequal)
            raise AssertionError(
                "Missing functions NOT to curry in eth_utils.curried:\n"
                + "\n".join(not_to_curry_formatted)
            )
        else:
            raise AssertionError(
                "unexplained difference between %r and %r"
                % (inferred_namespace, curried_namespace)
            )