From 730869ef1ffee406e42d926a0aa7161db772af83 Mon Sep 17 00:00:00 2001 From: alex Date: Sat, 23 Aug 2025 16:58:52 +0200 Subject: [PATCH] mod alterna valori ping pong --- src/utils/general.py | 42 ++++++++++++++++++++++++++++++------------ 1 file changed, 30 insertions(+), 12 deletions(-) diff --git a/src/utils/general.py b/src/utils/general.py index 0b7a833..b1fac0e 100644 --- a/src/utils/general.py +++ b/src/utils/general.py @@ -1,17 +1,37 @@ import glob import os +from itertools import cycle, chain import logging logger = logging.getLogger() -def alterna_valori(val1: str, val2: str) -> any: +def alterna_valori(*valori: any, ping_pong: bool = False) -> any: """ - Restituisce alternativamente il primo ed il secondo valore + Genera una sequenza ciclica di valori, con opzione per una sequenza "ping-pong". + + Args: + *valori (any): Uno o più valori da ciclare. + ping_pong (bool, optional): Se True, la sequenza sarà valori -> valori al contrario. + Ad esempio, per (1, 2, 3) diventa 1, 2, 3, 2, 1, 2, 3, ... + Se False, la sequenza è semplicemente ciclica. + Defaults to False. + + Yields: + any: Il prossimo valore nella sequenza ciclica. + """ - while True: - yield val1 - yield val2 + if not valori: + return + + if ping_pong: + # Crea la sequenza ping-pong: valori + valori al contrario (senza ripetere primo e ultimo) + forward = valori + backward = valori[-2:0:-1] # Esclude ultimo e primo elemento + ping_pong_sequence = chain(forward, backward) + yield from cycle(ping_pong_sequence) + else: + yield from cycle(valori) async def read_error_lines_from_logs(base_path: str, pattern: str) -> tuple[list[str], list[str]]: @@ -46,13 +66,11 @@ async def read_error_lines_from_logs(base_path: str, pattern: str) -> tuple[list try: with open(file_path, 'r', encoding='utf-8') as file: lines = file.readlines() - for line in lines: - stripped_line = line.strip() - if stripped_line: # Ignora righe vuote - if stripped_line.startswith('Error'): - errors.append(stripped_line) - else: - warnings.append(stripped_line) + # Usando dict.fromkeys() per mantenere l'ordine e togliere le righe duplicate per i warnings + non_empty_lines = [line.strip() for line in lines if line.strip()] + + errors = [line for line in non_empty_lines if line.startswith('Error')] + warnings = list(dict.fromkeys(line for line in non_empty_lines if not line.startswith('Error'))) except Exception as e: logger.error(f"Errore durante la lettura del file {file_path}: {e}")