Load text metrics as lists of the hour's samples
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
b0e77d2745
commit
f8a81c0990
2 files changed
+13
-5
No files matched your search
+11
-3
@@ -2,6 +2,7 @@
|
||||
|
||||
Format des CSV : docs/domaine/ce-que-mesure-la-station.md. Bibliothèque standard uniquement.
|
||||
"""
|
||||
import ast
|
||||
import csv
|
||||
import glob
|
||||
import os
|
||||
@@ -18,14 +19,21 @@ def parse_name(path):
|
||||
return (m["station"], m["metric"]) if m else None
|
||||
|
||||
|
||||
def parse_value(x):
|
||||
"""'' -> None (pas de mesure) ; "['up', 'up']" -> liste des échantillons de l'heure (métriques texte) ; sinon float."""
|
||||
if not x:
|
||||
return None
|
||||
return ast.literal_eval(x) if x.startswith("[") else float(x)
|
||||
|
||||
|
||||
def load_csv(path):
|
||||
"""CSV ',time_utc,metric' -> {datetime UTC: float ou None (pas de mesure)}."""
|
||||
"""CSV ',time_utc,metric' -> {datetime UTC: float, liste (métrique texte) ou None (pas de mesure)}."""
|
||||
try:
|
||||
with open(path, newline="") as f:
|
||||
rows = csv.reader(f)
|
||||
next(rows)
|
||||
return {datetime.fromisoformat(r[1]): float(r[2]) if r[2] else None for r in rows}
|
||||
except (StopIteration, IndexError, ValueError) as e:
|
||||
return {datetime.fromisoformat(r[1]): parse_value(r[2]) for r in rows}
|
||||
except (StopIteration, IndexError, ValueError, SyntaxError) as e:
|
||||
raise ValueError(f"{path} : CSV illisible ({e!r})") from e
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user