예제 #1
0
 def get_config_modifications(self):
     self.config_mods = ConfigSummary(added={
         key
         for key, value in iterate_flattened(self.config_updates)
     })
     for cfg_summary in self.summaries:
         self.config_mods.update_from(cfg_summary)
예제 #2
0
 def __call__(self, fixed=None, preset=None, fallback=None):
     result = dogmatize(fixed or {})
     recursive_fill_in(result, self._conf)
     recursive_fill_in(result, preset or {})
     added = result.revelation()
     config_summary = ConfigSummary(added, result.modified, result.typechanges)
     config_summary.update(undogmatize(result))
     return config_summary
예제 #3
0
def run():
    config = {'a': 17, 'foo': {'bar': True, 'baz': False}, 'seed': 1234}
    config_mod = ConfigSummary()
    main_func = mock.Mock(return_value=123)
    logger = mock.Mock()
    observer = [mock.Mock()]
    return Run(config, config_mod, main_func, observer, logger, logger, {}, {},
               [], [])
예제 #4
0
    def __call__(self, fixed=None, preset=None, fallback=None):
        """
        Evaluate this ConfigScope.

        This will evaluate the function body and fill the relevant local
        variables into entries into keys in this dictionary.

        :param fixed: Dictionary of entries that should stay fixed during the
                      evaluation. All of them will be part of the final config.
        :type fixed: dict
        :param preset: Dictionary of preset values that will be available
                       during the evaluation (if they are declared in the
                       function argument list). All of them will be part of the
                       final config.
        :type preset: dict
        :param fallback: Dictionary of fallback values that will be available
                         during the evaluation (if they are declared in the
                         function argument list). They will NOT be part of the
                         final config.
        :type fallback: dict
        :return: self
        :rtype: ConfigScope
        """
        cfg_locals = dogmatize(fixed or {})
        fallback = fallback or {}
        preset = preset or {}
        fallback_view = {}

        available_entries = set(preset.keys()) | set(fallback.keys())

        for arg in self.arg_spec.args:
            if arg not in available_entries:
                raise KeyError("'{}' not in preset for ConfigScope. "
                               "Available options are: {}".format(
                                   arg, available_entries))
            if arg in preset:
                cfg_locals[arg] = preset[arg]
            else:  # arg in fallback
                fallback_view[arg] = fallback[arg]

        cfg_locals.fallback = fallback_view
        eval(self._body_code, copy(self._func.__globals__), cfg_locals)

        added = cfg_locals.revelation()
        config_summary = ConfigSummary(added,
                                       cfg_locals.modified,
                                       cfg_locals.typechanges,
                                       cfg_locals.fallback_writes,
                                       docs=self._var_docs)
        # fill in the unused presets
        recursive_fill_in(cfg_locals, preset)

        for key, value in cfg_locals.items():
            try:
                config_summary[key] = normalize_or_die(value)
            except ValueError:
                pass
        return config_summary
예제 #5
0
def run():
    config = {'a': 17, 'foo': {'bar': True, 'baz': False}, 'seed': 1234}
    config_mod = ConfigSummary()
    signature = mock.Mock()
    signature.name = 'main_func'
    main_func = mock.Mock(return_value=123, prefix='', signature=signature)
    logger = mock.Mock()
    observer = [mock.Mock(priority=10)]
    return Run(config, config_mod, main_func, observer, logger, logger, {},
               {}, [], [])
예제 #6
0
def run():
    config = {"a": 17, "foo": {"bar": True, "baz": False}, "seed": 1234}
    config_mod = ConfigSummary()
    signature = mock.Mock()
    signature.name = "main_func"
    main_func = mock.Mock(return_value=123, prefix="", signature=signature)
    logger = mock.Mock()
    observer = [mock.Mock(priority=10)]
    return Run(config, config_mod, main_func, observer, logger, logger, {}, {},
               [], [])
예제 #7
0
def test_iterate_marked(cfg):
    assert list(_iterate_marked(cfg, ConfigSummary())) == \
        [('a', ConfigEntry('a', 0, False, False, None, None)),
         ('b', ConfigEntry('b', {}, False, False, None, None)),
         ('c', PathEntry('c', False, False, None, None)),
         ('c.cA', ConfigEntry('cA', 3, False, False, None, None)),
         ('c.cB', ConfigEntry('cB', 4, False, False, None, None)),
         ('c.cC', PathEntry('cC', False, False, None, None)),
         ('c.cC.cC1', ConfigEntry('cC1', 6, False, False, None, None)),
         ('d', PathEntry('d', False, False, None, None)),
         ('d.dA', ConfigEntry('dA', 8, False, False, None, None))
         ]
