From 092ae35512b3ef236622c4ab9dfe2bafb221ded7 Mon Sep 17 00:00:00 2001 From: Frederick Muriuki Muriithi Date: Mon, 12 Feb 2024 13:03:35 +0300 Subject: Refactor: Use new decimal places checker. --- quality_control/checks.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'quality_control') diff --git a/quality_control/checks.py b/quality_control/checks.py index 28b9ab5..475eb9e 100644 --- a/quality_control/checks.py +++ b/quality_control/checks.py @@ -2,6 +2,8 @@ import re from typing import Optional +from .errors import InvalidValue + def decimal_places_pattern(mini: int, maxi: Optional[int] = None) -> re.Pattern: """ Generate a regular expression for checking numbers @@ -49,3 +51,19 @@ def decimal_places_pattern(mini: int, maxi: Optional[int] = None) -> re.Pattern: + r"}" + r")$" ) + +def decimal_points_error(lineno: int, + field: str, + value: str, + mini: int, + maxi: Optional[int] = None) -> Optional[InvalidValue]: + """ + Check that 'value' in a decimal number with the appropriate decimal places. + """ + if not bool(decimal_places_pattern(mini, maxi).match(value)): + return InvalidValue(lineno, field, value, ( + f"Invalid value '{value}'. Expected numerical value " + + f"with at least {mini} decimal places" + + (f" and at most {maxi} decimal places" if maxi is not None else "") + + ".")) + return None -- cgit v1.2.3