1class Currency:
2 def __init__(self, name, amount_to_bribe):
3 self.name =name
4 self.amount_to_bribe = amount_to_bribe
5
6 def __eq__(self, other):
7 if isinstance(other, type(self)):
8 return self.amount_to_bribe == other.amount_to_bribe and self.name == other.name
9 return False
10
11
12class PoliticalParty:
13 def __init__(self, party_name, motto, members=[], preferred_currency=None):
14 # този път фразата е на английски не на италиански :D
15 self.name = party_name
16 self.__motto = motto
17 self.members = members
18 self.preferred_currency = preferred_currency
19
20 # @property
21 # def motto(self):
22 # return self._motto
23
24 # @motto.setter
25 # def motto(self, value):
26 # return "ТЦ! Не може! Грешка."
27
28 def convert_currency_to_voters(self, amount, currency: Currency):
29 return int(amount / currency.amount_to_bribe) * (2 if self.preferred_currency and self.preferred_currency == currency else 1)
30
31 def __getattr__(self, name: str):
32 if name == "motto":
33 return self.__motto
34 super().__getattr__(name)
35
36 def __setattr__(self, name: str, value):
37 if name == "motto":
38 return "ТЦ! Не може! Грешка."
39 super().__setattr__(name, value)
40
41 def __str__(self):
42 return self.name
43
44 def __add__(self, other: PoliticalParty | Coalition):
45 return Coalition(self, other) if isinstance(other, PoliticalParty) else Coalition(self, *other.parties)
46
47
48# GERB = PoliticalParty("ГЕРБ", "Ту-тууу!")
49# print(GERB.motto) # "Ту-тууу!"
50# print(GERB.__setattr__("motto", "Дайте пак да се пробваме?")) # ТЦ! Не може! Грешка.
51# print(GERB.motto) # "Ту-тууу!"
52
53class Coalition:
54 def __init__(self, *political_parties):
55 self.parties = political_parties
56
57 @property
58 def members(self):
59 return {party.name: party.members for party in self.parties}
60
61 def __add__(self, other: PoliticalParty | Coalition):
62 return Coalition(*self.parties, other) if isinstance(other, PoliticalParty) else Coalition(*self.parties, *other.parties)
63
64 def __str__(self):
65 return "-".join([n for n in self.members.keys()])
66
67
68class Elections:
69 history: dict[int, Elections] = {}
70
71 def __init__(self, year):
72 self.year = year
73 self.candidates = {}
74 Elections.history[year] = self
75
76 def register_party_or_coalition(self, party_or_coalition: PoliticalParty | Coalition):
77 self.candidates[party_or_coalition] = self.candidates.get(party_or_coalition, 0)
78
79 def vote(self, party_or_coalition: PoliticalParty | Coalition):
80 self.candidates[party_or_coalition] = self.candidates.get(party_or_coalition, 0) + 1
81
82 def rig_elections(self, party_or_coalition: PoliticalParty | Coalition, amount, currency: Currency):
83 all_members = party_or_coalition.parties if isinstance(party_or_coalition, Coalition) else [party_or_coalition]
84 doubling_currency_party = next((c for c in all_members if c.preferred_currency == currency), None)
85 votes = doubling_currency_party.convert_currency_to_voters(amount, currency) if doubling_currency_party else all_members[0].convert_currency_to_voters(amount, currency)
86 self.candidates[party_or_coalition] = self.candidates.get(party_or_coalition, 0) + votes
87
88 def get_results(self):
89 return {str(party_or_coalition): votes for party_or_coalition, votes in self.candidates.items()}
90
91 @classmethod
92 def get_results_by_year(cls, year):
93 return cls.history[year].get_results()
94
.................F...
======================================================================
FAIL: test_motto_write (test.TestPoliticalParty.test_motto_write)
Motto should not be writable after the party is created.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 37, in test_motto_write
with self.assertRaises(Exception):
~~~~~~~~~~~~~~~~~^^^^^^^^^^^
AssertionError: Exception not raised
----------------------------------------------------------------------
Ran 21 tests in 0.001s
FAILED (failures=1)
Виктор Бечев
22.03.2026 17:05Отвъд горното подобрение - изглежда доста добре.
|
| n | 1 | from typing import Any | n | ||
| 2 | |||||
| 3 | |||||
| 4 | class Currency: | 1 | class Currency: | ||
| 5 | def __init__(self, name, amount_to_bribe): | 2 | def __init__(self, name, amount_to_bribe): | ||
| 6 | self.name =name | 3 | self.name =name | ||
| 7 | self.amount_to_bribe = amount_to_bribe | 4 | self.amount_to_bribe = amount_to_bribe | ||
| 8 | 5 | ||||
| 9 | def __eq__(self, other): | 6 | def __eq__(self, other): | ||
| 10 | if isinstance(other, type(self)): | 7 | if isinstance(other, type(self)): | ||
| 11 | return self.amount_to_bribe == other.amount_to_bribe and self.name == other.name | 8 | return self.amount_to_bribe == other.amount_to_bribe and self.name == other.name | ||
| 12 | return False | 9 | return False | ||
| 13 | 10 | ||||
| 14 | 11 | ||||
| 15 | class PoliticalParty: | 12 | class PoliticalParty: | ||
| 16 | def __init__(self, party_name, motto, members=[], preferred_currency=None): | 13 | def __init__(self, party_name, motto, members=[], preferred_currency=None): | ||
| 17 | # този път фразата е на английски не на италиански :D | 14 | # този път фразата е на английски не на италиански :D | ||
| 18 | self.name = party_name | 15 | self.name = party_name | ||
| 19 | self.__motto = motto | 16 | self.__motto = motto | ||
| 20 | self.members = members | 17 | self.members = members | ||
| 21 | self.preferred_currency = preferred_currency | 18 | self.preferred_currency = preferred_currency | ||
| 22 | 19 | ||||
| 23 | # @property | 20 | # @property | ||
| 24 | # def motto(self): | 21 | # def motto(self): | ||
| 25 | # return self._motto | 22 | # return self._motto | ||
| 26 | 23 | ||||
| 27 | # @motto.setter | 24 | # @motto.setter | ||
| 28 | # def motto(self, value): | 25 | # def motto(self, value): | ||
| 29 | # return "ТЦ! Не може! Грешка." | 26 | # return "ТЦ! Не може! Грешка." | ||
| 30 | 27 | ||||
| 31 | def convert_currency_to_voters(self, amount, currency: Currency): | 28 | def convert_currency_to_voters(self, amount, currency: Currency): | ||
| 32 | return int(amount / currency.amount_to_bribe) * (2 if self.preferred_currency and self.preferred_currency == currency else 1) | 29 | return int(amount / currency.amount_to_bribe) * (2 if self.preferred_currency and self.preferred_currency == currency else 1) | ||
| 33 | 30 | ||||
| 34 | def __getattr__(self, name: str): | 31 | def __getattr__(self, name: str): | ||
| 35 | if name == "motto": | 32 | if name == "motto": | ||
| 36 | return self.__motto | 33 | return self.__motto | ||
| 37 | super().__getattr__(name) | 34 | super().__getattr__(name) | ||
| 38 | 35 | ||||
| n | 39 | def __setattr__(self, name: str, value: Any): | n | 36 | def __setattr__(self, name: str, value): |
| 40 | if name == "motto": | 37 | if name == "motto": | ||
| 41 | return "ТЦ! Не може! Грешка." | 38 | return "ТЦ! Не може! Грешка." | ||
| 42 | super().__setattr__(name, value) | 39 | super().__setattr__(name, value) | ||
| 43 | 40 | ||||
| 44 | def __str__(self): | 41 | def __str__(self): | ||
| 45 | return self.name | 42 | return self.name | ||
| 46 | 43 | ||||
| 47 | def __add__(self, other: PoliticalParty | Coalition): | 44 | def __add__(self, other: PoliticalParty | Coalition): | ||
| 48 | return Coalition(self, other) if isinstance(other, PoliticalParty) else Coalition(self, *other.parties) | 45 | return Coalition(self, other) if isinstance(other, PoliticalParty) else Coalition(self, *other.parties) | ||
| 49 | 46 | ||||
| t | t | 47 | |||
| 48 | # GERB = PoliticalParty("ГЕРБ", "Ту-тууу!") | ||||
| 49 | # print(GERB.motto) # "Ту-тууу!" | ||||
| 50 | # print(GERB.__setattr__("motto", "Дайте пак да се пробваме?")) # ТЦ! Не може! Грешка. | ||||
| 51 | # print(GERB.motto) # "Ту-тууу!" | ||||
| 50 | 52 | ||||
| 51 | class Coalition: | 53 | class Coalition: | ||
| 52 | def __init__(self, *political_parties): | 54 | def __init__(self, *political_parties): | ||
| 53 | self.parties = political_parties | 55 | self.parties = political_parties | ||
| 54 | 56 | ||||
| 55 | @property | 57 | @property | ||
| 56 | def members(self): | 58 | def members(self): | ||
| 57 | return {party.name: party.members for party in self.parties} | 59 | return {party.name: party.members for party in self.parties} | ||
| 58 | 60 | ||||
| 59 | def __add__(self, other: PoliticalParty | Coalition): | 61 | def __add__(self, other: PoliticalParty | Coalition): | ||
| 60 | return Coalition(*self.parties, other) if isinstance(other, PoliticalParty) else Coalition(*self.parties, *other.parties) | 62 | return Coalition(*self.parties, other) if isinstance(other, PoliticalParty) else Coalition(*self.parties, *other.parties) | ||
| 61 | 63 | ||||
| 62 | def __str__(self): | 64 | def __str__(self): | ||
| 63 | return "-".join([n for n in self.members.keys()]) | 65 | return "-".join([n for n in self.members.keys()]) | ||
| 64 | 66 | ||||
| 65 | 67 | ||||
| 66 | class Elections: | 68 | class Elections: | ||
| 67 | history: dict[int, Elections] = {} | 69 | history: dict[int, Elections] = {} | ||
| 68 | 70 | ||||
| 69 | def __init__(self, year): | 71 | def __init__(self, year): | ||
| 70 | self.year = year | 72 | self.year = year | ||
| 71 | self.candidates = {} | 73 | self.candidates = {} | ||
| 72 | Elections.history[year] = self | 74 | Elections.history[year] = self | ||
| 73 | 75 | ||||
| 74 | def register_party_or_coalition(self, party_or_coalition: PoliticalParty | Coalition): | 76 | def register_party_or_coalition(self, party_or_coalition: PoliticalParty | Coalition): | ||
| 75 | self.candidates[party_or_coalition] = self.candidates.get(party_or_coalition, 0) | 77 | self.candidates[party_or_coalition] = self.candidates.get(party_or_coalition, 0) | ||
| 76 | 78 | ||||
| 77 | def vote(self, party_or_coalition: PoliticalParty | Coalition): | 79 | def vote(self, party_or_coalition: PoliticalParty | Coalition): | ||
| 78 | self.candidates[party_or_coalition] = self.candidates.get(party_or_coalition, 0) + 1 | 80 | self.candidates[party_or_coalition] = self.candidates.get(party_or_coalition, 0) + 1 | ||
| 79 | 81 | ||||
| 80 | def rig_elections(self, party_or_coalition: PoliticalParty | Coalition, amount, currency: Currency): | 82 | def rig_elections(self, party_or_coalition: PoliticalParty | Coalition, amount, currency: Currency): | ||
| 81 | all_members = party_or_coalition.parties if isinstance(party_or_coalition, Coalition) else [party_or_coalition] | 83 | all_members = party_or_coalition.parties if isinstance(party_or_coalition, Coalition) else [party_or_coalition] | ||
| 82 | doubling_currency_party = next((c for c in all_members if c.preferred_currency == currency), None) | 84 | doubling_currency_party = next((c for c in all_members if c.preferred_currency == currency), None) | ||
| 83 | votes = doubling_currency_party.convert_currency_to_voters(amount, currency) if doubling_currency_party else all_members[0].convert_currency_to_voters(amount, currency) | 85 | votes = doubling_currency_party.convert_currency_to_voters(amount, currency) if doubling_currency_party else all_members[0].convert_currency_to_voters(amount, currency) | ||
| 84 | self.candidates[party_or_coalition] = self.candidates.get(party_or_coalition, 0) + votes | 86 | self.candidates[party_or_coalition] = self.candidates.get(party_or_coalition, 0) + votes | ||
| 85 | 87 | ||||
| 86 | def get_results(self): | 88 | def get_results(self): | ||
| 87 | return {str(party_or_coalition): votes for party_or_coalition, votes in self.candidates.items()} | 89 | return {str(party_or_coalition): votes for party_or_coalition, votes in self.candidates.items()} | ||
| 88 | 90 | ||||
| 89 | @classmethod | 91 | @classmethod | ||
| 90 | def get_results_by_year(cls, year): | 92 | def get_results_by_year(cls, year): | ||
| 91 | return cls.history[year].get_results() | 93 | return cls.history[year].get_results() | ||
| 92 | 94 |
| Legends | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
| |||||||||
22.03.2026 17:03