예제 #8
0
def test_iterate_marked(cfg):
    assert list(_iterate_marked(cfg, ConfigSummary())) == [
        ("a", ConfigEntry("a", 0, False, False, None, None)),
        ("b", ConfigEntry("b", {}, False, False, None, None)),
        ("c", PathEntry("c", False, False, None, None)),
        ("c.cA", ConfigEntry("cA", 3, False, False, None, None)),
        ("c.cB", ConfigEntry("cB", 4, False, False, None, None)),
        ("c.cC", PathEntry("cC", False, False, None, None)),
        ("c.cC.cC1", ConfigEntry("cC1", 6, False, False, None, None)),
        ("d", PathEntry("d", False, False, None, None)),
        ("d.dA", ConfigEntry("dA", 8, False, False, None, None)),
    ]
예제 #9
0
def test_iterate_marked_updated(cfg):
    modified = {'b', 'c', 'c.cC.cC1'}
    assert list(_iterate_marked(cfg, ConfigSummary(modified=modified))) == \
        [('a', ConfigEntry('a', 0, False, False, None, None)),
         ('b', ConfigEntry('b', {}, False, True, None, None)),
         ('c', PathEntry('c', False, True, None, None)),
         ('c.cA', ConfigEntry('cA', 3, False, False, None, None)),
         ('c.cB', ConfigEntry('cB', 4, False, False, None, None)),
         ('c.cC', PathEntry('cC', False, True, None, None)),
         ('c.cC.cC1', ConfigEntry('cC1', 6, False, True, None, None)),
         ('d', PathEntry('d', False, False, None, None)),
         ('d.dA', ConfigEntry('dA', 8, False, False, None, None))
         ]
예제 #10
0
def test_format_config(cfg):
    cfg_text = _format_config(cfg, ConfigSummary())
    lines = cfg_text.split('\n')
    assert lines[0].startswith('Configuration')
    assert ' a = 0' in lines[1]
    assert ' b = {}' in lines[2]
    assert ' c:' in lines[3]
    assert ' cA = 3' in lines[4]
    assert ' cB = 4' in lines[5]
    assert ' cC:' in lines[6]
    assert ' cC1 = 6' in lines[7]
    assert ' d:' in lines[8]
    assert ' dA = 8' in lines[9]
예제 #11
0
def test_format_config(cfg):
    cfg_text = _format_config(cfg, ConfigSummary())
    lines = cfg_text.split('\n')
    assert lines[0].startswith('Configuration')
    assert lines[1].find(' a = 0') > -1
    assert lines[2].find(' b = {}') > -1
    assert lines[3].find(' c:') > -1
    assert lines[4].find(' cA = 3') > -1
    assert lines[5].find(' cB = 4') > -1
    assert lines[6].find(' cC:') > -1
    assert lines[7].find(' cC1 = 6') > -1
    assert lines[8].find(' d:') > -1
    assert lines[9].find(' dA = 8') > -1
예제 #12
0
def test_iterate_marked_added(cfg):
    added = {'a', 'c.cB', 'c.cC.cC1'}
    assert list(iterate_marked(cfg, ConfigSummary(added=added))) == \
        [('a', ConfigEntry('a', 0, True, False, None)),
         ('b', ConfigEntry('b', {}, False, False, None)),
         ('c', PathEntry('c', False, True, None)),
         ('c.cA', ConfigEntry('cA', 3, False, False, None)),
         ('c.cB', ConfigEntry('cB', 4, True, False, None)),
         ('c.cC', PathEntry('cC', False, True, None)),
         ('c.cC.cC1', ConfigEntry('cC1', 6, True, False, None)),
         ('d', PathEntry('d', False, False, None)),
         ('d.dA', ConfigEntry('dA', 8, False, False, None))
         ]
