1

Aère le graphique des nouveaux personnages

This commit is contained in:
2025-12-03 22:29:19 +01:00
parent ad44796759
commit 46cef55a75
6 changed files with 240 additions and 1 deletions

View File

@@ -69,6 +69,47 @@ def aggregate_variations_and_totals(
return aggregates
def aggregate_new_characters_by_year(
minifigs_rows: Iterable[dict],
sets_years: Dict[str, str],
excluded_characters: Sequence[str] | None = None,
start_year: int | None = None,
end_year: int | None = None,
) -> List[dict]:
"""Compte le nombre de personnages introduits par année sur une plage donnée."""
excluded = set(excluded_characters or [])
first_year: Dict[str, int] = {}
for row in minifigs_rows:
character = row["known_character"].strip()
fig_num = row["fig_num"].strip()
if character == "" or fig_num == "":
continue
if character in excluded:
continue
year_str = sets_years.get(row["set_num"])
if year_str is None:
continue
year_int = int(year_str)
current = first_year.get(character)
if current is None or year_int < current:
first_year[character] = year_int
counts: Dict[int, int] = {}
if start_year is not None and end_year is not None:
for year in range(start_year, end_year + 1):
counts[year] = 0
for character, year_int in first_year.items():
if start_year is not None and year_int < start_year:
continue
if end_year is not None and year_int > end_year:
continue
counts[year_int] = counts.get(year_int, 0) + 1
years = sorted(counts.keys())
results: List[dict] = []
for year in years:
results.append({"year": str(year), "new_characters": str(counts[year])})
return results
def aggregate_by_gender(rows: Iterable[dict]) -> List[dict]:
"""Compte les minifigs distinctes par genre (fig_num unique)."""
genders_by_fig: Dict[str, str] = {}
@@ -102,6 +143,17 @@ def write_character_counts(path: Path, rows: Sequence[dict]) -> None:
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)
fieldnames = ["year", "new_characters"]
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_gender_counts(path: Path, rows: Sequence[dict]) -> None:
"""Écrit le CSV des comptes par genre."""
ensure_parent_dir(path)