# -*- coding: utf-8 -*- """ collatz_k_utils.py Utilitaires: lecture de listes depuis des fichiers Markdown, écriture de rapports, etc. """ from __future__ import annotations from pathlib import Path import re from typing import List, Optional def extract_ints_from_markdown(path: str, header_regex: Optional[str] = None) -> List[int]: """ Extrait tous les entiers d'un fichier Markdown. Si header_regex est fourni, extrait uniquement la section à partir du header. """ text = Path(path).read_text(encoding="utf-8") if header_regex is not None: m = re.search(header_regex, text, flags=re.S) if not m: raise ValueError(f"Section introuvable via regex: {header_regex}") text = m.group(1) return list(map(int, re.findall(r"\b\d+\b", text))) def parse_markdown_table_to_rows(path: str) -> List[List[str]]: """ Parse simple des lignes de table Markdown commençant par '|'. Retourne les champs (sans les '|', strip). """ rows: List[List[str]] = [] for ln in Path(path).read_text(encoding="utf-8").splitlines(): if not ln.startswith("|"): continue parts = [p.strip() for p in ln.strip().strip("|").split("|")] rows.append(parts) return rows def write_text(path: str, content: str) -> None: Path(path).write_text(content, encoding="utf-8")