Last active
January 11, 2026 19:07
-
-
Save serrasqueiro/9a25a66694ca2d209158ce51fc5ec804 to your computer and use it in GitHub Desktop.
ocultar saldos
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| import sys | |
| import re | |
| def main(): | |
| """ Retira informacao desnecessaria; | |
| masker = SaldoMasker(":", blanks_right=False) -> sem valor de saldo | |
| ... ("^", ...) -> descricao simplificada | |
| """ | |
| masker = SaldoMasker("^", blanks_right=False) | |
| for out in masker.process_stream(sys.stdin): | |
| sys.stdout.write(out) | |
| class SaldoMasker: | |
| what_else = "_" | |
| """ Mascarar o saldo de ficheiros de texto com varios campos. """ | |
| def __init__(self, mask="***", blanks_right=True): | |
| self.mask = mask | |
| self._right_subst = None if blanks_right else SaldoMasker.what_else | |
| self._simpler = mask in "^" | |
| def mask_line(self, line: str) -> str: | |
| s = line.rstrip("\n") | |
| # Encontrar todos os numeros com formato X.XX | |
| matches = list(re.finditer(r"\d+\.\d{2}", s)) | |
| if len(matches) < 2: | |
| return line | |
| saldo = matches[-1] | |
| start, end = saldo.span() | |
| # Largura real da coluna do saldo | |
| width = end - start | |
| # mascara alinhada 'a direita | |
| masked = self.mask.rjust(width) | |
| right = s[end:] | |
| # Reconstruir linha preservando tudo | |
| astr = s[:start] + ("" if self.mask == "^" else masked) | |
| astr += right if self._right_subst is None else self._else_str(right) | |
| return astr + "\n" | |
| def process_stream(self, stream): | |
| for line in stream: | |
| yield self.mask_line(line) | |
| def _else_str(self, right:str, do_strip:bool=True) -> str: | |
| """ Returns the string at the right with the appropriate trimming """ | |
| if do_strip: | |
| res = right.strip() | |
| else: | |
| res = right | |
| if self._simpler: | |
| now = res | |
| now = now.replace("; ", "@").split("@", 1)[-1] | |
| now = now.replace(" ", ".") | |
| else: | |
| now = res.replace(" ", self._right_subst) | |
| return now | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment