Skip to content

Commit

Permalink
Merge pull request #6 from johnomotani/check-all-any
Browse files Browse the repository at this point in the history
Replace 'checks' with 'check_all' and add 'check_any'
  • Loading branch information
johnomotani authored May 3, 2020
2 parents c40ed88 + ab7a0cb commit 036a848
Show file tree
Hide file tree
Showing 7 changed files with 189 additions and 68 deletions.
18 changes: 15 additions & 3 deletions optionsfactory/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,27 @@ def _checked(value, *, meta=None, name=None):
f"{'' if name is None else ' for key=' + str(name)}"
)

if meta.checks is not None:
for check in meta.checks:
if meta.check_all is not None:
for check in meta.check_all:
if not check(value):
raise ValueError(
f"The value {value}"
f"{'' if name is None else ' of key=' + str(name)} is not "
f"compatible with the checks"
f"compatible with check_all"
)

if meta.check_any is not None:
success = False
for check in meta.check_any:
if check(value):
success = True
if not success:
raise ValueError(
f"The value {value}"
f"{'' if name is None else ' of key=' + str(name)} is not "
f"compatible with check_any"
)

return value


Expand Down
14 changes: 12 additions & 2 deletions optionsfactory/checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@


def is_positive(x):
return x > 0
try:
return x > 0
except TypeError:
return False


def is_positive_or_None(x):
Expand All @@ -13,7 +16,10 @@ def is_positive_or_None(x):


def is_non_negative(x):
return x >= 0
try:
return x >= 0
except TypeError:
return False


def is_non_negative_or_None(x):
Expand All @@ -22,4 +28,8 @@ def is_non_negative_or_None(x):
return is_non_negative(x)


def is_None(x):
return x is None


NoneType = type(None)
15 changes: 9 additions & 6 deletions optionsfactory/tests/test_check_utilities.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import pytest

from ..checks import (
is_positive,
is_positive_or_None,
is_non_negative,
is_non_negative_or_None,
is_None,
)


Expand All @@ -13,8 +12,7 @@ def test_is_positive(self):
assert is_positive(1)
assert not is_positive(-1)
assert not is_positive(0.0)
with pytest.raises(TypeError):
is_positive(None)
assert not is_positive(None)

def test_is_positive_or_None(self):
assert is_positive_or_None(1)
Expand All @@ -26,11 +24,16 @@ def test_is_non_negative(self):
assert is_non_negative(1)
assert not is_non_negative(-1)
assert is_non_negative(0.0)
with pytest.raises(TypeError):
is_non_negative(None)
assert not is_non_negative(None)

def test_is_non_negative_or_None(self):
assert is_non_negative_or_None(1)
assert not is_non_negative_or_None(-1)
assert is_non_negative_or_None(0.0)
assert is_non_negative_or_None(None)

def test_is_None(self):
assert is_None(None)
assert not is_None(3.0)
assert not is_None(-1)
assert not is_None("foo")
38 changes: 17 additions & 21 deletions optionsfactory/tests/test_mutableoptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ def test_defaults(self):
11,
doc="option g",
value_type=int,
checks=[is_positive, lambda x: x < 20],
check_all=[is_positive, lambda x: x < 20],
),
h=WithMeta(
lambda options: options.a + 2,
doc="option h",
value_type=int,
checks=[is_positive, lambda x: x < 20],
check_any=[is_positive, lambda x: x < -20],
),
)

Expand Down Expand Up @@ -192,13 +192,13 @@ def test_initialise(self):
11,
doc="option g",
value_type=int,
checks=[is_positive, lambda x: x < 20],
check_all=[is_positive, lambda x: x < 20],
),
h=WithMeta(
lambda options: options.a + 2,
doc="option h",
value_type=int,
checks=[is_positive, lambda x: x < 20],
check_any=[is_positive, lambda x: x < -20],
),
)

Expand Down Expand Up @@ -323,16 +323,14 @@ def test_initialise(self):
opts = factory.create({"g": 3.5})
with pytest.raises(ValueError):
opts = factory.create({"h": -7})
with pytest.raises(ValueError):
opts = factory.create({"h": 21})
assert factory.create({"h": -21}).h == -21
with pytest.raises(TypeError):
opts = factory.create({"h": 3.5})
with pytest.raises(ValueError):
opts = factory.create({"a": -7})
opts.h
with pytest.raises(ValueError):
opts = factory.create({"a": 21})
opts.h
opts = factory.create({"a": -23})
assert opts.h == -21
with pytest.raises(TypeError):
opts = factory.create({"a": 3.5})
opts.h
Expand All @@ -349,13 +347,13 @@ def test_initialise_from_options(self):
11,
doc="option g",
value_type=int,
checks=[is_positive, lambda x: x < 20],
check_all=[is_positive, lambda x: x < 20],
),
h=WithMeta(
lambda options: options.a + 2,
doc="option h",
value_type=int,
checks=[is_positive, lambda x: x < 20],
check_any=[is_positive, lambda x: x < -20],
),
)

Expand Down Expand Up @@ -853,13 +851,13 @@ def test_defaults(self):
11,
doc="option g",
value_type=int,
checks=[is_positive, lambda x: x < 20],
check_all=[is_positive, lambda x: x < 20],
),
h=WithMeta(
lambda options: options.a + 2,
doc="option h",
value_type=int,
checks=[is_positive, lambda x: x < 20],
check_any=[is_positive, lambda x: x < -20],
),
)

Expand Down Expand Up @@ -950,13 +948,13 @@ def test_initialise(self):
11,
doc="option g",
value_type=int,
checks=[is_positive, lambda x: x < 20],
check_all=[is_positive, lambda x: x < 20],
),
h=WithMeta(
lambda options: options.a + 2,
doc="option h",
value_type=int,
checks=[is_positive, lambda x: x < 20],
check_any=[is_positive, lambda x: x < -20],
),
)

Expand Down Expand Up @@ -1056,14 +1054,12 @@ def test_initialise(self):
opts = factory.create_immutable({"g": 3.5})
with pytest.raises(ValueError):
opts = factory.create_immutable({"h": -7})
with pytest.raises(ValueError):
opts = factory.create_immutable({"h": 21})
assert factory.create_immutable({"h": -21}).h == -21
with pytest.raises(TypeError):
opts = factory.create_immutable({"h": 3.5})
with pytest.raises(ValueError):
opts = factory.create_immutable({"a": -7})
with pytest.raises(ValueError):
opts = factory.create_immutable({"a": 21})
assert factory.create_immutable({"a": -23}).h == -21
with pytest.raises(TypeError):
opts = factory.create_immutable({"a": 3.5})

Expand All @@ -1079,13 +1075,13 @@ def test_initialise_from_options(self):
11,
doc="option g",
value_type=int,
checks=[is_positive, lambda x: x < 20],
check_all=[is_positive, lambda x: x < 20],
),
h=WithMeta(
lambda options: options.a + 2,
doc="option h",
value_type=int,
checks=[is_positive, lambda x: x < 20],
check_any=[is_positive, lambda x: x < -20],
),
)

Expand Down
18 changes: 8 additions & 10 deletions optionsfactory/tests/test_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ def test_defaults(self):
11,
doc="option g",
value_type=int,
checks=[is_positive, lambda x: x < 20],
check_all=[is_positive, lambda x: x < 20],
),
h=WithMeta(
lambda options: options.a + 2,
doc="option h",
value_type=int,
checks=[is_positive, lambda x: x < 20],
check_any=[is_positive, lambda x: x < -20],
),
)

Expand Down Expand Up @@ -147,13 +147,13 @@ def test_initialise(self):
11,
doc="option g",
value_type=int,
checks=[is_positive, lambda x: x < 20],
check_all=[is_positive, lambda x: x < 20],
),
h=WithMeta(
lambda options: options.a + 2,
doc="option h",
value_type=int,
checks=[is_positive, lambda x: x < 20],
check_any=[is_positive, lambda x: x < -20],
),
)

Expand Down Expand Up @@ -253,14 +253,12 @@ def test_initialise(self):
opts = factory.create({"g": 3.5})
with pytest.raises(ValueError):
opts = factory.create({"h": -7})
with pytest.raises(ValueError):
opts = factory.create({"h": 21})
assert factory.create({"h": -21}).h == -21
with pytest.raises(TypeError):
opts = factory.create({"h": 3.5})
with pytest.raises(ValueError):
opts = factory.create({"a": -7})
with pytest.raises(ValueError):
opts = factory.create({"a": 21})
assert factory.create({"a": -23}).h == -21
with pytest.raises(TypeError):
opts = factory.create({"a": 3.5})

Expand All @@ -276,13 +274,13 @@ def test_initialise_from_options(self):
11,
doc="option g",
value_type=int,
checks=[is_positive, lambda x: x < 20],
check_all=[is_positive, lambda x: x < 20],
),
h=WithMeta(
lambda options: options.a + 2,
doc="option h",
value_type=int,
checks=[is_positive, lambda x: x < 20],
check_any=[is_positive, lambda x: x < -20],
),
)

Expand Down
Loading

0 comments on commit 036a848

Please sign in to comment.