Skip to content

Commit

Permalink
__copy__ configuration directive now accepts a __default__
Browse files Browse the repository at this point in the history
  • Loading branch information
dnouri committed Jun 21, 2019
1 parent 2549869 commit cb798f6
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
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

0 comments on commit cb798f6

Please sign in to comment.