Skip to content

Commit

Permalink
rename skip_boolean -> eval_bools and change logic accordingly
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxDall committed Apr 20, 2024
1 parent a3ff496 commit 489a2b6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
14 changes: 7 additions & 7 deletions src/fundus/scraping/filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def _guarded_bool(value: Any):


class Requires:
def __init__(self, *required_attributes: str, skip_bool: bool = False) -> None:
def __init__(self, *required_attributes: str, eval_bools: bool = True) -> None:
"""Class to filter extractions based on attribute values
If a required_attribute is not present in the extracted data or evaluates to bool() -> False,
Expand All @@ -146,12 +146,12 @@ def __init__(self, *required_attributes: str, skip_bool: bool = False) -> None:
Args:
*required_attributes: Attributes required to evaluate to True in order to
pass the filter. If no attributes are given, all attributes will be evaluated
skip_boolean: If True then all attributes with boolean value will be evaluated with
<value> != None. If false, with bool(<value>). Defaults to False.
eval_bools: If True the boolean values will also be evaluated with bool(<value>).
If False, all boolean values evaluate to True. Defaults to True.
"""
self.required_attributes = set(required_attributes)
# somehow mypy does not recognize bool as callable :(
self._eval: Callable[[Any], bool] = _guarded_bool if skip_bool else bool # type: ignore[assignment]
self._eval: Callable[[Any], bool] = bool if eval_bools else _guarded_bool # type: ignore[assignment]

def __call__(self, extraction: Dict[str, Any]) -> FilterResultWithMissingAttributes:
missing_attributes = [
Expand All @@ -163,14 +163,14 @@ def __call__(self, extraction: Dict[str, Any]) -> FilterResultWithMissingAttribu


class RequiresAll(Requires):
def __init__(self, skip_boolean: bool = True) -> None:
def __init__(self, eval_bool: bool = False) -> None:
"""Name wrap for Requires()
This is for readability only. By default, it requires all non-boolean attributes of the extraction
to evaluate to True. Set `skip_boolean=False` to alter this behaviour.
See class:Requires docstring for more information.
Args:
skip_boolean: See Requires docstring for more information. Defaults to True.
eval_bool: See Requires docstring for more information. Defaults to False.
"""
super().__init__(skip_bool=skip_boolean)
super().__init__(eval_bools=eval_bool)
6 changes: 3 additions & 3 deletions tests/test_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def test_requires(self):

assert sorted(result.missing_attributes) == sorted(("b", "c"))

assert not Requires("c", skip_bool=True)(extraction)
assert not Requires("c", eval_bools=False)(extraction)

def test_requires_all(self):
extraction = {"a": "Some Stuff", "b": [], "c": False}
Expand All @@ -34,8 +34,8 @@ def test_requires_all(self):
# test skip_boolean=False
extraction = {"a": "Some Stuff", "b": [], "c": False}

assert (result := RequiresAll(skip_boolean=False)(extraction))
assert (result := RequiresAll(eval_bool=True)(extraction))
assert sorted(result.missing_attributes) == sorted(("b", "c"))

extraction = {"a": "Some Stuff", "c": True}
assert not RequiresAll(skip_boolean=False)(extraction)
assert not RequiresAll(eval_bool=True)(extraction)

0 comments on commit 489a2b6

Please sign in to comment.