-
Notifications
You must be signed in to change notification settings - Fork 381
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add custom read only containers and use them for the config storage * Flake8, python2.7 compatibility and testcases for read-only containers
- Loading branch information
Showing
8 changed files
with
289 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
import pytest | ||
|
||
from sacred.config.custom_containers import make_read_only, ReadOnlyList, \ | ||
ReadOnlyDict | ||
from sacred.utils import SacredError | ||
|
||
|
||
def _check_read_only_dict(d): | ||
assert isinstance(d, ReadOnlyDict) | ||
|
||
raises_dict = pytest.raises( | ||
SacredError, match='This ReadOnlyDict is read-only!') | ||
|
||
if len(d) > 0: | ||
# Test removal of entries and overwrite an already present entry | ||
key = list(d.keys())[0] | ||
|
||
with raises_dict: | ||
d[key] = 42 | ||
|
||
with raises_dict: | ||
del d[key] | ||
|
||
with raises_dict: | ||
d.pop(key) | ||
|
||
# Test direct writes | ||
with raises_dict: | ||
d['abcdefg'] = 42 | ||
|
||
# Test other functions that modify the dict | ||
with raises_dict: | ||
d.clear() | ||
|
||
with raises_dict: | ||
d.update({'abcdefg': 42}) | ||
|
||
with raises_dict: | ||
d.popitem() | ||
|
||
with raises_dict: | ||
d.setdefault('a', 0) | ||
|
||
|
||
def _check_read_only_list(l): | ||
assert isinstance(l, ReadOnlyList) | ||
|
||
raises_list = pytest.raises( | ||
SacredError, match='This ReadOnlyList is read-only!') | ||
|
||
if len(l): | ||
with raises_list: | ||
del l[0] | ||
|
||
with raises_list: | ||
l[0] = 42 | ||
|
||
with raises_list: | ||
l.pop(0) | ||
|
||
with raises_list: | ||
l.pop() | ||
|
||
with raises_list: | ||
l.clear() | ||
|
||
with raises_list: | ||
l.append(42) | ||
|
||
with raises_list: | ||
l.extend([1, 2, 3, 4]) | ||
|
||
with raises_list: | ||
l.insert(0, 0) | ||
|
||
with raises_list: | ||
l.remove(1) | ||
|
||
with raises_list: | ||
l.sort() | ||
|
||
with raises_list: | ||
l.reverse() | ||
|
||
|
||
def test_readonly_dict(): | ||
d = dict(a=1, b=2, c=3) | ||
d = make_read_only(d) | ||
_check_read_only_dict(d) | ||
|
||
|
||
def test_nested_readonly_dict(): | ||
d = dict(a=1, b=dict(c=3)) | ||
d = make_read_only(d) | ||
_check_read_only_dict(d) | ||
_check_read_only_dict(d['b']) | ||
|
||
|
||
def test_readonly_list(): | ||
l = [1, 2, 3, 4] | ||
l = make_read_only(l) | ||
_check_read_only_list(l) | ||
|
||
|
||
def test_nested_readonly_list(): | ||
l = [1, [2, [3, [4]]]] | ||
l = make_read_only(l) | ||
_check_read_only_list(l) | ||
_check_read_only_list(l[1]) | ||
_check_read_only_list(l[1][1]) | ||
_check_read_only_list(l[1][1][1]) | ||
|
||
|
||
def test_nested_readonly_containers(): | ||
container = ([0, [], {}, ()], {0: (), 1: [], 2: {}}) | ||
container = make_read_only(container) | ||
_check_read_only_list(container[0]) | ||
_check_read_only_list(container[0][1]) | ||
_check_read_only_dict(container[0][2]) | ||
_check_read_only_dict(container[1]) | ||
_check_read_only_dict(container[1][2]) | ||
_check_read_only_list(container[1][1]) | ||
|
||
# Check explicitly for tuple (and not isinstance) to be sure that the type | ||
# is not altered | ||
assert type(container) == tuple | ||
assert type(container[0][3]) == tuple | ||
assert type(container[1][0]) == tuple |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters