-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrip_to_it.py
More file actions
62 lines (49 loc) · 1.68 KB
/
Copy pathstrip_to_it.py
File metadata and controls
62 lines (49 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/env python3
"""Mantiene in data.json SOLO gli enti italiani (country == IT).
Il progetto attivo è l'Osservatorio italiano: `data.json` deve contenere **solo
l'Italia**, per non confondersi con i dati del fork mondiale (166 paesi). Questo
passo rende data.json IT-only e ricalcola `total` + `counts` (distribuzione
provider). Idempotente.
Uso: uv run python3 scripts/strip_to_it.py [--keep IT]
"""
from __future__ import annotations
import argparse
import json
import sys
from collections import Counter
from pathlib import Path
ROOT = Path(__file__).resolve().parent.parent
try:
sys.stdout.reconfigure(encoding="utf-8")
except Exception: # noqa: BLE001
pass
def main() -> int:
ap = argparse.ArgumentParser()
ap.add_argument("--data", type=Path, default=ROOT / "data.json")
ap.add_argument("--keep", default="IT")
args = ap.parse_args()
d = json.loads(args.data.read_text(encoding="utf-8"))
muns = d.get("municipalities")
if muns is None:
print("data.json senza chiave 'municipalities': niente da fare")
return 0
keep = args.keep.upper()
before = len(muns)
kept = {
k: v for k, v in muns.items() if (v.get("country") or "").upper() == keep
}
d["municipalities"] = kept
d["total"] = len(kept)
d["counts"] = dict(
Counter(v.get("provider", "unknown") for v in kept.values()).most_common()
)
args.data.write_text(
json.dumps(d, ensure_ascii=False, separators=(",", ":")), encoding="utf-8"
)
print(
f"strip_to_it: {before} → {len(kept)} enti "
f"(rimossi {before - len(kept)} non-{keep})"
)
return 0
if __name__ == "__main__":
raise SystemExit(main())