aboutsummaryrefslogtreecommitdiff
path: root/quality_control/utils.py
blob: 0072608a3055220d7133346395816c1d167b3faa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
"""Utilities that might be useful elsewhere."""

from collections import namedtuple

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__