예제 #13
0
def test_iterate_marked_updated(cfg):
    modified = {"b", "c", "c.cC.cC1"}
    assert list(_iterate_marked(cfg, ConfigSummary(modified=modified))) == [
        ("a", ConfigEntry("a", 0, False, False, None, None)),
        ("b", ConfigEntry("b", {}, False, True, None, None)),
        ("c", PathEntry("c", False, True, None, None)),
        ("c.cA", ConfigEntry("cA", 3, False, False, None, None)),
        ("c.cB", ConfigEntry("cB", 4, False, False, None, None)),
        ("c.cC", PathEntry("cC", False, True, None, None)),
        ("c.cC.cC1", ConfigEntry("cC1", 6, False, True, None, None)),
        ("d", PathEntry("d", False, False, None, None)),
        ("d.dA", ConfigEntry("dA", 8, False, False, None, None)),
    ]
예제 #14
0
def test_format_config(cfg):
    cfg_text = _format_config(cfg, ConfigSummary())
    lines = cfg_text.split("\n")
    assert lines[0].startswith("Configuration")
    assert " a = 0" in lines[1]
    assert " b = {}" in lines[2]
    assert " c:" in lines[3]
    assert " cA = 3" in lines[4]
    assert " cB = 4" in lines[5]
    assert " cC:" in lines[6]
    assert " cC1 = 6" in lines[7]
    assert " d:" in lines[8]
    assert " dA = 8" in lines[9]
예제 #15
0
def test_iterate_marked_typechanged(cfg):
    typechanged = {'a': (bool, int), 'd.dA': (float, int)}
    result = list(_iterate_marked(cfg, ConfigSummary(typechanged=typechanged)))
    assert result == \
        [('a', ConfigEntry('a', 0, False, False, (bool, int), None)),
         ('b', ConfigEntry('b', {}, False, False, None, None)),
         ('c', PathEntry('c', False, False, None, None)),
         ('c.cA', ConfigEntry('cA', 3, False, False, None, None)),
         ('c.cB', ConfigEntry('cB', 4, False, False, None, None)),
         ('c.cC', PathEntry('cC', False, False, None, None)),
         ('c.cC.cC1', ConfigEntry('cC1', 6, False, False, None, None)),
         ('d', PathEntry('d', False, True, None, None)),
         ('d.dA', ConfigEntry('dA', 8, False, False, (float, int), None))
         ]
예제 #16
0
def test_iterate_marked_typechanged(cfg):
    typechanged = {"a": (bool, int), "d.dA": (float, int)}
    result = list(_iterate_marked(cfg, ConfigSummary(typechanged=typechanged)))
    assert result == [
        ("a", ConfigEntry("a", 0, False, False, (bool, int), None)),
        ("b", ConfigEntry("b", {}, False, False, None, None)),
        ("c", PathEntry("c", False, False, None, None)),
        ("c.cA", ConfigEntry("cA", 3, False, False, None, None)),
        ("c.cB", ConfigEntry("cB", 4, False, False, None, None)),
        ("c.cC", PathEntry("cC", False, False, None, None)),
        ("c.cC.cC1", ConfigEntry("cC1", 6, False, False, None, None)),
        ("d", PathEntry("d", False, True, None, None)),
        ("d.dA", ConfigEntry("dA", 8, False, False, (float, int), None)),
    ]
예제 #17
0
파일: test_run.py 프로젝트: J3rome/sacred
def run():
    config = {"a": 17, "foo": {"bar": True, "baz": False}, "seed": 1234}
    config_mod = ConfigSummary()
    signature = mock.Mock()
    signature.name = "main_func"

    def side_effect(*args):
        # TODO : Type checking ? Does mock have a function for this ?
        for arg in args:
            arg = arg + 10

        return args

    main_func = mock.Mock(return_value=123,
                          prefix="",
                          signature=signature,
                          side_effect=side_effect)
    logger = mock.Mock()
    observer = [mock.Mock(priority=10)]
    return Run(config, config_mod, main_func, observer, logger, logger, {}, {},
               [], [])
예제 #18
0
def get_config_modifications(scaffolding, config_updates):
    config_modifications = ConfigSummary()
    for sc_path, scaffold in scaffolding.items():
        config_modifications.update_add(scaffold.config_mods, path=sc_path)
    return config_modifications
예제 #19
0
 def get_config_modifications(self):
     self.config_mods = ConfigSummary()
     for cfg_summary in self.summaries:
         self.config_mods.update_from(cfg_summary)