-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
41 lines (34 loc) · 1.29 KB
/
utils.py
File metadata and controls
41 lines (34 loc) · 1.29 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
import json
from pathlib import Path
from datetime import datetime
from collections import Counter
from typing import List, Dict
import logging
def setup_logging():
log_dir = Path("logs")
log_dir.mkdir(exist_ok=True)
log_file = log_dir / f"sim_{datetime.now().strftime('%Y%m%d_%H%M%S')}.log"
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s | %(levelname)s | %(message)s',
handlers=[
logging.FileHandler(log_file, encoding='utf-8'),
logging.StreamHandler()
]
)
return logging.getLogger(__name__)
def guardar_estado(poblacion: List, generacion: int, historia: List):
data = {
"generacion": generacion,
"timestamp": datetime.now().isoformat(),
"poblacion_total": len(poblacion),
"vivos": len([a for a in poblacion if a.energia > 0]),
"culturas": dict(Counter(a.cultura for a in poblacion if a.energia > 0)),
"mitos_ejemplos": [a.mitos[-1] for a in poblacion if a.mitos][-5:]
}
out_dir = Path("outputs")
out_dir.mkdir(exist_ok=True)
with open(out_dir / f"estado_gen_{generacion}.json", "w", encoding="utf-8") as f:
json.dump(data, f, indent=2, ensure_ascii=False)
def contar_culturas(vivos):
return dict(Counter(a.cultura for a in vivos))