aboutsummaryrefslogtreecommitdiff
path: root/quality_control/utils.py
blob: 67301d6a4b788ad0f3f23953fdb326701f8027aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
"""Utilities that might be useful elsewhere."""
import re
from collections import namedtuple

from .errors import InvalidValue

ProgressIndicator = namedtuple(
    "ProgressIndicator", ("filesize", "processedsize", "currentline", "percent"))

def make_progress_calculator(filesize: int):
    """
    Returns a function that takes two arguments, `linenumber` and `linetext` and
    return a `ProgressIndicator` object with the computed progress.
    """
    processedsize = 0
    def __calculator__(linenumber: int, linetext: str) -> ProgressIndicator:
        nonlocal processedsize
        processedsize = processedsize + len(linetext)
        return ProgressIndicator(
            filesize, processedsize, linenumber,
            ((processedsize/filesize) * 100))

    return __calculator__

def cell_error(pattern, val, **error_kwargs):
    "Return the error in the cell"
    if re.search(pattern, val):
        return None
    if re.search(r"^0\.0+$", val) or re.search("^0+$", val):
        return None
    return InvalidValue(**error_kwargs)