1

Compare la répartition des genres minifigs vs personnages

This commit is contained in:
2025-12-03 22:55:05 +01:00
parent cc613a88af
commit a976b57afe
6 changed files with 142 additions and 7 deletions

View File

@@ -195,6 +195,28 @@ def aggregate_by_gender(rows: Iterable[dict]) -> List[dict]:
return aggregates
def aggregate_characters_by_gender(rows: Iterable[dict]) -> List[dict]:
"""Compte les personnages distincts par genre (hors genres inconnus)."""
gender_by_character: Dict[str, str] = {}
counts: Dict[str, int] = defaultdict(int)
for row in rows:
character = row["known_character"].strip()
gender = row.get("gender", "").strip().lower()
if character == "":
continue
if gender not in ("male", "female"):
continue
if character in gender_by_character:
continue
gender_by_character[character] = gender
counts[gender] += 1
aggregates: List[dict] = []
for gender in ("female", "male"):
if gender in counts:
aggregates.append({"gender": gender, "character_count": str(counts[gender])})
return aggregates
def write_character_counts(path: Path, rows: Sequence[dict]) -> None:
"""Écrit le CSV des comptes par personnage."""
ensure_parent_dir(path)
@@ -206,6 +228,17 @@ def write_character_counts(path: Path, rows: Sequence[dict]) -> None:
writer.writerow(row)
def write_character_gender_counts(path: Path, rows: Sequence[dict]) -> None:
"""Écrit le CSV des comptes de personnages par genre."""
ensure_parent_dir(path)
fieldnames = ["gender", "character_count"]
with path.open("w", newline="") as csv_file:
writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
writer.writeheader()
for row in rows:
writer.writerow(row)
def write_new_characters_by_year(path: Path, rows: Sequence[dict]) -> None:
"""Écrit le CSV des personnages introduits chaque année."""
ensure_parent_dir(path)