mod alterna valori ping pong

This commit is contained in:
2025-08-23 16:58:52 +02:00
parent d1582b8f9e
commit 730869ef1f

View File

@@ -1,17 +1,37 @@
import glob import glob
import os import os
from itertools import cycle, chain
import logging import logging
logger = logging.getLogger() 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: if not valori:
yield val1 return
yield val2
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]]: 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: try:
with open(file_path, 'r', encoding='utf-8') as file: with open(file_path, 'r', encoding='utf-8') as file:
lines = file.readlines() lines = file.readlines()
for line in lines: # Usando dict.fromkeys() per mantenere l'ordine e togliere le righe duplicate per i warnings
stripped_line = line.strip() non_empty_lines = [line.strip() for line in lines if line.strip()]
if stripped_line: # Ignora righe vuote
if stripped_line.startswith('Error'): errors = [line for line in non_empty_lines if line.startswith('Error')]
errors.append(stripped_line) warnings = list(dict.fromkeys(line for line in non_empty_lines if not line.startswith('Error')))
else:
warnings.append(stripped_line)
except Exception as e: except Exception as e:
logger.error(f"Errore durante la lettura del file {file_path}: {e}") logger.error(f"Errore durante la lettura del file {file_path}: {e}")