Skip to content

Commit

Permalink
Added test cases and a fix for Issue #409. (#505)
Browse files Browse the repository at this point in the history
* Added test cases and a fix for issue-409. Recursively fill in the result with preset after setting in the conf

* Added a user level interface test case

* Removed reference to the issue
  • Loading branch information
wecacuee authored and JarnoRFB committed Jul 10, 2019
1 parent 5a29918 commit b3ed1f1
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 4 deletions.
7 changes: 4 additions & 3 deletions sacred/config/config_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
# coding=utf-8

from sacred.config.config_summary import ConfigSummary
from sacred.config.utils import dogmatize, normalize_or_die, undogmatize
from sacred.config.utils import (dogmatize, normalize_or_die, undogmatize,
recursive_fill_in)


class ConfigDict(object):
Expand All @@ -12,8 +13,8 @@ def __init__(self, d):

def __call__(self, fixed=None, preset=None, fallback=None):
result = dogmatize(fixed or {})
result.update(preset)
result.update(self._conf)
recursive_fill_in(result, self._conf)
recursive_fill_in(result, preset or {})
added = result.revelation()
config_summary = ConfigSummary(added, result.modified,
result.typechanges)
Expand Down
27 changes: 27 additions & 0 deletions tests/test_config/test_config_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,30 @@ def test_fixed_subentry_of_preset():
assert set(cfg['d'].keys()) == {'a', 'b'}
assert cfg['d']['a'] == 10
assert cfg['d']['b'] == 2


def test_add_config_dict_sequential():
# https://github.com/IDSIA/sacred/issues/409

adict = ConfigDict(dict(
dictnest2 = {
'key_1': 'value_1',
'key_2': 'value_2'
}))

bdict = ConfigDict(dict(
dictnest2 = {
'key_2': 'update_value_2',
'key_3': 'value3',
'key_4': 'value4'
}))

final_config = bdict(preset=adict())
assert final_config == {
'dictnest2': {
'key_1': 'value_1',
'key_2': 'update_value_2',
'key_3': 'value3',
'key_4': 'value4'
}
}
34 changes: 33 additions & 1 deletion tests/test_config/test_config_scope_chain.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@


import pytest
from sacred.config import ConfigScope, chain_evaluate_config_scopes
from sacred.config import ConfigScope, ConfigDict, chain_evaluate_config_scopes


def test_chained_config_scopes_contain_combined_keys():
Expand Down Expand Up @@ -143,3 +143,35 @@ def test_empty_chain_contains_preset_and_fixed():
assert set(final_cfg.keys()) == {'a', 'b'}
assert final_cfg['a'] == 0
assert final_cfg['b'] == 2


def test_add_config_dict_sequential():
# https://github.com/IDSIA/sacred/issues/409
@ConfigScope
def cfg1():
dictnest2 = {
'key_1': 'value_1',
'key_2': 'value_2'
}
cfg1dict = ConfigDict(cfg1())

@ConfigScope
def cfg2():
dictnest2 = {
'key_2': 'update_value_2',
'key_3': 'value3',
'key_4': 'value4'
}
cfg2dict = ConfigDict(cfg2())
final_config_scope, _ = chain_evaluate_config_scopes([cfg1, cfg2])
assert final_config_scope == {
'dictnest2': {
'key_1': 'value_1',
'key_2': 'update_value_2',
'key_3': 'value3',
'key_4': 'value4'
}
}

final_config_dict, _ = chain_evaluate_config_scopes([cfg1dict, cfg2dict])
assert final_config_dict == final_config_scope
48 changes: 48 additions & 0 deletions tests/test_experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,3 +367,51 @@ def main(_config, nested_dict, nested_list, nested_tuple):

ex.run()


def test_add_config_dict_chain(ex):

@ex.config
def config1():
"""This is my demo configuration"""
dictnest_cap = {
'key_1': 'value_1',
'key_2': 'value_2'
}


@ex.config
def config2():
"""This is my demo configuration"""
dictnest_cap = {
'key_2': 'update_value_2',
'key_3': 'value3',
'key_4': 'value4'
}


adict = {
'dictnest_dict': {
'key_1': 'value_1',
'key_2': 'value_2'
}
}
ex.add_config(adict)

bdict = {
'dictnest_dict': {
'key_2': 'update_value_2',
'key_3': 'value3',
'key_4': 'value4'
}
}
ex.add_config(bdict)

@ex.automain
def run():
pass

final_config = ex.run().config
assert final_config['dictnest_cap'] == {
'key_1': 'value_1', 'key_2': 'update_value_2',
'key_3': 'value3', 'key_4': 'value4'}
assert final_config['dictnest_cap'] == final_config['dictnest_dict']

0 comments on commit b3ed1f1

Please sign in to comment.