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

Removed FallbackDict implementation. #552

Merged
merged 2 commits into from
Aug 1, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions sacred/config/captured_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down
70 changes: 4 additions & 66 deletions sacred/config/custom_containers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(kwargs)
return fallback_copy


class DogmaticDict(dict):
Expand Down
20 changes: 2 additions & 18 deletions tests/test_config/test_fallback_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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)