| f | class Currency: | f | class Currency: |
| """Class that defines different currencies that help political parties byu votes.""" | | """Class that defines different currencies that help political parties byu votes.""" |
| def __init__(self, name, rate): | | def __init__(self, name, rate): |
| self.name = name | | self.name = name |
| self.rate = rate | | self.rate = rate |
| | | |
| def __eq__(self, other): | | def __eq__(self, other): |
| return self.name == other.name and self.rate == other.rate | | return self.name == other.name and self.rate == other.rate |
| | | |
| | | |
| class PoliticalParty: | | class PoliticalParty: |
| n | """Class which defines political parties.""" | n | """Class that defines political parties and currency-voters' conversion.""" |
| def __init__(self, name, motto, members=[], preferred_currency=None): | | def __init__(self, name, motto, members=[], preferred_currency=None): |
| self.name = name | | self.name = name |
| self.__motto = motto | | self.__motto = motto |
| self.members = members | | self.members = members |
| self.preferred_currency = preferred_currency | | self.preferred_currency = preferred_currency |
| | | |
| def convert_currency_to_voters(self, currency_count, currency): | | def convert_currency_to_voters(self, currency_count, currency): |
| multiplier = 1 | | multiplier = 1 |
| if self.preferred_currency and currency == self.preferred_currency: | | if self.preferred_currency and currency == self.preferred_currency: |
| multiplier *= 2 | | multiplier *= 2 |
| | | |
| return int(currency_count / currency.rate) * multiplier | | return int(currency_count / currency.rate) * multiplier |
| | | |
| @property | | @property |
| def motto(self): | | def motto(self): |
| return self.__motto | | return self.__motto |
| | | |
| def __str__(self): | | def __str__(self): |
| return self.name | | return self.name |
| | | |
| def __add__(self, other): | | def __add__(self, other): |
| return Coalition(self, other) | | return Coalition(self, other) |
| | | |
| class Coalition: | | class Coalition: |
| n | """Class which defines coalitions.""" | n | """Class that defines coalitions and how they add parties.""" |
| def __init__(self, *parties): | | def __init__(self, *parties): |
| self.parties = list(parties) | | self.parties = list(parties) |
| | | |
| @property | | @property |
| def members(self): | | def members(self): |
| return {party.name : party.members for party in self.parties} | | return {party.name : party.members for party in self.parties} |
| | | |
| def __add__(self, other): | | def __add__(self, other): |
| new_parties = self.parties.copy() | | new_parties = self.parties.copy() |
| if isinstance(other, PoliticalParty): | | if isinstance(other, PoliticalParty): |
| new_parties.append(other) | | new_parties.append(other) |
| elif isinstance(other, Coalition): | | elif isinstance(other, Coalition): |
| new_parties.extend(other.parties) | | new_parties.extend(other.parties) |
| else: | | else: |
| raise TypeError("Invalid type for Coalition.") | | raise TypeError("Invalid type for Coalition.") |
| | | |
| return Coalition(*new_parties) | | return Coalition(*new_parties) |
| | | |
| def __str__(self): | | def __str__(self): |
| return "-".join([party.name for party in self.parties]) | | return "-".join([party.name for party in self.parties]) |
| | | |
| | | |
| class Elections: | | class Elections: |
| t | """Class which defines the election's process.""" | t | """Class that defines the election's process.""" |
| _history = {} | | _history = {} |
| | | |
| def __init__(self, date): | | def __init__(self, date): |
| self.date = date | | self.date = date |
| self.participants = {} | | self.participants = {} |
| Elections._history[date] = self.participants | | Elections._history[date] = self.participants |
| | | |
| def register_party_or_coalition(self, party_or_coalition): | | def register_party_or_coalition(self, party_or_coalition): |
| if not isinstance(party_or_coalition, (Coalition, PoliticalParty)): | | if not isinstance(party_or_coalition, (Coalition, PoliticalParty)): |
| raise TypeError("Invalid type for Elections.") | | raise TypeError("Invalid type for Elections.") |
| self.participants[party_or_coalition] = 0 | | self.participants[party_or_coalition] = 0 |
| | | |
| def vote(self, party_or_coalition): | | def vote(self, party_or_coalition): |
| if party_or_coalition not in self.participants: | | if party_or_coalition not in self.participants: |
| # хубаво да се направи custom exception, but time is ticking away | | # хубаво да се направи custom exception, but time is ticking away |
| raise RuntimeError("Party or coalition not registered in the elections.") | | raise RuntimeError("Party or coalition not registered in the elections.") |
| self.participants[party_or_coalition] += 1 | | self.participants[party_or_coalition] += 1 |
| | | |
| def rig_elections(self, party_or_coalition, count, currency): | | def rig_elections(self, party_or_coalition, count, currency): |
| if isinstance(party_or_coalition, PoliticalParty): | | if isinstance(party_or_coalition, PoliticalParty): |
| voters = party_or_coalition.convert_currency_to_voters(count, currency) | | voters = party_or_coalition.convert_currency_to_voters(count, currency) |
| self.participants[party_or_coalition] += voters | | self.participants[party_or_coalition] += voters |
| elif isinstance(party_or_coalition, Coalition): | | elif isinstance(party_or_coalition, Coalition): |
| max_voters = 0 | | max_voters = 0 |
| | | |
| for party in party_or_coalition.parties: | | for party in party_or_coalition.parties: |
| voters = party.convert_currency_to_voters(count, currency) | | voters = party.convert_currency_to_voters(count, currency) |
| if voters > max_voters: | | if voters > max_voters: |
| max_voters = voters | | max_voters = voters |
| self.participants[party_or_coalition] += max_voters | | self.participants[party_or_coalition] += max_voters |
| else: | | else: |
| raise TypeError("Invalid type for rig_elections.") | | raise TypeError("Invalid type for rig_elections.") |
| | | |
| def get_results(self): | | def get_results(self): |
| return {str(party_or_coalition) : count for party_or_coalition, count in self.participants.items()} | | return {str(party_or_coalition) : count for party_or_coalition, count in self.participants.items()} |
| | | |
| @staticmethod | | @staticmethod |
| def get_results_by_year(year): | | def get_results_by_year(year): |
| if year in Elections._history: | | if year in Elections._history: |
| return {str(k): v for k, v in Elections._history[year].items()} | | return {str(k): v for k, v in Elections._history[year].items()} |
| return {} | | return {} |
| | | |
25.03.2026 11:48
25.03.2026 11:48