File size: 1,632 Bytes
71a7230
4c04f50
71a7230
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
"""Amend avec from align_block."""
# pylint: disable=unused-variable, unused-import

from typing import List, Tuple, Union

from itertools import zip_longest
from logzero import logger  # noqa

# from radiobee.gen_aset import gen_aset


# fmt: off
def amend_avec(
        avec: List[Tuple[int, int]],
        len0: int = 0,
        len1: int = 0,
) -> List[Tuple[Union[int, str], Union[int, str]]]:
    # fmt: on
    """Amend avec from align_block.

    Rewritten based on gen_aset.

    Args:
        avec: list generated by align_block
        len0: max first possibe entries
        len1: max second entries
    Return
        nicely amended to include all entries
    Raise
        None
    """
    # empty avec []
    if not avec:
        return [*zip_longest(range(len0), range(len1), fillvalue="")]
    # empty [[]]
    if len(avec) == 1:
        if not avec[0]:
            return [*zip_longest(range(len0), range(len1), fillvalue="")]

    buff = []
    pos0, pos1 = -1, -1
    for elm in avec:
        # elm0, elm1, elm2 = elm
        elm0, elm1, *elm2 = elm
        elm0 = int(elm0)
        elm1 = int(elm1)
        interval = max(elm0 - pos0 - 1, elm1 - pos1 - 1)
        _ = zip_longest(range(pos0 + 1, elm0), range(pos1 + 1, elm1), [""] * interval, fillvalue="")
        buff.extend(_)
        buff.append(elm)
        pos0, pos1 = elm0, elm1

    # last batch if any
    # elm0, elm1 = tgt_len, src_len
    elm0, elm1 = len0, len1
    interval = max(elm0 - pos0 - 1, elm1 - pos1 - 1)
    _ = zip_longest(range(pos0 + 1, elm0), range(pos1 + 1, elm1), [""] * interval, fillvalue="")
    buff.extend(_)

    return buff