You've already forked etude_lego_jurassic_world
Ajoute le graphique variations vs total par personnage
This commit is contained in:
@@ -34,6 +34,41 @@ def aggregate_by_character(rows: Iterable[dict]) -> List[dict]:
|
||||
return aggregates
|
||||
|
||||
|
||||
def aggregate_variations_and_totals(
|
||||
rows: Iterable[dict],
|
||||
excluded_characters: Sequence[str] | None = None,
|
||||
) -> List[dict]:
|
||||
"""Compte les variations uniques et le total de minifigs par personnage."""
|
||||
excluded = set(excluded_characters or [])
|
||||
variations: Dict[str, set] = defaultdict(set)
|
||||
totals: Dict[str, int] = defaultdict(int)
|
||||
genders: Dict[str, str] = {}
|
||||
for row in rows:
|
||||
character = row["known_character"].strip()
|
||||
fig_num = row["fig_num"].strip()
|
||||
gender = row.get("gender", "").strip()
|
||||
if character == "" or fig_num == "":
|
||||
continue
|
||||
if character in excluded:
|
||||
continue
|
||||
variations[character].add(fig_num)
|
||||
totals[character] += 1
|
||||
if character not in genders:
|
||||
genders[character] = gender
|
||||
aggregates: List[dict] = []
|
||||
for character, fig_nums in variations.items():
|
||||
aggregates.append(
|
||||
{
|
||||
"known_character": character,
|
||||
"gender": genders.get(character, ""),
|
||||
"variation_count": len(fig_nums),
|
||||
"total_minifigs": totals.get(character, 0),
|
||||
}
|
||||
)
|
||||
aggregates.sort(key=lambda r: (-r["total_minifigs"], -r["variation_count"], r["known_character"]))
|
||||
return aggregates
|
||||
|
||||
|
||||
def aggregate_by_gender(rows: Iterable[dict]) -> List[dict]:
|
||||
"""Compte les minifigs distinctes par genre (fig_num unique)."""
|
||||
genders_by_fig: Dict[str, str] = {}
|
||||
@@ -78,6 +113,17 @@ def write_gender_counts(path: Path, rows: Sequence[dict]) -> None:
|
||||
writer.writerow(row)
|
||||
|
||||
|
||||
def write_character_variations_totals(path: Path, rows: Sequence[dict]) -> None:
|
||||
"""Écrit le CSV comparant variations et total par personnage."""
|
||||
ensure_parent_dir(path)
|
||||
fieldnames = ["known_character", "gender", "variation_count", "total_minifigs"]
|
||||
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 load_sets_enriched(path: Path) -> Dict[str, str]:
|
||||
"""Indexe les années par set_num."""
|
||||
lookup: Dict[str, str] = {}
|
||||
|
||||
Reference in New Issue
Block a user