aboutsummaryrefslogtreecommitdiff
path: root/quality_control
diff options
context:
space:
mode:
Diffstat (limited to 'quality_control')
-rw-r--r--quality_control/checks.py18
1 files changed, 18 insertions, 0 deletions
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