Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support passing variable number of arguments to resolvers. #46

Merged
merged 1 commit into from
Oct 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions docs/source/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,26 @@ This example creates a resolver that adds 10 the the given value.
1000


Custom resolvers support variadic argument lists in the form of a comma separated list of zero or more values (coming in OmegaConf 1.3.1).
Whitespaces are stripped from both ends of each value ("foo,bar" is the same as "foo, bar ").
You can use literal commas and spaces anywhere by escaping (:code:`\,` and :code:`\ `).
.. doctest::

>>> OmegaConf.register_resolver("concat", lambda x,y: x+y)
>>> c = OmegaConf.create({
... 'key1': '${concat:Hello,World}',
... 'key_trimmed': '${concat:Hello , World}',
... 'escape_whitespace': '${concat:Hello,\ World}',
... })
>>> c.key1
'HelloWorld'
>>> c.key_trimmed
'HelloWorld'
>>> c.escape_whitespace
'Hello World'



Merging configurations
----------------------
Merging configurations enables the creation of reusable configuration files for each logical component
Expand Down
4 changes: 3 additions & 1 deletion omegaconf/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,9 @@ def _resolve_value(root_node, inter_type, inter_key):
return ret

def _resolve_single(self, value):
match_list = list(re.finditer(r"\${(\w+:)?([\w\.%_-]+?)}", value))
key_prefix = r"\${(\w+:)?"
legal_characters = r"([\w\.%_ \\,-]*?)}"
match_list = list(re.finditer(key_prefix+legal_characters, value))
if len(match_list) == 0:
return value

Expand Down
16 changes: 15 additions & 1 deletion omegaconf/omegaconf.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import copy
import io
import os
import re
import sys
import warnings
from contextlib import contextmanager
Expand Down Expand Up @@ -137,6 +138,19 @@ def merge(*others):
target.merge_with(*others[1:])
return target


@staticmethod
def _tokenize_args(string):
if string is None or string == '':
return []
def _unescape_word_boundary(match):
if match.start() == 0 or match.end() == len(match.string):
return ''
return match.group(0)
escaped = re.split(r'(?<!\\),', string)
escaped = [re.sub(r'(?<!\\) ', _unescape_word_boundary, x) for x in escaped]
return [re.sub(r'(\\([ ,]))', lambda x: x.group(2), x) for x in escaped]

@staticmethod
def register_resolver(name, resolver):
assert callable(resolver), "resolver must be callable"
Expand All @@ -145,7 +159,7 @@ def register_resolver(name, resolver):

def caching(config, key):
cache = OmegaConf.get_cache(config)[name]
val = cache[key] if key in cache else resolver(key)
val = cache[key] if key in cache else resolver(*OmegaConf._tokenize_args(key))
cache[key] = val
return val

Expand Down
17 changes: 17 additions & 0 deletions tests/test_base_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,23 @@ def test_read_write_override(src, func, expectation):
func(c)


@pytest.mark.parametrize('string, tokenized', [
('dog,cat', ['dog', 'cat']),
('dog\,cat\ ', ['dog,cat ']),
('dog,\ cat', ['dog', ' cat']),
('\ ,cat', [' ', 'cat']),
('dog, cat', ['dog', 'cat']),
('dog, ca t', ['dog', 'ca t']),
('dog, cat', ['dog', 'cat']),
('whitespace\ , before comma', ['whitespace ', 'before comma']),
(None, []),
('', []),
('no , escape', ['no', 'escape']),
])
def test_tokenize_with_escapes(string, tokenized):
assert OmegaConf._tokenize_args(string) == tokenized


@pytest.mark.parametrize('src, func, expectation', [
({}, lambda c: c.__setattr__('foo', 1), pytest.raises(KeyError)),
])
Expand Down
17 changes: 16 additions & 1 deletion tests/test_interpolation.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ def test_resolver_cache_1():

def test_resolver_cache_2():
"""
Tests that resolver cache is not shared between different OmegaConf objects
Tests that resolver cache is not shared between different OmegaConf objects
"""
try:
OmegaConf.register_resolver("random", lambda _: random.randint(0, 10000000))
Expand All @@ -250,6 +250,21 @@ def test_resolver_cache_2():
OmegaConf.clear_resolvers()


@pytest.mark.parametrize('resolver,name,key,result', [
(lambda *args: args, 'arg_list', "${my_resolver:cat, dog}",("cat", "dog")),
(lambda *args: args, 'escape_comma',"${my_resolver:cat\, do g}",("cat, do g",)),
(lambda *args: args, 'escape_whitespace',"${my_resolver:cat\, do g}",("cat, do g",)),
(lambda: 'zero', 'zero_arg',"${my_resolver:}",("zero")),
])
Comment on lines +253 to +258
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not familiar with this syntex. Why do you need it here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what you mean? Would you prefer I passed in entire config dictionaries as opposed to pairs of key, string_to_resolve?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, I was confused by the lambda being passed there, I get why you need this now.

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()


def test_copy_cache():
OmegaConf.register_resolver("random", lambda _: random.randint(0, 10000000))
c1 = OmegaConf.create(dict(
Expand Down