From 9802030daf4afdccba87dec4a171bb42792c05bd Mon Sep 17 00:00:00 2001 From: gabrieldemarmiesse Date: Thu, 1 Aug 2019 14:32:34 +0200 Subject: [PATCH 1/2] Removed FallbackDict implementation. --- sacred/config/captured_function.py | 4 +- sacred/config/custom_containers.py | 70 ++----------------------- tests/test_config/test_fallback_dict.py | 20 +------ 3 files changed, 8 insertions(+), 86 deletions(-) diff --git a/sacred/config/captured_function.py b/sacred/config/captured_function.py index aa2b1034..b60cd4dd 100644 --- a/sacred/config/captured_function.py +++ b/sacred/config/captured_function.py @@ -5,7 +5,7 @@ from datetime import timedelta import wrapt -from sacred.config.custom_containers import FallbackDict +from sacred.config.custom_containers import fallback_dict from sacred.config.signature import Signature from sacred.randomness import create_rnd, get_seed from sacred.utils import ConfigError @@ -26,7 +26,7 @@ def create_captured_function(function, prefix=None): @wrapt.decorator def captured_function(wrapped, instance, args, kwargs): - options = FallbackDict( + options = fallback_dict( wrapped.config, _config=wrapped.config, _log=wrapped.logger, diff --git a/sacred/config/custom_containers.py b/sacred/config/custom_containers.py index 0ac17184..1bf42637 100644 --- a/sacred/config/custom_containers.py +++ b/sacred/config/custom_containers.py @@ -6,72 +6,10 @@ from sacred.utils import join_paths, SacredError -class FallbackDict(dict): - """Dictionary that defaults to a fallback dict for missing keys.""" - - def __init__(self, fallback, **kwargs): - super(FallbackDict, self).__init__(**kwargs) - self.fallback = fallback - - def __getitem__(self, item): - if dict.__contains__(self, item): - return dict.__getitem__(self, item) - else: - return self.fallback[item] - - def __contains__(self, item): - return dict.__contains__(self, item) or (item in self.fallback) - - def get(self, k, d=None): - if dict.__contains__(self, k): - return dict.__getitem__(self, k) - else: - return self.fallback.get(k, d) - - def items(self): - raise NotImplementedError() - - def iteritems(self): - raise NotImplementedError() - - def iterkeys(self): - raise NotImplementedError() - - def itervalues(self): - raise NotImplementedError() - - def keys(self): - raise NotImplementedError() - - def pop(self, k, d=None): - raise NotImplementedError() - - def popitem(self): - raise NotImplementedError() - - def setdefault(self, k, d=None): - raise NotImplementedError() - - def update(self, e=None, **f): - raise NotImplementedError() - - def values(self): - raise NotImplementedError() - - def viewitems(self): - raise NotImplementedError() - - def viewkeys(self): - raise NotImplementedError() - - def viewvalues(self): - raise NotImplementedError() - - def __iter__(self): - raise NotImplementedError() - - def __len__(self): - raise NotImplementedError() +def fallback_dict(fallback, **kwargs): + fallback_copy = fallback.copy() + fallback_copy.update(dict(**kwargs)) + return fallback_copy class DogmaticDict(dict): diff --git a/tests/test_config/test_fallback_dict.py b/tests/test_config/test_fallback_dict.py index b6a040f9..a51536be 100644 --- a/tests/test_config/test_fallback_dict.py +++ b/tests/test_config/test_fallback_dict.py @@ -2,12 +2,12 @@ # coding=utf-8 import pytest -from sacred.config.custom_containers import FallbackDict +from sacred.config.custom_containers import fallback_dict @pytest.fixture def fbdict(): - return FallbackDict({'fall1': 7, 'fall3': True}) + return fallback_dict({'fall1': 7, 'fall3': True}) def test_is_dictionary(fbdict): @@ -34,19 +34,3 @@ def test_get(fbdict): assert fbdict.get('fall1', 18) == 7 assert fbdict.get('notexisting', 18) == 18 assert fbdict.get('fall3', 18) is True - - -@pytest.mark.parametrize('method', - ['items', 'iteritems', 'iterkeys', 'itervalues', - 'keys', 'popitem', 'update', 'values', 'viewitems', - 'viewkeys', 'viewvalues', '__iter__', '__len__']) -def test_not_implemented(method, fbdict): - with pytest.raises(NotImplementedError): - getattr(fbdict, method)() - - -def test_special_not_implemented(fbdict): - with pytest.raises(NotImplementedError): - fbdict.pop('fall1') - with pytest.raises(NotImplementedError): - fbdict.setdefault('fall2', None) From ec3cd3239df10ed9dda749be612792a1382abd70 Mon Sep 17 00:00:00 2001 From: gabrieldemarmiesse Date: Thu, 1 Aug 2019 14:33:46 +0200 Subject: [PATCH 2/2] Small fix. --- sacred/config/custom_containers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sacred/config/custom_containers.py b/sacred/config/custom_containers.py index 1bf42637..5af5a91f 100644 --- a/sacred/config/custom_containers.py +++ b/sacred/config/custom_containers.py @@ -8,7 +8,7 @@ def fallback_dict(fallback, **kwargs): fallback_copy = fallback.copy() - fallback_copy.update(dict(**kwargs)) + fallback_copy.update(kwargs) return fallback_copy