Spaces:
Build error
Build error
"""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 | |