Skip to content

Commit

Permalink
[classes] Raise ExceptionGroup for all duplicates
Browse files Browse the repository at this point in the history
  • Loading branch information
Prometheus3375 committed Dec 23, 2024
1 parent a9d05c4 commit afbc3d9
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions classes.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from collections import Counter
from collections.abc import Iterable, Sequence
from dataclasses import InitVar, dataclass, field
from itertools import product
Expand Down Expand Up @@ -208,21 +209,34 @@ def to_dim_wishlist_file(self, filepath: str, /) -> None:
"""
Writes this wishlist to a file.
"""
added_rolls: set[tuple[Item, frozenset[Item]]] = set()

with open(filepath, 'w', encoding='utf-8') as file:
file.write(f'title:{self.title}\n')
if self.description:
file.write(f'description:{self.description}\n')

file.write(f'\n')

added_rolls: Counter[tuple[Item, frozenset[Item]]] = Counter()
self._write_wishlist_entry_list(file, added_rolls, trash_list=False)
self._write_wishlist_entry_list(file, added_rolls, trash_list=True)

errors = [
ValueError(
f'{item.name} [{item.hash}] with '
f'{', '.join(perk.name for perk in combo)} '
f'is duplicated {count - 1} {'time' if count == 2 else 'times'}'
)
for (item, combo), count in added_rolls.items()
if count > 1
]
if errors:
raise ExceptionGroup('some perk combinations are duplicated', errors)

def _write_wishlist_entry_list(
self,
file: TextIO,
added_rolls: set[tuple[Item, frozenset[Item]]],
added_rolls: Counter[tuple[Item, frozenset[Item]]],
/,
trash_list: bool,
) -> None:
Expand All @@ -242,15 +256,7 @@ def _write_wishlist_entry_list(
last_name = ''
for entry in entry_list:
for combo in entry.combos:
roll = entry.item, frozenset(combo)
if roll in added_rolls:
raise ValueError(
f'{entry.item.name} with '
f'{', '.join(perk.name for perk in combo)} '
f'was already added to this wishlist'
)
else:
added_rolls.add(roll)
added_rolls[entry.item, frozenset(combo)] += 1

curr_name = entry.item.name
if curr_name != last_name:
Expand Down

0 comments on commit afbc3d9

Please sign in to comment.