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

__copy__ configuration directive now accepts a __default__ #109

Merged
merged 1 commit into from
Jun 23, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions docs/user/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,15 @@ With ``__copy__``, you can reduce this down to:
}

Reducing duplication in your configuration can help avoid errors.

If the target of the ``__copy__`` directive does not exist, we can
fall back to a default using the ``__default__`` special keyword. An
example that defaults to an empty ``param_grid`` for cross
validation:

.. code-block:: python

'grid_search': {
'param_grid': {'__copy__': 'param_grid', '__default__': {}},
# ... some involved grid search configuration
}
14 changes: 12 additions & 2 deletions palladium/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,20 @@ def __call__(self, name, props):
if self_reference:
value = self._resolve(self.configs[:-1], dotted_path)
else:
value = self._resolve(self.configs, dotted_path)
try:
value = self._resolve(self.configs, dotted_path)
except KeyError:
if '__default__' in props:
return props['__default__']
else:
raise

value = deepcopy(value)
if len(props) > 1:
nonmagicprops = [
prop for prop in props
if not (prop.startswith('__') and prop.endswith('__'))
]
if nonmagicprops:
recursive_copy = self.key in value
value.update(props)
if not recursive_copy:
Expand Down
6 changes: 6 additions & 0 deletions palladium/tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,11 @@ def config2(self):
},
'mycopiedconstant': {
'__copy__': 'mycopiedconstant',
'__default__': 42,
},
'mycopywithdefault': {
'__copy__': 'nonexistant',
'__default__': 42,
},
}

Expand Down Expand Up @@ -301,6 +306,7 @@ def test_config1_and_2(self, process_config, config1, config2):
config['mysupernewdict']['mycopiedcomponent'], MyDummyComponent)

assert config['mycopiedconstant'] == 3
assert config['mycopywithdefault'] == 42

def test_initialize_config_logging(self, process_config):
with patch('palladium.config.dictConfig') as dictConfig:
Expand Down