Exemplo n.º 1
0
 def _register_resolvers(self):
     OmegaConf.clear_resolvers()
     # Device count resolver
     device_count = max(1, torch.cuda.device_count())
     OmegaConf.register_resolver("device_count", lambda: device_count)
     OmegaConf.register_resolver("resolve_cache_dir", resolve_cache_dir)
     OmegaConf.register_resolver("resolve_dir", resolve_dir)
Exemplo n.º 2
0
def test_resolver_that_allows_a_list_of_arguments(resolver, name, key, result):
    try:
        OmegaConf.register_resolver('my_resolver', resolver)
        c = OmegaConf.create({name: key})
        assert c[name] == result
    finally:
        OmegaConf.clear_resolvers()
Exemplo n.º 3
0
def test_clear_resolvers_and_has_resolver_legacy(
        restore_resolvers: Any) -> None:
    assert not OmegaConf.has_resolver("foo")
    OmegaConf.legacy_register_resolver("foo", lambda x: int(x) + 10)
    assert OmegaConf.has_resolver("foo")
    OmegaConf.clear_resolvers()
    assert not OmegaConf.has_resolver("foo")
Exemplo n.º 4
0
def test_register_resolver_twice_error():
    try:
        OmegaConf.register_resolver("foo", lambda: 10)
        with pytest.raises(AssertionError):
            OmegaConf.register_resolver("foo", lambda: 10)
    finally:
        OmegaConf.clear_resolvers()
Exemplo n.º 5
0
def test_clear_resolvers():
    assert OmegaConf.get_resolver('foo') is None
    try:
        OmegaConf.register_resolver('foo', lambda x: int(x) + 10)
        assert OmegaConf.get_resolver('foo') is not None
    finally:
        OmegaConf.clear_resolvers()
        assert OmegaConf.get_resolver('foo') is None
Exemplo n.º 6
0
def test_values_from_env_come_parsed(value, expected):
    try:
        os.environ["my_key"] = value
        c = OmegaConf.create(dict(my_key="${env:my_key}", ))
        assert c.my_key == expected
    finally:
        del os.environ["my_key"]
        OmegaConf.clear_resolvers()
Exemplo n.º 7
0
def test_env_interpolation1():
    try:
        os.environ['foobar'] = '1234'
        c = OmegaConf.create(dict(path='/test/${env:foobar}', ))
        assert c.path == '/test/1234'
    finally:
        del os.environ['foobar']
        OmegaConf.clear_resolvers()
Exemplo n.º 8
0
def test_env_values_are_typed(value: Any, expected: Any) -> None:
    try:
        os.environ["my_key"] = value
        c = OmegaConf.create(dict(my_key="${env:my_key}"))
        assert c.my_key == expected
    finally:
        del os.environ["my_key"]
        OmegaConf.clear_resolvers()
Exemplo n.º 9
0
def test_env_interpolation1() -> None:
    try:
        os.environ["foobar"] = "1234"
        c = OmegaConf.create(dict(path="/test/${env:foobar}"))
        assert c.path == "/test/1234"
    finally:
        del os.environ["foobar"]
        OmegaConf.clear_resolvers()
Exemplo n.º 10
0
def test_register_resolver_1():
    try:
        OmegaConf.register_resolver("plus_10", lambda x: int(x) + 10)
        c = OmegaConf.create(dict(k='${plus_10:990}', ))

        assert type(c.k) == int
        assert c.k == 1000
    finally:
        OmegaConf.clear_resolvers()
Exemplo n.º 11
0
def test_resolver_that_allows_a_list_of_arguments(resolver: Resolver,
                                                  name: str, key: str,
                                                  result: Any) -> None:
    try:
        OmegaConf.register_resolver("my_resolver", resolver)
        c = OmegaConf.create({name: key})
        assert isinstance(c, DictConfig)
        assert c[name] == result
    finally:
        OmegaConf.clear_resolvers()
Exemplo n.º 12
0
def test_register_resolver_twice_error() -> None:
    try:

        def foo() -> int:
            return 10

        OmegaConf.register_resolver("foo", foo)
        with pytest.raises(AssertionError):
            OmegaConf.register_resolver("foo", lambda: 10)
    finally:
        OmegaConf.clear_resolvers()
Exemplo n.º 13
0
def test_resolver_cache_1():
    # resolvers are always converted to stateless idempotent functions
    # subsequent calls to the same function with the same argument will always return the same value.
    # this is important to allow embedding of functions like time() without having the value change during
    # the program execution.
    try:
        OmegaConf.register_resolver("random",
                                    lambda _: random.randint(0, 10000000))
        c = OmegaConf.create(dict(k='${random:_}', ))
        assert c.k == c.k
    finally:
        OmegaConf.clear_resolvers()
Exemplo n.º 14
0
def test_resolver_cache_2():
    """
    Tests that resolver cache is not shared between different OmegaConf objects
    """
    try:
        OmegaConf.register_resolver("random",
                                    lambda _: random.randint(0, 10000000))
        c1 = OmegaConf.create(dict(k='${random:_}', ))
        c2 = OmegaConf.create(dict(k='${random:_}', ))
        assert c1.k != c2.k
        assert c1.k == c1.k
        assert c2.k == c2.k
    finally:
        OmegaConf.clear_resolvers()
Exemplo n.º 15
0
def test_clear_resolvers(restore_resolvers: Any) -> None:
    assert OmegaConf.get_resolver("foo") is None
    OmegaConf.register_resolver("foo", lambda x: int(x) + 10)
    assert OmegaConf.get_resolver("foo") is not None
    OmegaConf.clear_resolvers()
    assert OmegaConf.get_resolver("foo") is None
Exemplo n.º 16
0
def test_clear_resolvers_and_has_resolver(restore_resolvers: Any) -> None:
    assert not OmegaConf.has_resolver("foo")
    OmegaConf.register_new_resolver("foo", lambda x: x + 10)
    assert OmegaConf.has_resolver("foo")
    OmegaConf.clear_resolvers()
    assert not OmegaConf.has_resolver("foo")