algo/v0/collatz_k_scripts/collatz_k_utils.py
Nicolas Cantu 2b99e8ff02 Skills document-improvement et scripts Collatz
**Motivations:**
- Ajout skill pour amélioration de documents en background
- Scripts et documentation Collatz

**Evolutions:**
- .cursor/skills/document-improvement/ (SKILL, reference, examples)
- v0/collatz_k_scripts/ (core, fusion, pipeline, utils, reproduce)
- v0/journal.md, v0/log.md, v0/README collatz

**Pages affectées:**
- .cursor/skills/document-improvement/
- v0/collatz_k_scripts/
- v0/journal.md, v0/log.md
2026-02-27 16:23:25 +01:00

45 lines
1.3 KiB
Python

# -*- 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")