Ejemplo n.º 1
0
def test_kwargs():
    """@TODO: Docs. Contribution is welcome."""
    r = Registry("")

    r.add(bar=foo)

    r.get("bar")
Ejemplo n.º 2
0
def test_add_function_name_override():
    """@TODO: Docs. Contribution is welcome."""
    r = Registry("")

    r.add(foo, name="bar")

    assert "bar" in r._factories
Ejemplo n.º 3
0
def test_add_lambda_override():
    """@TODO: Docs. Contribution is welcome."""
    r = Registry("")

    r.add(lambda x: x, name="bar")

    assert "bar" in r._factories
Ejemplo n.º 4
0
def test_add_function():
    """@TODO: Docs. Contribution is welcome."""
    r = Registry("")

    r.add(foo)

    assert "foo" in r._factories
Ejemplo n.º 5
0
def test_double_add_same_nofail():
    r = Registry("")
    r.add(foo)
    # It's ok to add same twice, forced by python relative import
    # implementation
    # https://github.com/catalyst-team/catalyst/issues/135
    r.add(foo)
Ejemplo n.º 6
0
def _samplers_loader(r: Registry):
    from torch.utils.data import sampler as s
    factories = {
        k: v
        for k, v in s.__dict__.items() if "Sampler" in k and k != "Sampler"
    }
    r.add(**factories)
    from catalyst.data import sampler
    r.add_from_module(sampler)
Ejemplo n.º 7
0
def test_fail_instantiation():
    r = Registry("")

    r.add(foo)

    with pytest.raises(RegistryException) as e_ifo:
        r.get_instance("foo", c=1)

    assert hasattr(e_ifo.value, "__cause__")
Ejemplo n.º 8
0
def test_from_config():
    r = Registry("obj")

    r.add(foo)

    res = r.get_from_params(**{"obj": "foo", "a": 1, "b": 2})
    assert res == {"a": 1, "b": 2}

    res = r.get_from_params(**{})
    assert res is None
Ejemplo n.º 9
0
def test_fail_instantiation():
    """@TODO: Docs. Contribution is welcome."""
    r = Registry("")

    r.add(foo)

    with pytest.raises(RegistryException) as e_ifo:
        r.get_instance("foo", c=1)

    assert hasattr(e_ifo.value, "__cause__")
Ejemplo n.º 10
0
def test_fail_double_add_different():
    r = Registry("")
    r.add(foo)

    with pytest.raises(RegistryException):

        def bar():
            pass

        r.add(foo=bar)
Ejemplo n.º 11
0
def test_fail_double_add_different():
    """@TODO: Docs. Contribution is welcome."""
    r = Registry("")
    r.add(foo)

    with pytest.raises(RegistryException):

        def bar():
            pass

        r.add(foo=bar)
Ejemplo n.º 12
0
def test_from_config():
    """@TODO: Docs. Contribution is welcome."""
    r = Registry("obj")

    r.add(foo)

    res = r.get_from_params(**{"obj": "foo", "a": 1, "b": 2})
    assert res == {"a": 1, "b": 2}

    res = r.get_from_params(**{})
    assert res is None
Ejemplo n.º 13
0
def test_instantiations():
    r = Registry("")

    r.add(foo)

    res = r.get_instance("foo", 1, 2)
    assert res == {"a": 1, "b": 2}

    res = r.get_instance("foo", 1, b=2)
    assert res == {"a": 1, "b": 2}

    res = r.get_instance("foo", a=1, b=2)
    assert res == {"a": 1, "b": 2}
Ejemplo n.º 14
0
def test_instantiations():
    """@TODO: Docs. Contribution is welcome."""
    r = Registry("")

    r.add(foo)

    res = r.get_instance("foo", 1, 2)
    assert res == {"a": 1, "b": 2}

    res = r.get_instance("foo", 1, b=2)
    assert res == {"a": 1, "b": 2}

    res = r.get_instance("foo", a=1, b=2)
    assert res == {"a": 1, "b": 2}
Ejemplo n.º 15
0
def test_meta_factory():
    def meta_1(fn, args, kwargs):
        return fn

    def meta_2(fn, args, kwargs):
        return 1

    r = Registry("obj", meta_1)
    r.add(foo)

    res = r.get_from_params(**{"obj": "foo"})
    assert res == foo

    res = r.get_from_params(**{"obj": "foo"}, meta_factory=meta_2)
    assert res == 1
Ejemplo n.º 16
0
def test_meta_factory():
    """@TODO: Docs. Contribution is welcome."""  # noqa: D202

    def meta_1(fn, args, kwargs):
        return fn

    def meta_2(fn, args, kwargs):
        return 1

    r = Registry("obj", meta_1)
    r.add(foo)

    res = r.get_from_params(**{"obj": "foo"})
    assert res == foo

    res = r.get_from_params(**{"obj": "foo"}, meta_factory=meta_2)
    assert res == 1
Ejemplo n.º 17
0
def test_add_lambda_override():
    r = Registry("")

    r.add(lambda x: x, name="bar")

    assert "bar" in r._factories
Ejemplo n.º 18
0
def test_add_function_name_override():
    r = Registry("")

    r.add(foo, name="bar")

    assert "bar" in r._factories
Ejemplo n.º 19
0
def test_add_function():
    r = Registry("")

    r.add(foo)

    assert "foo" in r._factories
Ejemplo n.º 20
0
def test_kwargs():
    r = Registry("")

    r.add(bar=foo)

    r.get("bar")
Ejemplo n.º 21
0
def test_fail_multiple_with_name():
    r = Registry("")

    with pytest.raises(RegistryException):
        r.add(foo, foo, name="bar")
Ejemplo n.º 22
0
def test_fail_multiple_with_name():
    """@TODO: Docs. Contribution is welcome."""
    r = Registry("")

    with pytest.raises(RegistryException):
        r.add(foo, foo, name="bar")
Ejemplo n.º 23
0
def test_add_lambda_fail():
    r = Registry("")

    with pytest.raises(RegistryException):
        r.add(lambda x: x)
Ejemplo n.º 24
0
def test_add_lambda_fail():
    """@TODO: Docs. Contribution is welcome."""
    r = Registry("")

    with pytest.raises(RegistryException):
        r.add(lambda x: x)