1import re
2from collections import defaultdict
3
4
5def extract_wish(text):
6 match = re.search(r'["\']([a-zA-Z0-9 ]+)["\']', text)
7 return match.group(1)
8
9
10def extract_kid_id(text):
11 match = re.search(r'\b(\d+)\b', text)
12 return int(match.group(1))
13
14
15def track_exceptions(method): # as in the lection
16 def wrapper(self, *args, **kwargs):
17 try:
18 return method(self, *args, **kwargs)
19 except Exception:
20 self._threw_exception = True
21 raise
22 return wrapper
23
24
25def is_bad_kid(kid):
26 return kid._threw_exception
27
28
29class Santa(object):
30 def __new__(cls):
31 if not hasattr(cls, 'instance'):
32 cls.instance = super(Santa, cls).__new__(cls)
33 return cls.instance
34
35 def __init__(self):
36 self.wishes = {}
37 self.kids_ages = {}
38
39 def __call__(self, kid, talk):
40 wish = extract_wish(talk)
41 self.wishes[kid] = wish
42
43 def __matmul__(self, letter):
44 kid_id = extract_kid_id(letter)
45 kid = next((child for child in Kid.get_all_kids() if id(child) == kid_id), None)
46 wish = extract_wish(letter)
47 self.wishes[kid] = wish
48
49 def __iter__(self):
50 return iter(self.wishes.values()) # is this right
51
52 def xmas(self):
53 for every_kid in Kid.get_all_kids():
54 if every_kid not in self.kids_ages:
55 self.kids_ages[every_kid] = 1
56 else:
57 self.kids_ages[every_kid] += 1
58
59 if not self.wishes:
60 return
61
62 wish_counts = defaultdict(int)
63 for wish in self.wishes.values():
64 wish_counts[wish] += 1
65 most_wished = max(wish_counts.items(), key=lambda x: x[1])[0] # is this actually right
66
67 for every_kid in Kid.get_all_kids():
68 if self.kids_ages[every_kid] > 5:
69 continue
70
71 if is_bad_kid(every_kid):
72 every_kid("coal")
73
74 else:
75 wish = self.wishes.get(every_kid)
76 if wish:
77 every_kid(wish)
78 else:
79 every_kid(most_wished)
80
81 every_kid._threw_exception = False
82
83 self.wishes.clear()
84
85
86class Kid(type):
87 _all_kids = set()
88
89 def __new__(cls, name, bases, dct):
90 for attr_name, attr_value in dct.items():
91 if callable(attr_value) and not attr_name.startswith("_"):
92 dct[attr_name] = track_exceptions(attr_value)
93 return super().__new__(cls, name, bases, dct)
94
95 def __call__(cls, *args, **kwargs):
96 instance = super().__call__(*args, **kwargs)
97 Kid._all_kids.add(instance)
98 instance._threw_exception = False
99 if not callable(instance):
100 raise NotImplementedError("Стига де! Това все пак са деца! Трябва да можем да им викаме!")
101 return instance
102
103 @staticmethod
104 def get_all_kids():
105 return Kid._all_kids
.F......F...........
======================================================================
FAIL: test_class_from_kid_without_call_dunder (test.TestKid.test_class_from_kid_without_call_dunder)
Test creating new class from Kid.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 18, in test_class_from_kid_without_call_dunder
with self.assertRaises(NotImplementedError):
AssertionError: NotImplementedError not raised
======================================================================
FAIL: test_signature_matching (test.TestSanta.test_signature_matching)
Test matching present in the letter / call.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 119, in test_signature_matching
self.assertEqual(kid2.SECRET_PRESENT, 'toy2')
AssertionError: 'toy1' != 'toy2'
- toy1
? ^
+ toy2
? ^
----------------------------------------------------------------------
Ran 20 tests in 0.052s
FAILED (failures=2)
Виктор Бечев
19.12.2024 16:17Няма проблем, ние отбелязваме кой колко версии е качвал и накрая на семестъра ви пращаме сметка или фактура, по желание.
|
Валентина Петрова
19.12.2024 16:07Извинявам се за спама, но или компютърът, или сайтът ми бъгна.
|
t | 1 | import re | t | 1 | import re |
2 | from collections import defaultdict | 2 | from collections import defaultdict | ||
3 | 3 | ||||
4 | 4 | ||||
5 | def extract_wish(text): | 5 | def extract_wish(text): | ||
6 | match = re.search(r'["\']([a-zA-Z0-9 ]+)["\']', text) | 6 | match = re.search(r'["\']([a-zA-Z0-9 ]+)["\']', text) | ||
7 | return match.group(1) | 7 | return match.group(1) | ||
8 | 8 | ||||
9 | 9 | ||||
10 | def extract_kid_id(text): | 10 | def extract_kid_id(text): | ||
11 | match = re.search(r'\b(\d+)\b', text) | 11 | match = re.search(r'\b(\d+)\b', text) | ||
12 | return int(match.group(1)) | 12 | return int(match.group(1)) | ||
13 | 13 | ||||
14 | 14 | ||||
15 | def track_exceptions(method): # as in the lection | 15 | def track_exceptions(method): # as in the lection | ||
16 | def wrapper(self, *args, **kwargs): | 16 | def wrapper(self, *args, **kwargs): | ||
17 | try: | 17 | try: | ||
18 | return method(self, *args, **kwargs) | 18 | return method(self, *args, **kwargs) | ||
19 | except Exception: | 19 | except Exception: | ||
20 | self._threw_exception = True | 20 | self._threw_exception = True | ||
21 | raise | 21 | raise | ||
22 | return wrapper | 22 | return wrapper | ||
23 | 23 | ||||
24 | 24 | ||||
25 | def is_bad_kid(kid): | 25 | def is_bad_kid(kid): | ||
26 | return kid._threw_exception | 26 | return kid._threw_exception | ||
27 | 27 | ||||
28 | 28 | ||||
29 | class Santa(object): | 29 | class Santa(object): | ||
30 | def __new__(cls): | 30 | def __new__(cls): | ||
31 | if not hasattr(cls, 'instance'): | 31 | if not hasattr(cls, 'instance'): | ||
32 | cls.instance = super(Santa, cls).__new__(cls) | 32 | cls.instance = super(Santa, cls).__new__(cls) | ||
33 | return cls.instance | 33 | return cls.instance | ||
34 | 34 | ||||
35 | def __init__(self): | 35 | def __init__(self): | ||
36 | self.wishes = {} | 36 | self.wishes = {} | ||
37 | self.kids_ages = {} | 37 | self.kids_ages = {} | ||
38 | 38 | ||||
39 | def __call__(self, kid, talk): | 39 | def __call__(self, kid, talk): | ||
40 | wish = extract_wish(talk) | 40 | wish = extract_wish(talk) | ||
41 | self.wishes[kid] = wish | 41 | self.wishes[kid] = wish | ||
42 | 42 | ||||
43 | def __matmul__(self, letter): | 43 | def __matmul__(self, letter): | ||
44 | kid_id = extract_kid_id(letter) | 44 | kid_id = extract_kid_id(letter) | ||
45 | kid = next((child for child in Kid.get_all_kids() if id(child) == kid_id), None) | 45 | kid = next((child for child in Kid.get_all_kids() if id(child) == kid_id), None) | ||
46 | wish = extract_wish(letter) | 46 | wish = extract_wish(letter) | ||
47 | self.wishes[kid] = wish | 47 | self.wishes[kid] = wish | ||
48 | 48 | ||||
49 | def __iter__(self): | 49 | def __iter__(self): | ||
50 | return iter(self.wishes.values()) # is this right | 50 | return iter(self.wishes.values()) # is this right | ||
51 | 51 | ||||
52 | def xmas(self): | 52 | def xmas(self): | ||
53 | for every_kid in Kid.get_all_kids(): | 53 | for every_kid in Kid.get_all_kids(): | ||
54 | if every_kid not in self.kids_ages: | 54 | if every_kid not in self.kids_ages: | ||
55 | self.kids_ages[every_kid] = 1 | 55 | self.kids_ages[every_kid] = 1 | ||
56 | else: | 56 | else: | ||
57 | self.kids_ages[every_kid] += 1 | 57 | self.kids_ages[every_kid] += 1 | ||
58 | 58 | ||||
59 | if not self.wishes: | 59 | if not self.wishes: | ||
60 | return | 60 | return | ||
61 | 61 | ||||
62 | wish_counts = defaultdict(int) | 62 | wish_counts = defaultdict(int) | ||
63 | for wish in self.wishes.values(): | 63 | for wish in self.wishes.values(): | ||
64 | wish_counts[wish] += 1 | 64 | wish_counts[wish] += 1 | ||
65 | most_wished = max(wish_counts.items(), key=lambda x: x[1])[0] # is this actually right | 65 | most_wished = max(wish_counts.items(), key=lambda x: x[1])[0] # is this actually right | ||
66 | 66 | ||||
67 | for every_kid in Kid.get_all_kids(): | 67 | for every_kid in Kid.get_all_kids(): | ||
68 | if self.kids_ages[every_kid] > 5: | 68 | if self.kids_ages[every_kid] > 5: | ||
69 | continue | 69 | continue | ||
70 | 70 | ||||
71 | if is_bad_kid(every_kid): | 71 | if is_bad_kid(every_kid): | ||
72 | every_kid("coal") | 72 | every_kid("coal") | ||
73 | 73 | ||||
74 | else: | 74 | else: | ||
75 | wish = self.wishes.get(every_kid) | 75 | wish = self.wishes.get(every_kid) | ||
76 | if wish: | 76 | if wish: | ||
77 | every_kid(wish) | 77 | every_kid(wish) | ||
78 | else: | 78 | else: | ||
79 | every_kid(most_wished) | 79 | every_kid(most_wished) | ||
80 | 80 | ||||
81 | every_kid._threw_exception = False | 81 | every_kid._threw_exception = False | ||
82 | 82 | ||||
83 | self.wishes.clear() | 83 | self.wishes.clear() | ||
84 | 84 | ||||
85 | 85 | ||||
86 | class Kid(type): | 86 | class Kid(type): | ||
87 | _all_kids = set() | 87 | _all_kids = set() | ||
88 | 88 | ||||
89 | def __new__(cls, name, bases, dct): | 89 | def __new__(cls, name, bases, dct): | ||
90 | for attr_name, attr_value in dct.items(): | 90 | for attr_name, attr_value in dct.items(): | ||
91 | if callable(attr_value) and not attr_name.startswith("_"): | 91 | if callable(attr_value) and not attr_name.startswith("_"): | ||
92 | dct[attr_name] = track_exceptions(attr_value) | 92 | dct[attr_name] = track_exceptions(attr_value) | ||
93 | return super().__new__(cls, name, bases, dct) | 93 | return super().__new__(cls, name, bases, dct) | ||
94 | 94 | ||||
95 | def __call__(cls, *args, **kwargs): | 95 | def __call__(cls, *args, **kwargs): | ||
96 | instance = super().__call__(*args, **kwargs) | 96 | instance = super().__call__(*args, **kwargs) | ||
97 | Kid._all_kids.add(instance) | 97 | Kid._all_kids.add(instance) | ||
98 | instance._threw_exception = False | 98 | instance._threw_exception = False | ||
99 | if not callable(instance): | 99 | if not callable(instance): | ||
100 | raise NotImplementedError("Стига де! Това все пак са деца! Трябва да можем да им викаме!") | 100 | raise NotImplementedError("Стига де! Това все пак са деца! Трябва да можем да им викаме!") | ||
101 | return instance | 101 | return instance | ||
102 | 102 | ||||
103 | @staticmethod | 103 | @staticmethod | ||
104 | def get_all_kids(): | 104 | def get_all_kids(): | ||
105 | return Kid._all_kids | 105 | return Kid._all_kids |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
t | 1 | import re | t | 1 | import re |
2 | from collections import defaultdict | 2 | from collections import defaultdict | ||
3 | 3 | ||||
4 | 4 | ||||
5 | def extract_wish(text): | 5 | def extract_wish(text): | ||
6 | match = re.search(r'["\']([a-zA-Z0-9 ]+)["\']', text) | 6 | match = re.search(r'["\']([a-zA-Z0-9 ]+)["\']', text) | ||
7 | return match.group(1) | 7 | return match.group(1) | ||
8 | 8 | ||||
9 | 9 | ||||
10 | def extract_kid_id(text): | 10 | def extract_kid_id(text): | ||
11 | match = re.search(r'\b(\d+)\b', text) | 11 | match = re.search(r'\b(\d+)\b', text) | ||
12 | return int(match.group(1)) | 12 | return int(match.group(1)) | ||
13 | 13 | ||||
14 | 14 | ||||
15 | def track_exceptions(method): # as in the lection | 15 | def track_exceptions(method): # as in the lection | ||
16 | def wrapper(self, *args, **kwargs): | 16 | def wrapper(self, *args, **kwargs): | ||
17 | try: | 17 | try: | ||
18 | return method(self, *args, **kwargs) | 18 | return method(self, *args, **kwargs) | ||
19 | except Exception: | 19 | except Exception: | ||
20 | self._threw_exception = True | 20 | self._threw_exception = True | ||
21 | raise | 21 | raise | ||
22 | return wrapper | 22 | return wrapper | ||
23 | 23 | ||||
24 | 24 | ||||
25 | def is_bad_kid(kid): | 25 | def is_bad_kid(kid): | ||
26 | return kid._threw_exception | 26 | return kid._threw_exception | ||
27 | 27 | ||||
28 | 28 | ||||
29 | class Santa(object): | 29 | class Santa(object): | ||
30 | def __new__(cls): | 30 | def __new__(cls): | ||
31 | if not hasattr(cls, 'instance'): | 31 | if not hasattr(cls, 'instance'): | ||
32 | cls.instance = super(Santa, cls).__new__(cls) | 32 | cls.instance = super(Santa, cls).__new__(cls) | ||
33 | return cls.instance | 33 | return cls.instance | ||
34 | 34 | ||||
35 | def __init__(self): | 35 | def __init__(self): | ||
36 | self.wishes = {} | 36 | self.wishes = {} | ||
37 | self.kids_ages = {} | 37 | self.kids_ages = {} | ||
38 | 38 | ||||
39 | def __call__(self, kid, talk): | 39 | def __call__(self, kid, talk): | ||
40 | wish = extract_wish(talk) | 40 | wish = extract_wish(talk) | ||
41 | self.wishes[kid] = wish | 41 | self.wishes[kid] = wish | ||
42 | 42 | ||||
43 | def __matmul__(self, letter): | 43 | def __matmul__(self, letter): | ||
44 | kid_id = extract_kid_id(letter) | 44 | kid_id = extract_kid_id(letter) | ||
45 | kid = next((child for child in Kid.get_all_kids() if id(child) == kid_id), None) | 45 | kid = next((child for child in Kid.get_all_kids() if id(child) == kid_id), None) | ||
46 | wish = extract_wish(letter) | 46 | wish = extract_wish(letter) | ||
47 | self.wishes[kid] = wish | 47 | self.wishes[kid] = wish | ||
48 | 48 | ||||
49 | def __iter__(self): | 49 | def __iter__(self): | ||
50 | return iter(self.wishes.values()) # is this right | 50 | return iter(self.wishes.values()) # is this right | ||
51 | 51 | ||||
52 | def xmas(self): | 52 | def xmas(self): | ||
53 | for every_kid in Kid.get_all_kids(): | 53 | for every_kid in Kid.get_all_kids(): | ||
54 | if every_kid not in self.kids_ages: | 54 | if every_kid not in self.kids_ages: | ||
55 | self.kids_ages[every_kid] = 1 | 55 | self.kids_ages[every_kid] = 1 | ||
56 | else: | 56 | else: | ||
57 | self.kids_ages[every_kid] += 1 | 57 | self.kids_ages[every_kid] += 1 | ||
58 | 58 | ||||
59 | if not self.wishes: | 59 | if not self.wishes: | ||
60 | return | 60 | return | ||
61 | 61 | ||||
62 | wish_counts = defaultdict(int) | 62 | wish_counts = defaultdict(int) | ||
63 | for wish in self.wishes.values(): | 63 | for wish in self.wishes.values(): | ||
64 | wish_counts[wish] += 1 | 64 | wish_counts[wish] += 1 | ||
65 | most_wished = max(wish_counts.items(), key=lambda x: x[1])[0] # is this actually right | 65 | most_wished = max(wish_counts.items(), key=lambda x: x[1])[0] # is this actually right | ||
66 | 66 | ||||
67 | for every_kid in Kid.get_all_kids(): | 67 | for every_kid in Kid.get_all_kids(): | ||
68 | if self.kids_ages[every_kid] > 5: | 68 | if self.kids_ages[every_kid] > 5: | ||
69 | continue | 69 | continue | ||
70 | 70 | ||||
71 | if is_bad_kid(every_kid): | 71 | if is_bad_kid(every_kid): | ||
72 | every_kid("coal") | 72 | every_kid("coal") | ||
73 | 73 | ||||
74 | else: | 74 | else: | ||
75 | wish = self.wishes.get(every_kid) | 75 | wish = self.wishes.get(every_kid) | ||
76 | if wish: | 76 | if wish: | ||
77 | every_kid(wish) | 77 | every_kid(wish) | ||
78 | else: | 78 | else: | ||
79 | every_kid(most_wished) | 79 | every_kid(most_wished) | ||
80 | 80 | ||||
81 | every_kid._threw_exception = False | 81 | every_kid._threw_exception = False | ||
82 | 82 | ||||
83 | self.wishes.clear() | 83 | self.wishes.clear() | ||
84 | 84 | ||||
85 | 85 | ||||
86 | class Kid(type): | 86 | class Kid(type): | ||
87 | _all_kids = set() | 87 | _all_kids = set() | ||
88 | 88 | ||||
89 | def __new__(cls, name, bases, dct): | 89 | def __new__(cls, name, bases, dct): | ||
90 | for attr_name, attr_value in dct.items(): | 90 | for attr_name, attr_value in dct.items(): | ||
91 | if callable(attr_value) and not attr_name.startswith("_"): | 91 | if callable(attr_value) and not attr_name.startswith("_"): | ||
92 | dct[attr_name] = track_exceptions(attr_value) | 92 | dct[attr_name] = track_exceptions(attr_value) | ||
93 | return super().__new__(cls, name, bases, dct) | 93 | return super().__new__(cls, name, bases, dct) | ||
94 | 94 | ||||
95 | def __call__(cls, *args, **kwargs): | 95 | def __call__(cls, *args, **kwargs): | ||
96 | instance = super().__call__(*args, **kwargs) | 96 | instance = super().__call__(*args, **kwargs) | ||
97 | Kid._all_kids.add(instance) | 97 | Kid._all_kids.add(instance) | ||
98 | instance._threw_exception = False | 98 | instance._threw_exception = False | ||
99 | if not callable(instance): | 99 | if not callable(instance): | ||
100 | raise NotImplementedError("Стига де! Това все пак са деца! Трябва да можем да им викаме!") | 100 | raise NotImplementedError("Стига де! Това все пак са деца! Трябва да можем да им викаме!") | ||
101 | return instance | 101 | return instance | ||
102 | 102 | ||||
103 | @staticmethod | 103 | @staticmethod | ||
104 | def get_all_kids(): | 104 | def get_all_kids(): | ||
105 | return Kid._all_kids | 105 | return Kid._all_kids |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
t | 1 | import re | t | 1 | import re |
2 | from collections import defaultdict | 2 | from collections import defaultdict | ||
3 | 3 | ||||
4 | 4 | ||||
5 | def extract_wish(text): | 5 | def extract_wish(text): | ||
6 | match = re.search(r'["\']([a-zA-Z0-9 ]+)["\']', text) | 6 | match = re.search(r'["\']([a-zA-Z0-9 ]+)["\']', text) | ||
7 | return match.group(1) | 7 | return match.group(1) | ||
8 | 8 | ||||
9 | 9 | ||||
10 | def extract_kid_id(text): | 10 | def extract_kid_id(text): | ||
11 | match = re.search(r'\b(\d+)\b', text) | 11 | match = re.search(r'\b(\d+)\b', text) | ||
12 | return int(match.group(1)) | 12 | return int(match.group(1)) | ||
13 | 13 | ||||
14 | 14 | ||||
15 | def track_exceptions(method): # as in the lection | 15 | def track_exceptions(method): # as in the lection | ||
16 | def wrapper(self, *args, **kwargs): | 16 | def wrapper(self, *args, **kwargs): | ||
17 | try: | 17 | try: | ||
18 | return method(self, *args, **kwargs) | 18 | return method(self, *args, **kwargs) | ||
19 | except Exception: | 19 | except Exception: | ||
20 | self._threw_exception = True | 20 | self._threw_exception = True | ||
21 | raise | 21 | raise | ||
22 | return wrapper | 22 | return wrapper | ||
23 | 23 | ||||
24 | 24 | ||||
25 | def is_bad_kid(kid): | 25 | def is_bad_kid(kid): | ||
26 | return kid._threw_exception | 26 | return kid._threw_exception | ||
27 | 27 | ||||
28 | 28 | ||||
29 | class Santa(object): | 29 | class Santa(object): | ||
30 | def __new__(cls): | 30 | def __new__(cls): | ||
31 | if not hasattr(cls, 'instance'): | 31 | if not hasattr(cls, 'instance'): | ||
32 | cls.instance = super(Santa, cls).__new__(cls) | 32 | cls.instance = super(Santa, cls).__new__(cls) | ||
33 | return cls.instance | 33 | return cls.instance | ||
34 | 34 | ||||
35 | def __init__(self): | 35 | def __init__(self): | ||
36 | self.wishes = {} | 36 | self.wishes = {} | ||
37 | self.kids_ages = {} | 37 | self.kids_ages = {} | ||
38 | 38 | ||||
39 | def __call__(self, kid, talk): | 39 | def __call__(self, kid, talk): | ||
40 | wish = extract_wish(talk) | 40 | wish = extract_wish(talk) | ||
41 | self.wishes[kid] = wish | 41 | self.wishes[kid] = wish | ||
42 | 42 | ||||
43 | def __matmul__(self, letter): | 43 | def __matmul__(self, letter): | ||
44 | kid_id = extract_kid_id(letter) | 44 | kid_id = extract_kid_id(letter) | ||
45 | kid = next((child for child in Kid.get_all_kids() if id(child) == kid_id), None) | 45 | kid = next((child for child in Kid.get_all_kids() if id(child) == kid_id), None) | ||
46 | wish = extract_wish(letter) | 46 | wish = extract_wish(letter) | ||
47 | self.wishes[kid] = wish | 47 | self.wishes[kid] = wish | ||
48 | 48 | ||||
49 | def __iter__(self): | 49 | def __iter__(self): | ||
50 | return iter(self.wishes.values()) # is this right | 50 | return iter(self.wishes.values()) # is this right | ||
51 | 51 | ||||
52 | def xmas(self): | 52 | def xmas(self): | ||
53 | for every_kid in Kid.get_all_kids(): | 53 | for every_kid in Kid.get_all_kids(): | ||
54 | if every_kid not in self.kids_ages: | 54 | if every_kid not in self.kids_ages: | ||
55 | self.kids_ages[every_kid] = 1 | 55 | self.kids_ages[every_kid] = 1 | ||
56 | else: | 56 | else: | ||
57 | self.kids_ages[every_kid] += 1 | 57 | self.kids_ages[every_kid] += 1 | ||
58 | 58 | ||||
59 | if not self.wishes: | 59 | if not self.wishes: | ||
60 | return | 60 | return | ||
61 | 61 | ||||
62 | wish_counts = defaultdict(int) | 62 | wish_counts = defaultdict(int) | ||
63 | for wish in self.wishes.values(): | 63 | for wish in self.wishes.values(): | ||
64 | wish_counts[wish] += 1 | 64 | wish_counts[wish] += 1 | ||
65 | most_wished = max(wish_counts.items(), key=lambda x: x[1])[0] # is this actually right | 65 | most_wished = max(wish_counts.items(), key=lambda x: x[1])[0] # is this actually right | ||
66 | 66 | ||||
67 | for every_kid in Kid.get_all_kids(): | 67 | for every_kid in Kid.get_all_kids(): | ||
68 | if self.kids_ages[every_kid] > 5: | 68 | if self.kids_ages[every_kid] > 5: | ||
69 | continue | 69 | continue | ||
70 | 70 | ||||
71 | if is_bad_kid(every_kid): | 71 | if is_bad_kid(every_kid): | ||
72 | every_kid("coal") | 72 | every_kid("coal") | ||
73 | 73 | ||||
74 | else: | 74 | else: | ||
75 | wish = self.wishes.get(every_kid) | 75 | wish = self.wishes.get(every_kid) | ||
76 | if wish: | 76 | if wish: | ||
77 | every_kid(wish) | 77 | every_kid(wish) | ||
78 | else: | 78 | else: | ||
79 | every_kid(most_wished) | 79 | every_kid(most_wished) | ||
80 | 80 | ||||
81 | every_kid._threw_exception = False | 81 | every_kid._threw_exception = False | ||
82 | 82 | ||||
83 | self.wishes.clear() | 83 | self.wishes.clear() | ||
84 | 84 | ||||
85 | 85 | ||||
86 | class Kid(type): | 86 | class Kid(type): | ||
87 | _all_kids = set() | 87 | _all_kids = set() | ||
88 | 88 | ||||
89 | def __new__(cls, name, bases, dct): | 89 | def __new__(cls, name, bases, dct): | ||
90 | for attr_name, attr_value in dct.items(): | 90 | for attr_name, attr_value in dct.items(): | ||
91 | if callable(attr_value) and not attr_name.startswith("_"): | 91 | if callable(attr_value) and not attr_name.startswith("_"): | ||
92 | dct[attr_name] = track_exceptions(attr_value) | 92 | dct[attr_name] = track_exceptions(attr_value) | ||
93 | return super().__new__(cls, name, bases, dct) | 93 | return super().__new__(cls, name, bases, dct) | ||
94 | 94 | ||||
95 | def __call__(cls, *args, **kwargs): | 95 | def __call__(cls, *args, **kwargs): | ||
96 | instance = super().__call__(*args, **kwargs) | 96 | instance = super().__call__(*args, **kwargs) | ||
97 | Kid._all_kids.add(instance) | 97 | Kid._all_kids.add(instance) | ||
98 | instance._threw_exception = False | 98 | instance._threw_exception = False | ||
99 | if not callable(instance): | 99 | if not callable(instance): | ||
100 | raise NotImplementedError("Стига де! Това все пак са деца! Трябва да можем да им викаме!") | 100 | raise NotImplementedError("Стига де! Това все пак са деца! Трябва да можем да им викаме!") | ||
101 | return instance | 101 | return instance | ||
102 | 102 | ||||
103 | @staticmethod | 103 | @staticmethod | ||
104 | def get_all_kids(): | 104 | def get_all_kids(): | ||
105 | return Kid._all_kids | 105 | return Kid._all_kids |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
t | 1 | import re | t | 1 | import re |
2 | from collections import defaultdict | 2 | from collections import defaultdict | ||
3 | 3 | ||||
4 | 4 | ||||
5 | def extract_wish(text): | 5 | def extract_wish(text): | ||
6 | match = re.search(r'["\']([a-zA-Z0-9 ]+)["\']', text) | 6 | match = re.search(r'["\']([a-zA-Z0-9 ]+)["\']', text) | ||
7 | return match.group(1) | 7 | return match.group(1) | ||
8 | 8 | ||||
9 | 9 | ||||
10 | def extract_kid_id(text): | 10 | def extract_kid_id(text): | ||
11 | match = re.search(r'\b(\d+)\b', text) | 11 | match = re.search(r'\b(\d+)\b', text) | ||
12 | return int(match.group(1)) | 12 | return int(match.group(1)) | ||
13 | 13 | ||||
14 | 14 | ||||
15 | def track_exceptions(method): # as in the lection | 15 | def track_exceptions(method): # as in the lection | ||
16 | def wrapper(self, *args, **kwargs): | 16 | def wrapper(self, *args, **kwargs): | ||
17 | try: | 17 | try: | ||
18 | return method(self, *args, **kwargs) | 18 | return method(self, *args, **kwargs) | ||
19 | except Exception: | 19 | except Exception: | ||
20 | self._threw_exception = True | 20 | self._threw_exception = True | ||
21 | raise | 21 | raise | ||
22 | return wrapper | 22 | return wrapper | ||
23 | 23 | ||||
24 | 24 | ||||
25 | def is_bad_kid(kid): | 25 | def is_bad_kid(kid): | ||
26 | return kid._threw_exception | 26 | return kid._threw_exception | ||
27 | 27 | ||||
28 | 28 | ||||
29 | class Santa(object): | 29 | class Santa(object): | ||
30 | def __new__(cls): | 30 | def __new__(cls): | ||
31 | if not hasattr(cls, 'instance'): | 31 | if not hasattr(cls, 'instance'): | ||
32 | cls.instance = super(Santa, cls).__new__(cls) | 32 | cls.instance = super(Santa, cls).__new__(cls) | ||
33 | return cls.instance | 33 | return cls.instance | ||
34 | 34 | ||||
35 | def __init__(self): | 35 | def __init__(self): | ||
36 | self.wishes = {} | 36 | self.wishes = {} | ||
37 | self.kids_ages = {} | 37 | self.kids_ages = {} | ||
38 | 38 | ||||
39 | def __call__(self, kid, talk): | 39 | def __call__(self, kid, talk): | ||
40 | wish = extract_wish(talk) | 40 | wish = extract_wish(talk) | ||
41 | self.wishes[kid] = wish | 41 | self.wishes[kid] = wish | ||
42 | 42 | ||||
43 | def __matmul__(self, letter): | 43 | def __matmul__(self, letter): | ||
44 | kid_id = extract_kid_id(letter) | 44 | kid_id = extract_kid_id(letter) | ||
45 | kid = next((child for child in Kid.get_all_kids() if id(child) == kid_id), None) | 45 | kid = next((child for child in Kid.get_all_kids() if id(child) == kid_id), None) | ||
46 | wish = extract_wish(letter) | 46 | wish = extract_wish(letter) | ||
47 | self.wishes[kid] = wish | 47 | self.wishes[kid] = wish | ||
48 | 48 | ||||
49 | def __iter__(self): | 49 | def __iter__(self): | ||
50 | return iter(self.wishes.values()) # is this right | 50 | return iter(self.wishes.values()) # is this right | ||
51 | 51 | ||||
52 | def xmas(self): | 52 | def xmas(self): | ||
53 | for every_kid in Kid.get_all_kids(): | 53 | for every_kid in Kid.get_all_kids(): | ||
54 | if every_kid not in self.kids_ages: | 54 | if every_kid not in self.kids_ages: | ||
55 | self.kids_ages[every_kid] = 1 | 55 | self.kids_ages[every_kid] = 1 | ||
56 | else: | 56 | else: | ||
57 | self.kids_ages[every_kid] += 1 | 57 | self.kids_ages[every_kid] += 1 | ||
58 | 58 | ||||
59 | if not self.wishes: | 59 | if not self.wishes: | ||
60 | return | 60 | return | ||
61 | 61 | ||||
62 | wish_counts = defaultdict(int) | 62 | wish_counts = defaultdict(int) | ||
63 | for wish in self.wishes.values(): | 63 | for wish in self.wishes.values(): | ||
64 | wish_counts[wish] += 1 | 64 | wish_counts[wish] += 1 | ||
65 | most_wished = max(wish_counts.items(), key=lambda x: x[1])[0] # is this actually right | 65 | most_wished = max(wish_counts.items(), key=lambda x: x[1])[0] # is this actually right | ||
66 | 66 | ||||
67 | for every_kid in Kid.get_all_kids(): | 67 | for every_kid in Kid.get_all_kids(): | ||
68 | if self.kids_ages[every_kid] > 5: | 68 | if self.kids_ages[every_kid] > 5: | ||
69 | continue | 69 | continue | ||
70 | 70 | ||||
71 | if is_bad_kid(every_kid): | 71 | if is_bad_kid(every_kid): | ||
72 | every_kid("coal") | 72 | every_kid("coal") | ||
73 | 73 | ||||
74 | else: | 74 | else: | ||
75 | wish = self.wishes.get(every_kid) | 75 | wish = self.wishes.get(every_kid) | ||
76 | if wish: | 76 | if wish: | ||
77 | every_kid(wish) | 77 | every_kid(wish) | ||
78 | else: | 78 | else: | ||
79 | every_kid(most_wished) | 79 | every_kid(most_wished) | ||
80 | 80 | ||||
81 | every_kid._threw_exception = False | 81 | every_kid._threw_exception = False | ||
82 | 82 | ||||
83 | self.wishes.clear() | 83 | self.wishes.clear() | ||
84 | 84 | ||||
85 | 85 | ||||
86 | class Kid(type): | 86 | class Kid(type): | ||
87 | _all_kids = set() | 87 | _all_kids = set() | ||
88 | 88 | ||||
89 | def __new__(cls, name, bases, dct): | 89 | def __new__(cls, name, bases, dct): | ||
90 | for attr_name, attr_value in dct.items(): | 90 | for attr_name, attr_value in dct.items(): | ||
91 | if callable(attr_value) and not attr_name.startswith("_"): | 91 | if callable(attr_value) and not attr_name.startswith("_"): | ||
92 | dct[attr_name] = track_exceptions(attr_value) | 92 | dct[attr_name] = track_exceptions(attr_value) | ||
93 | return super().__new__(cls, name, bases, dct) | 93 | return super().__new__(cls, name, bases, dct) | ||
94 | 94 | ||||
95 | def __call__(cls, *args, **kwargs): | 95 | def __call__(cls, *args, **kwargs): | ||
96 | instance = super().__call__(*args, **kwargs) | 96 | instance = super().__call__(*args, **kwargs) | ||
97 | Kid._all_kids.add(instance) | 97 | Kid._all_kids.add(instance) | ||
98 | instance._threw_exception = False | 98 | instance._threw_exception = False | ||
99 | if not callable(instance): | 99 | if not callable(instance): | ||
100 | raise NotImplementedError("Стига де! Това все пак са деца! Трябва да можем да им викаме!") | 100 | raise NotImplementedError("Стига де! Това все пак са деца! Трябва да можем да им викаме!") | ||
101 | return instance | 101 | return instance | ||
102 | 102 | ||||
103 | @staticmethod | 103 | @staticmethod | ||
104 | def get_all_kids(): | 104 | def get_all_kids(): | ||
105 | return Kid._all_kids | 105 | return Kid._all_kids |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
t | 1 | import re | t | 1 | import re |
2 | from collections import defaultdict | 2 | from collections import defaultdict | ||
3 | 3 | ||||
4 | 4 | ||||
5 | def extract_wish(text): | 5 | def extract_wish(text): | ||
6 | match = re.search(r'["\']([a-zA-Z0-9 ]+)["\']', text) | 6 | match = re.search(r'["\']([a-zA-Z0-9 ]+)["\']', text) | ||
7 | return match.group(1) | 7 | return match.group(1) | ||
8 | 8 | ||||
9 | 9 | ||||
10 | def extract_kid_id(text): | 10 | def extract_kid_id(text): | ||
11 | match = re.search(r'\b(\d+)\b', text) | 11 | match = re.search(r'\b(\d+)\b', text) | ||
12 | return int(match.group(1)) | 12 | return int(match.group(1)) | ||
13 | 13 | ||||
14 | 14 | ||||
15 | def track_exceptions(method): # as in the lection | 15 | def track_exceptions(method): # as in the lection | ||
16 | def wrapper(self, *args, **kwargs): | 16 | def wrapper(self, *args, **kwargs): | ||
17 | try: | 17 | try: | ||
18 | return method(self, *args, **kwargs) | 18 | return method(self, *args, **kwargs) | ||
19 | except Exception: | 19 | except Exception: | ||
20 | self._threw_exception = True | 20 | self._threw_exception = True | ||
21 | raise | 21 | raise | ||
22 | return wrapper | 22 | return wrapper | ||
23 | 23 | ||||
24 | 24 | ||||
25 | def is_bad_kid(kid): | 25 | def is_bad_kid(kid): | ||
26 | return kid._threw_exception | 26 | return kid._threw_exception | ||
27 | 27 | ||||
28 | 28 | ||||
29 | class Santa(object): | 29 | class Santa(object): | ||
30 | def __new__(cls): | 30 | def __new__(cls): | ||
31 | if not hasattr(cls, 'instance'): | 31 | if not hasattr(cls, 'instance'): | ||
32 | cls.instance = super(Santa, cls).__new__(cls) | 32 | cls.instance = super(Santa, cls).__new__(cls) | ||
33 | return cls.instance | 33 | return cls.instance | ||
34 | 34 | ||||
35 | def __init__(self): | 35 | def __init__(self): | ||
36 | self.wishes = {} | 36 | self.wishes = {} | ||
37 | self.kids_ages = {} | 37 | self.kids_ages = {} | ||
38 | 38 | ||||
39 | def __call__(self, kid, talk): | 39 | def __call__(self, kid, talk): | ||
40 | wish = extract_wish(talk) | 40 | wish = extract_wish(talk) | ||
41 | self.wishes[kid] = wish | 41 | self.wishes[kid] = wish | ||
42 | 42 | ||||
43 | def __matmul__(self, letter): | 43 | def __matmul__(self, letter): | ||
44 | kid_id = extract_kid_id(letter) | 44 | kid_id = extract_kid_id(letter) | ||
45 | kid = next((child for child in Kid.get_all_kids() if id(child) == kid_id), None) | 45 | kid = next((child for child in Kid.get_all_kids() if id(child) == kid_id), None) | ||
46 | wish = extract_wish(letter) | 46 | wish = extract_wish(letter) | ||
47 | self.wishes[kid] = wish | 47 | self.wishes[kid] = wish | ||
48 | 48 | ||||
49 | def __iter__(self): | 49 | def __iter__(self): | ||
50 | return iter(self.wishes.values()) # is this right | 50 | return iter(self.wishes.values()) # is this right | ||
51 | 51 | ||||
52 | def xmas(self): | 52 | def xmas(self): | ||
53 | for every_kid in Kid.get_all_kids(): | 53 | for every_kid in Kid.get_all_kids(): | ||
54 | if every_kid not in self.kids_ages: | 54 | if every_kid not in self.kids_ages: | ||
55 | self.kids_ages[every_kid] = 1 | 55 | self.kids_ages[every_kid] = 1 | ||
56 | else: | 56 | else: | ||
57 | self.kids_ages[every_kid] += 1 | 57 | self.kids_ages[every_kid] += 1 | ||
58 | 58 | ||||
59 | if not self.wishes: | 59 | if not self.wishes: | ||
60 | return | 60 | return | ||
61 | 61 | ||||
62 | wish_counts = defaultdict(int) | 62 | wish_counts = defaultdict(int) | ||
63 | for wish in self.wishes.values(): | 63 | for wish in self.wishes.values(): | ||
64 | wish_counts[wish] += 1 | 64 | wish_counts[wish] += 1 | ||
65 | most_wished = max(wish_counts.items(), key=lambda x: x[1])[0] # is this actually right | 65 | most_wished = max(wish_counts.items(), key=lambda x: x[1])[0] # is this actually right | ||
66 | 66 | ||||
67 | for every_kid in Kid.get_all_kids(): | 67 | for every_kid in Kid.get_all_kids(): | ||
68 | if self.kids_ages[every_kid] > 5: | 68 | if self.kids_ages[every_kid] > 5: | ||
69 | continue | 69 | continue | ||
70 | 70 | ||||
71 | if is_bad_kid(every_kid): | 71 | if is_bad_kid(every_kid): | ||
72 | every_kid("coal") | 72 | every_kid("coal") | ||
73 | 73 | ||||
74 | else: | 74 | else: | ||
75 | wish = self.wishes.get(every_kid) | 75 | wish = self.wishes.get(every_kid) | ||
76 | if wish: | 76 | if wish: | ||
77 | every_kid(wish) | 77 | every_kid(wish) | ||
78 | else: | 78 | else: | ||
79 | every_kid(most_wished) | 79 | every_kid(most_wished) | ||
80 | 80 | ||||
81 | every_kid._threw_exception = False | 81 | every_kid._threw_exception = False | ||
82 | 82 | ||||
83 | self.wishes.clear() | 83 | self.wishes.clear() | ||
84 | 84 | ||||
85 | 85 | ||||
86 | class Kid(type): | 86 | class Kid(type): | ||
87 | _all_kids = set() | 87 | _all_kids = set() | ||
88 | 88 | ||||
89 | def __new__(cls, name, bases, dct): | 89 | def __new__(cls, name, bases, dct): | ||
90 | for attr_name, attr_value in dct.items(): | 90 | for attr_name, attr_value in dct.items(): | ||
91 | if callable(attr_value) and not attr_name.startswith("_"): | 91 | if callable(attr_value) and not attr_name.startswith("_"): | ||
92 | dct[attr_name] = track_exceptions(attr_value) | 92 | dct[attr_name] = track_exceptions(attr_value) | ||
93 | return super().__new__(cls, name, bases, dct) | 93 | return super().__new__(cls, name, bases, dct) | ||
94 | 94 | ||||
95 | def __call__(cls, *args, **kwargs): | 95 | def __call__(cls, *args, **kwargs): | ||
96 | instance = super().__call__(*args, **kwargs) | 96 | instance = super().__call__(*args, **kwargs) | ||
97 | Kid._all_kids.add(instance) | 97 | Kid._all_kids.add(instance) | ||
98 | instance._threw_exception = False | 98 | instance._threw_exception = False | ||
99 | if not callable(instance): | 99 | if not callable(instance): | ||
100 | raise NotImplementedError("Стига де! Това все пак са деца! Трябва да можем да им викаме!") | 100 | raise NotImplementedError("Стига де! Това все пак са деца! Трябва да можем да им викаме!") | ||
101 | return instance | 101 | return instance | ||
102 | 102 | ||||
103 | @staticmethod | 103 | @staticmethod | ||
104 | def get_all_kids(): | 104 | def get_all_kids(): | ||
105 | return Kid._all_kids | 105 | return Kid._all_kids |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
t | 1 | import re | t | 1 | import re |
2 | from collections import defaultdict | 2 | from collections import defaultdict | ||
3 | 3 | ||||
4 | 4 | ||||
5 | def extract_wish(text): | 5 | def extract_wish(text): | ||
6 | match = re.search(r'["\']([a-zA-Z0-9 ]+)["\']', text) | 6 | match = re.search(r'["\']([a-zA-Z0-9 ]+)["\']', text) | ||
7 | return match.group(1) | 7 | return match.group(1) | ||
8 | 8 | ||||
9 | 9 | ||||
10 | def extract_kid_id(text): | 10 | def extract_kid_id(text): | ||
11 | match = re.search(r'\b(\d+)\b', text) | 11 | match = re.search(r'\b(\d+)\b', text) | ||
12 | return int(match.group(1)) | 12 | return int(match.group(1)) | ||
13 | 13 | ||||
14 | 14 | ||||
15 | def track_exceptions(method): # as in the lection | 15 | def track_exceptions(method): # as in the lection | ||
16 | def wrapper(self, *args, **kwargs): | 16 | def wrapper(self, *args, **kwargs): | ||
17 | try: | 17 | try: | ||
18 | return method(self, *args, **kwargs) | 18 | return method(self, *args, **kwargs) | ||
19 | except Exception: | 19 | except Exception: | ||
20 | self._threw_exception = True | 20 | self._threw_exception = True | ||
21 | raise | 21 | raise | ||
22 | return wrapper | 22 | return wrapper | ||
23 | 23 | ||||
24 | 24 | ||||
25 | def is_bad_kid(kid): | 25 | def is_bad_kid(kid): | ||
26 | return kid._threw_exception | 26 | return kid._threw_exception | ||
27 | 27 | ||||
28 | 28 | ||||
29 | class Santa(object): | 29 | class Santa(object): | ||
30 | def __new__(cls): | 30 | def __new__(cls): | ||
31 | if not hasattr(cls, 'instance'): | 31 | if not hasattr(cls, 'instance'): | ||
32 | cls.instance = super(Santa, cls).__new__(cls) | 32 | cls.instance = super(Santa, cls).__new__(cls) | ||
33 | return cls.instance | 33 | return cls.instance | ||
34 | 34 | ||||
35 | def __init__(self): | 35 | def __init__(self): | ||
36 | self.wishes = {} | 36 | self.wishes = {} | ||
37 | self.kids_ages = {} | 37 | self.kids_ages = {} | ||
38 | 38 | ||||
39 | def __call__(self, kid, talk): | 39 | def __call__(self, kid, talk): | ||
40 | wish = extract_wish(talk) | 40 | wish = extract_wish(talk) | ||
41 | self.wishes[kid] = wish | 41 | self.wishes[kid] = wish | ||
42 | 42 | ||||
43 | def __matmul__(self, letter): | 43 | def __matmul__(self, letter): | ||
44 | kid_id = extract_kid_id(letter) | 44 | kid_id = extract_kid_id(letter) | ||
45 | kid = next((child for child in Kid.get_all_kids() if id(child) == kid_id), None) | 45 | kid = next((child for child in Kid.get_all_kids() if id(child) == kid_id), None) | ||
46 | wish = extract_wish(letter) | 46 | wish = extract_wish(letter) | ||
47 | self.wishes[kid] = wish | 47 | self.wishes[kid] = wish | ||
48 | 48 | ||||
49 | def __iter__(self): | 49 | def __iter__(self): | ||
50 | return iter(self.wishes.values()) # is this right | 50 | return iter(self.wishes.values()) # is this right | ||
51 | 51 | ||||
52 | def xmas(self): | 52 | def xmas(self): | ||
53 | for every_kid in Kid.get_all_kids(): | 53 | for every_kid in Kid.get_all_kids(): | ||
54 | if every_kid not in self.kids_ages: | 54 | if every_kid not in self.kids_ages: | ||
55 | self.kids_ages[every_kid] = 1 | 55 | self.kids_ages[every_kid] = 1 | ||
56 | else: | 56 | else: | ||
57 | self.kids_ages[every_kid] += 1 | 57 | self.kids_ages[every_kid] += 1 | ||
58 | 58 | ||||
59 | if not self.wishes: | 59 | if not self.wishes: | ||
60 | return | 60 | return | ||
61 | 61 | ||||
62 | wish_counts = defaultdict(int) | 62 | wish_counts = defaultdict(int) | ||
63 | for wish in self.wishes.values(): | 63 | for wish in self.wishes.values(): | ||
64 | wish_counts[wish] += 1 | 64 | wish_counts[wish] += 1 | ||
65 | most_wished = max(wish_counts.items(), key=lambda x: x[1])[0] # is this actually right | 65 | most_wished = max(wish_counts.items(), key=lambda x: x[1])[0] # is this actually right | ||
66 | 66 | ||||
67 | for every_kid in Kid.get_all_kids(): | 67 | for every_kid in Kid.get_all_kids(): | ||
68 | if self.kids_ages[every_kid] > 5: | 68 | if self.kids_ages[every_kid] > 5: | ||
69 | continue | 69 | continue | ||
70 | 70 | ||||
71 | if is_bad_kid(every_kid): | 71 | if is_bad_kid(every_kid): | ||
72 | every_kid("coal") | 72 | every_kid("coal") | ||
73 | 73 | ||||
74 | else: | 74 | else: | ||
75 | wish = self.wishes.get(every_kid) | 75 | wish = self.wishes.get(every_kid) | ||
76 | if wish: | 76 | if wish: | ||
77 | every_kid(wish) | 77 | every_kid(wish) | ||
78 | else: | 78 | else: | ||
79 | every_kid(most_wished) | 79 | every_kid(most_wished) | ||
80 | 80 | ||||
81 | every_kid._threw_exception = False | 81 | every_kid._threw_exception = False | ||
82 | 82 | ||||
83 | self.wishes.clear() | 83 | self.wishes.clear() | ||
84 | 84 | ||||
85 | 85 | ||||
86 | class Kid(type): | 86 | class Kid(type): | ||
87 | _all_kids = set() | 87 | _all_kids = set() | ||
88 | 88 | ||||
89 | def __new__(cls, name, bases, dct): | 89 | def __new__(cls, name, bases, dct): | ||
90 | for attr_name, attr_value in dct.items(): | 90 | for attr_name, attr_value in dct.items(): | ||
91 | if callable(attr_value) and not attr_name.startswith("_"): | 91 | if callable(attr_value) and not attr_name.startswith("_"): | ||
92 | dct[attr_name] = track_exceptions(attr_value) | 92 | dct[attr_name] = track_exceptions(attr_value) | ||
93 | return super().__new__(cls, name, bases, dct) | 93 | return super().__new__(cls, name, bases, dct) | ||
94 | 94 | ||||
95 | def __call__(cls, *args, **kwargs): | 95 | def __call__(cls, *args, **kwargs): | ||
96 | instance = super().__call__(*args, **kwargs) | 96 | instance = super().__call__(*args, **kwargs) | ||
97 | Kid._all_kids.add(instance) | 97 | Kid._all_kids.add(instance) | ||
98 | instance._threw_exception = False | 98 | instance._threw_exception = False | ||
99 | if not callable(instance): | 99 | if not callable(instance): | ||
100 | raise NotImplementedError("Стига де! Това все пак са деца! Трябва да можем да им викаме!") | 100 | raise NotImplementedError("Стига де! Това все пак са деца! Трябва да можем да им викаме!") | ||
101 | return instance | 101 | return instance | ||
102 | 102 | ||||
103 | @staticmethod | 103 | @staticmethod | ||
104 | def get_all_kids(): | 104 | def get_all_kids(): | ||
105 | return Kid._all_kids | 105 | return Kid._all_kids |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
t | 1 | import re | t | 1 | import re |
2 | from collections import defaultdict | 2 | from collections import defaultdict | ||
3 | 3 | ||||
4 | 4 | ||||
5 | def extract_wish(text): | 5 | def extract_wish(text): | ||
6 | match = re.search(r'["\']([a-zA-Z0-9 ]+)["\']', text) | 6 | match = re.search(r'["\']([a-zA-Z0-9 ]+)["\']', text) | ||
7 | return match.group(1) | 7 | return match.group(1) | ||
8 | 8 | ||||
9 | 9 | ||||
10 | def extract_kid_id(text): | 10 | def extract_kid_id(text): | ||
11 | match = re.search(r'\b(\d+)\b', text) | 11 | match = re.search(r'\b(\d+)\b', text) | ||
12 | return int(match.group(1)) | 12 | return int(match.group(1)) | ||
13 | 13 | ||||
14 | 14 | ||||
15 | def track_exceptions(method): # as in the lection | 15 | def track_exceptions(method): # as in the lection | ||
16 | def wrapper(self, *args, **kwargs): | 16 | def wrapper(self, *args, **kwargs): | ||
17 | try: | 17 | try: | ||
18 | return method(self, *args, **kwargs) | 18 | return method(self, *args, **kwargs) | ||
19 | except Exception: | 19 | except Exception: | ||
20 | self._threw_exception = True | 20 | self._threw_exception = True | ||
21 | raise | 21 | raise | ||
22 | return wrapper | 22 | return wrapper | ||
23 | 23 | ||||
24 | 24 | ||||
25 | def is_bad_kid(kid): | 25 | def is_bad_kid(kid): | ||
26 | return kid._threw_exception | 26 | return kid._threw_exception | ||
27 | 27 | ||||
28 | 28 | ||||
29 | class Santa(object): | 29 | class Santa(object): | ||
30 | def __new__(cls): | 30 | def __new__(cls): | ||
31 | if not hasattr(cls, 'instance'): | 31 | if not hasattr(cls, 'instance'): | ||
32 | cls.instance = super(Santa, cls).__new__(cls) | 32 | cls.instance = super(Santa, cls).__new__(cls) | ||
33 | return cls.instance | 33 | return cls.instance | ||
34 | 34 | ||||
35 | def __init__(self): | 35 | def __init__(self): | ||
36 | self.wishes = {} | 36 | self.wishes = {} | ||
37 | self.kids_ages = {} | 37 | self.kids_ages = {} | ||
38 | 38 | ||||
39 | def __call__(self, kid, talk): | 39 | def __call__(self, kid, talk): | ||
40 | wish = extract_wish(talk) | 40 | wish = extract_wish(talk) | ||
41 | self.wishes[kid] = wish | 41 | self.wishes[kid] = wish | ||
42 | 42 | ||||
43 | def __matmul__(self, letter): | 43 | def __matmul__(self, letter): | ||
44 | kid_id = extract_kid_id(letter) | 44 | kid_id = extract_kid_id(letter) | ||
45 | kid = next((child for child in Kid.get_all_kids() if id(child) == kid_id), None) | 45 | kid = next((child for child in Kid.get_all_kids() if id(child) == kid_id), None) | ||
46 | wish = extract_wish(letter) | 46 | wish = extract_wish(letter) | ||
47 | self.wishes[kid] = wish | 47 | self.wishes[kid] = wish | ||
48 | 48 | ||||
49 | def __iter__(self): | 49 | def __iter__(self): | ||
50 | return iter(self.wishes.values()) # is this right | 50 | return iter(self.wishes.values()) # is this right | ||
51 | 51 | ||||
52 | def xmas(self): | 52 | def xmas(self): | ||
53 | for every_kid in Kid.get_all_kids(): | 53 | for every_kid in Kid.get_all_kids(): | ||
54 | if every_kid not in self.kids_ages: | 54 | if every_kid not in self.kids_ages: | ||
55 | self.kids_ages[every_kid] = 1 | 55 | self.kids_ages[every_kid] = 1 | ||
56 | else: | 56 | else: | ||
57 | self.kids_ages[every_kid] += 1 | 57 | self.kids_ages[every_kid] += 1 | ||
58 | 58 | ||||
59 | if not self.wishes: | 59 | if not self.wishes: | ||
60 | return | 60 | return | ||
61 | 61 | ||||
62 | wish_counts = defaultdict(int) | 62 | wish_counts = defaultdict(int) | ||
63 | for wish in self.wishes.values(): | 63 | for wish in self.wishes.values(): | ||
64 | wish_counts[wish] += 1 | 64 | wish_counts[wish] += 1 | ||
65 | most_wished = max(wish_counts.items(), key=lambda x: x[1])[0] # is this actually right | 65 | most_wished = max(wish_counts.items(), key=lambda x: x[1])[0] # is this actually right | ||
66 | 66 | ||||
67 | for every_kid in Kid.get_all_kids(): | 67 | for every_kid in Kid.get_all_kids(): | ||
68 | if self.kids_ages[every_kid] > 5: | 68 | if self.kids_ages[every_kid] > 5: | ||
69 | continue | 69 | continue | ||
70 | 70 | ||||
71 | if is_bad_kid(every_kid): | 71 | if is_bad_kid(every_kid): | ||
72 | every_kid("coal") | 72 | every_kid("coal") | ||
73 | 73 | ||||
74 | else: | 74 | else: | ||
75 | wish = self.wishes.get(every_kid) | 75 | wish = self.wishes.get(every_kid) | ||
76 | if wish: | 76 | if wish: | ||
77 | every_kid(wish) | 77 | every_kid(wish) | ||
78 | else: | 78 | else: | ||
79 | every_kid(most_wished) | 79 | every_kid(most_wished) | ||
80 | 80 | ||||
81 | every_kid._threw_exception = False | 81 | every_kid._threw_exception = False | ||
82 | 82 | ||||
83 | self.wishes.clear() | 83 | self.wishes.clear() | ||
84 | 84 | ||||
85 | 85 | ||||
86 | class Kid(type): | 86 | class Kid(type): | ||
87 | _all_kids = set() | 87 | _all_kids = set() | ||
88 | 88 | ||||
89 | def __new__(cls, name, bases, dct): | 89 | def __new__(cls, name, bases, dct): | ||
90 | for attr_name, attr_value in dct.items(): | 90 | for attr_name, attr_value in dct.items(): | ||
91 | if callable(attr_value) and not attr_name.startswith("_"): | 91 | if callable(attr_value) and not attr_name.startswith("_"): | ||
92 | dct[attr_name] = track_exceptions(attr_value) | 92 | dct[attr_name] = track_exceptions(attr_value) | ||
93 | return super().__new__(cls, name, bases, dct) | 93 | return super().__new__(cls, name, bases, dct) | ||
94 | 94 | ||||
95 | def __call__(cls, *args, **kwargs): | 95 | def __call__(cls, *args, **kwargs): | ||
96 | instance = super().__call__(*args, **kwargs) | 96 | instance = super().__call__(*args, **kwargs) | ||
97 | Kid._all_kids.add(instance) | 97 | Kid._all_kids.add(instance) | ||
98 | instance._threw_exception = False | 98 | instance._threw_exception = False | ||
99 | if not callable(instance): | 99 | if not callable(instance): | ||
100 | raise NotImplementedError("Стига де! Това все пак са деца! Трябва да можем да им викаме!") | 100 | raise NotImplementedError("Стига де! Това все пак са деца! Трябва да можем да им викаме!") | ||
101 | return instance | 101 | return instance | ||
102 | 102 | ||||
103 | @staticmethod | 103 | @staticmethod | ||
104 | def get_all_kids(): | 104 | def get_all_kids(): | ||
105 | return Kid._all_kids | 105 | return Kid._all_kids |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
t | 1 | import re | t | 1 | import re |
2 | from collections import defaultdict | 2 | from collections import defaultdict | ||
3 | 3 | ||||
4 | 4 | ||||
5 | def extract_wish(text): | 5 | def extract_wish(text): | ||
6 | match = re.search(r'["\']([a-zA-Z0-9 ]+)["\']', text) | 6 | match = re.search(r'["\']([a-zA-Z0-9 ]+)["\']', text) | ||
7 | return match.group(1) | 7 | return match.group(1) | ||
8 | 8 | ||||
9 | 9 | ||||
10 | def extract_kid_id(text): | 10 | def extract_kid_id(text): | ||
11 | match = re.search(r'\b(\d+)\b', text) | 11 | match = re.search(r'\b(\d+)\b', text) | ||
12 | return int(match.group(1)) | 12 | return int(match.group(1)) | ||
13 | 13 | ||||
14 | 14 | ||||
15 | def track_exceptions(method): # as in the lection | 15 | def track_exceptions(method): # as in the lection | ||
16 | def wrapper(self, *args, **kwargs): | 16 | def wrapper(self, *args, **kwargs): | ||
17 | try: | 17 | try: | ||
18 | return method(self, *args, **kwargs) | 18 | return method(self, *args, **kwargs) | ||
19 | except Exception: | 19 | except Exception: | ||
20 | self._threw_exception = True | 20 | self._threw_exception = True | ||
21 | raise | 21 | raise | ||
22 | return wrapper | 22 | return wrapper | ||
23 | 23 | ||||
24 | 24 | ||||
25 | def is_bad_kid(kid): | 25 | def is_bad_kid(kid): | ||
26 | return kid._threw_exception | 26 | return kid._threw_exception | ||
27 | 27 | ||||
28 | 28 | ||||
29 | class Santa(object): | 29 | class Santa(object): | ||
30 | def __new__(cls): | 30 | def __new__(cls): | ||
31 | if not hasattr(cls, 'instance'): | 31 | if not hasattr(cls, 'instance'): | ||
32 | cls.instance = super(Santa, cls).__new__(cls) | 32 | cls.instance = super(Santa, cls).__new__(cls) | ||
33 | return cls.instance | 33 | return cls.instance | ||
34 | 34 | ||||
35 | def __init__(self): | 35 | def __init__(self): | ||
36 | self.wishes = {} | 36 | self.wishes = {} | ||
37 | self.kids_ages = {} | 37 | self.kids_ages = {} | ||
38 | 38 | ||||
39 | def __call__(self, kid, talk): | 39 | def __call__(self, kid, talk): | ||
40 | wish = extract_wish(talk) | 40 | wish = extract_wish(talk) | ||
41 | self.wishes[kid] = wish | 41 | self.wishes[kid] = wish | ||
42 | 42 | ||||
43 | def __matmul__(self, letter): | 43 | def __matmul__(self, letter): | ||
44 | kid_id = extract_kid_id(letter) | 44 | kid_id = extract_kid_id(letter) | ||
45 | kid = next((child for child in Kid.get_all_kids() if id(child) == kid_id), None) | 45 | kid = next((child for child in Kid.get_all_kids() if id(child) == kid_id), None) | ||
46 | wish = extract_wish(letter) | 46 | wish = extract_wish(letter) | ||
47 | self.wishes[kid] = wish | 47 | self.wishes[kid] = wish | ||
48 | 48 | ||||
49 | def __iter__(self): | 49 | def __iter__(self): | ||
50 | return iter(self.wishes.values()) # is this right | 50 | return iter(self.wishes.values()) # is this right | ||
51 | 51 | ||||
52 | def xmas(self): | 52 | def xmas(self): | ||
53 | for every_kid in Kid.get_all_kids(): | 53 | for every_kid in Kid.get_all_kids(): | ||
54 | if every_kid not in self.kids_ages: | 54 | if every_kid not in self.kids_ages: | ||
55 | self.kids_ages[every_kid] = 1 | 55 | self.kids_ages[every_kid] = 1 | ||
56 | else: | 56 | else: | ||
57 | self.kids_ages[every_kid] += 1 | 57 | self.kids_ages[every_kid] += 1 | ||
58 | 58 | ||||
59 | if not self.wishes: | 59 | if not self.wishes: | ||
60 | return | 60 | return | ||
61 | 61 | ||||
62 | wish_counts = defaultdict(int) | 62 | wish_counts = defaultdict(int) | ||
63 | for wish in self.wishes.values(): | 63 | for wish in self.wishes.values(): | ||
64 | wish_counts[wish] += 1 | 64 | wish_counts[wish] += 1 | ||
65 | most_wished = max(wish_counts.items(), key=lambda x: x[1])[0] # is this actually right | 65 | most_wished = max(wish_counts.items(), key=lambda x: x[1])[0] # is this actually right | ||
66 | 66 | ||||
67 | for every_kid in Kid.get_all_kids(): | 67 | for every_kid in Kid.get_all_kids(): | ||
68 | if self.kids_ages[every_kid] > 5: | 68 | if self.kids_ages[every_kid] > 5: | ||
69 | continue | 69 | continue | ||
70 | 70 | ||||
71 | if is_bad_kid(every_kid): | 71 | if is_bad_kid(every_kid): | ||
72 | every_kid("coal") | 72 | every_kid("coal") | ||
73 | 73 | ||||
74 | else: | 74 | else: | ||
75 | wish = self.wishes.get(every_kid) | 75 | wish = self.wishes.get(every_kid) | ||
76 | if wish: | 76 | if wish: | ||
77 | every_kid(wish) | 77 | every_kid(wish) | ||
78 | else: | 78 | else: | ||
79 | every_kid(most_wished) | 79 | every_kid(most_wished) | ||
80 | 80 | ||||
81 | every_kid._threw_exception = False | 81 | every_kid._threw_exception = False | ||
82 | 82 | ||||
83 | self.wishes.clear() | 83 | self.wishes.clear() | ||
84 | 84 | ||||
85 | 85 | ||||
86 | class Kid(type): | 86 | class Kid(type): | ||
87 | _all_kids = set() | 87 | _all_kids = set() | ||
88 | 88 | ||||
89 | def __new__(cls, name, bases, dct): | 89 | def __new__(cls, name, bases, dct): | ||
90 | for attr_name, attr_value in dct.items(): | 90 | for attr_name, attr_value in dct.items(): | ||
91 | if callable(attr_value) and not attr_name.startswith("_"): | 91 | if callable(attr_value) and not attr_name.startswith("_"): | ||
92 | dct[attr_name] = track_exceptions(attr_value) | 92 | dct[attr_name] = track_exceptions(attr_value) | ||
93 | return super().__new__(cls, name, bases, dct) | 93 | return super().__new__(cls, name, bases, dct) | ||
94 | 94 | ||||
95 | def __call__(cls, *args, **kwargs): | 95 | def __call__(cls, *args, **kwargs): | ||
96 | instance = super().__call__(*args, **kwargs) | 96 | instance = super().__call__(*args, **kwargs) | ||
97 | Kid._all_kids.add(instance) | 97 | Kid._all_kids.add(instance) | ||
98 | instance._threw_exception = False | 98 | instance._threw_exception = False | ||
99 | if not callable(instance): | 99 | if not callable(instance): | ||
100 | raise NotImplementedError("Стига де! Това все пак са деца! Трябва да можем да им викаме!") | 100 | raise NotImplementedError("Стига де! Това все пак са деца! Трябва да можем да им викаме!") | ||
101 | return instance | 101 | return instance | ||
102 | 102 | ||||
103 | @staticmethod | 103 | @staticmethod | ||
104 | def get_all_kids(): | 104 | def get_all_kids(): | ||
105 | return Kid._all_kids | 105 | return Kid._all_kids |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
t | 1 | import re | t | 1 | import re |
2 | from collections import defaultdict | 2 | from collections import defaultdict | ||
3 | 3 | ||||
4 | 4 | ||||
5 | def extract_wish(text): | 5 | def extract_wish(text): | ||
6 | match = re.search(r'["\']([a-zA-Z0-9 ]+)["\']', text) | 6 | match = re.search(r'["\']([a-zA-Z0-9 ]+)["\']', text) | ||
7 | return match.group(1) | 7 | return match.group(1) | ||
8 | 8 | ||||
9 | 9 | ||||
10 | def extract_kid_id(text): | 10 | def extract_kid_id(text): | ||
11 | match = re.search(r'\b(\d+)\b', text) | 11 | match = re.search(r'\b(\d+)\b', text) | ||
12 | return int(match.group(1)) | 12 | return int(match.group(1)) | ||
13 | 13 | ||||
14 | 14 | ||||
15 | def track_exceptions(method): # as in the lection | 15 | def track_exceptions(method): # as in the lection | ||
16 | def wrapper(self, *args, **kwargs): | 16 | def wrapper(self, *args, **kwargs): | ||
17 | try: | 17 | try: | ||
18 | return method(self, *args, **kwargs) | 18 | return method(self, *args, **kwargs) | ||
19 | except Exception: | 19 | except Exception: | ||
20 | self._threw_exception = True | 20 | self._threw_exception = True | ||
21 | raise | 21 | raise | ||
22 | return wrapper | 22 | return wrapper | ||
23 | 23 | ||||
24 | 24 | ||||
25 | def is_bad_kid(kid): | 25 | def is_bad_kid(kid): | ||
26 | return kid._threw_exception | 26 | return kid._threw_exception | ||
27 | 27 | ||||
28 | 28 | ||||
29 | class Santa(object): | 29 | class Santa(object): | ||
30 | def __new__(cls): | 30 | def __new__(cls): | ||
31 | if not hasattr(cls, 'instance'): | 31 | if not hasattr(cls, 'instance'): | ||
32 | cls.instance = super(Santa, cls).__new__(cls) | 32 | cls.instance = super(Santa, cls).__new__(cls) | ||
33 | return cls.instance | 33 | return cls.instance | ||
34 | 34 | ||||
35 | def __init__(self): | 35 | def __init__(self): | ||
36 | self.wishes = {} | 36 | self.wishes = {} | ||
37 | self.kids_ages = {} | 37 | self.kids_ages = {} | ||
38 | 38 | ||||
39 | def __call__(self, kid, talk): | 39 | def __call__(self, kid, talk): | ||
40 | wish = extract_wish(talk) | 40 | wish = extract_wish(talk) | ||
41 | self.wishes[kid] = wish | 41 | self.wishes[kid] = wish | ||
42 | 42 | ||||
43 | def __matmul__(self, letter): | 43 | def __matmul__(self, letter): | ||
44 | kid_id = extract_kid_id(letter) | 44 | kid_id = extract_kid_id(letter) | ||
45 | kid = next((child for child in Kid.get_all_kids() if id(child) == kid_id), None) | 45 | kid = next((child for child in Kid.get_all_kids() if id(child) == kid_id), None) | ||
46 | wish = extract_wish(letter) | 46 | wish = extract_wish(letter) | ||
47 | self.wishes[kid] = wish | 47 | self.wishes[kid] = wish | ||
48 | 48 | ||||
49 | def __iter__(self): | 49 | def __iter__(self): | ||
50 | return iter(self.wishes.values()) # is this right | 50 | return iter(self.wishes.values()) # is this right | ||
51 | 51 | ||||
52 | def xmas(self): | 52 | def xmas(self): | ||
53 | for every_kid in Kid.get_all_kids(): | 53 | for every_kid in Kid.get_all_kids(): | ||
54 | if every_kid not in self.kids_ages: | 54 | if every_kid not in self.kids_ages: | ||
55 | self.kids_ages[every_kid] = 1 | 55 | self.kids_ages[every_kid] = 1 | ||
56 | else: | 56 | else: | ||
57 | self.kids_ages[every_kid] += 1 | 57 | self.kids_ages[every_kid] += 1 | ||
58 | 58 | ||||
59 | if not self.wishes: | 59 | if not self.wishes: | ||
60 | return | 60 | return | ||
61 | 61 | ||||
62 | wish_counts = defaultdict(int) | 62 | wish_counts = defaultdict(int) | ||
63 | for wish in self.wishes.values(): | 63 | for wish in self.wishes.values(): | ||
64 | wish_counts[wish] += 1 | 64 | wish_counts[wish] += 1 | ||
65 | most_wished = max(wish_counts.items(), key=lambda x: x[1])[0] # is this actually right | 65 | most_wished = max(wish_counts.items(), key=lambda x: x[1])[0] # is this actually right | ||
66 | 66 | ||||
67 | for every_kid in Kid.get_all_kids(): | 67 | for every_kid in Kid.get_all_kids(): | ||
68 | if self.kids_ages[every_kid] > 5: | 68 | if self.kids_ages[every_kid] > 5: | ||
69 | continue | 69 | continue | ||
70 | 70 | ||||
71 | if is_bad_kid(every_kid): | 71 | if is_bad_kid(every_kid): | ||
72 | every_kid("coal") | 72 | every_kid("coal") | ||
73 | 73 | ||||
74 | else: | 74 | else: | ||
75 | wish = self.wishes.get(every_kid) | 75 | wish = self.wishes.get(every_kid) | ||
76 | if wish: | 76 | if wish: | ||
77 | every_kid(wish) | 77 | every_kid(wish) | ||
78 | else: | 78 | else: | ||
79 | every_kid(most_wished) | 79 | every_kid(most_wished) | ||
80 | 80 | ||||
81 | every_kid._threw_exception = False | 81 | every_kid._threw_exception = False | ||
82 | 82 | ||||
83 | self.wishes.clear() | 83 | self.wishes.clear() | ||
84 | 84 | ||||
85 | 85 | ||||
86 | class Kid(type): | 86 | class Kid(type): | ||
87 | _all_kids = set() | 87 | _all_kids = set() | ||
88 | 88 | ||||
89 | def __new__(cls, name, bases, dct): | 89 | def __new__(cls, name, bases, dct): | ||
90 | for attr_name, attr_value in dct.items(): | 90 | for attr_name, attr_value in dct.items(): | ||
91 | if callable(attr_value) and not attr_name.startswith("_"): | 91 | if callable(attr_value) and not attr_name.startswith("_"): | ||
92 | dct[attr_name] = track_exceptions(attr_value) | 92 | dct[attr_name] = track_exceptions(attr_value) | ||
93 | return super().__new__(cls, name, bases, dct) | 93 | return super().__new__(cls, name, bases, dct) | ||
94 | 94 | ||||
95 | def __call__(cls, *args, **kwargs): | 95 | def __call__(cls, *args, **kwargs): | ||
96 | instance = super().__call__(*args, **kwargs) | 96 | instance = super().__call__(*args, **kwargs) | ||
97 | Kid._all_kids.add(instance) | 97 | Kid._all_kids.add(instance) | ||
98 | instance._threw_exception = False | 98 | instance._threw_exception = False | ||
99 | if not callable(instance): | 99 | if not callable(instance): | ||
100 | raise NotImplementedError("Стига де! Това все пак са деца! Трябва да можем да им викаме!") | 100 | raise NotImplementedError("Стига де! Това все пак са деца! Трябва да можем да им викаме!") | ||
101 | return instance | 101 | return instance | ||
102 | 102 | ||||
103 | @staticmethod | 103 | @staticmethod | ||
104 | def get_all_kids(): | 104 | def get_all_kids(): | ||
105 | return Kid._all_kids | 105 | return Kid._all_kids |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
t | 1 | import re | t | 1 | import re |
2 | from collections import defaultdict | 2 | from collections import defaultdict | ||
3 | 3 | ||||
4 | 4 | ||||
5 | def extract_wish(text): | 5 | def extract_wish(text): | ||
6 | match = re.search(r'["\']([a-zA-Z0-9 ]+)["\']', text) | 6 | match = re.search(r'["\']([a-zA-Z0-9 ]+)["\']', text) | ||
7 | return match.group(1) | 7 | return match.group(1) | ||
8 | 8 | ||||
9 | 9 | ||||
10 | def extract_kid_id(text): | 10 | def extract_kid_id(text): | ||
11 | match = re.search(r'\b(\d+)\b', text) | 11 | match = re.search(r'\b(\d+)\b', text) | ||
12 | return int(match.group(1)) | 12 | return int(match.group(1)) | ||
13 | 13 | ||||
14 | 14 | ||||
15 | def track_exceptions(method): # as in the lection | 15 | def track_exceptions(method): # as in the lection | ||
16 | def wrapper(self, *args, **kwargs): | 16 | def wrapper(self, *args, **kwargs): | ||
17 | try: | 17 | try: | ||
18 | return method(self, *args, **kwargs) | 18 | return method(self, *args, **kwargs) | ||
19 | except Exception: | 19 | except Exception: | ||
20 | self._threw_exception = True | 20 | self._threw_exception = True | ||
21 | raise | 21 | raise | ||
22 | return wrapper | 22 | return wrapper | ||
23 | 23 | ||||
24 | 24 | ||||
25 | def is_bad_kid(kid): | 25 | def is_bad_kid(kid): | ||
26 | return kid._threw_exception | 26 | return kid._threw_exception | ||
27 | 27 | ||||
28 | 28 | ||||
29 | class Santa(object): | 29 | class Santa(object): | ||
30 | def __new__(cls): | 30 | def __new__(cls): | ||
31 | if not hasattr(cls, 'instance'): | 31 | if not hasattr(cls, 'instance'): | ||
32 | cls.instance = super(Santa, cls).__new__(cls) | 32 | cls.instance = super(Santa, cls).__new__(cls) | ||
33 | return cls.instance | 33 | return cls.instance | ||
34 | 34 | ||||
35 | def __init__(self): | 35 | def __init__(self): | ||
36 | self.wishes = {} | 36 | self.wishes = {} | ||
37 | self.kids_ages = {} | 37 | self.kids_ages = {} | ||
38 | 38 | ||||
39 | def __call__(self, kid, talk): | 39 | def __call__(self, kid, talk): | ||
40 | wish = extract_wish(talk) | 40 | wish = extract_wish(talk) | ||
41 | self.wishes[kid] = wish | 41 | self.wishes[kid] = wish | ||
42 | 42 | ||||
43 | def __matmul__(self, letter): | 43 | def __matmul__(self, letter): | ||
44 | kid_id = extract_kid_id(letter) | 44 | kid_id = extract_kid_id(letter) | ||
45 | kid = next((child for child in Kid.get_all_kids() if id(child) == kid_id), None) | 45 | kid = next((child for child in Kid.get_all_kids() if id(child) == kid_id), None) | ||
46 | wish = extract_wish(letter) | 46 | wish = extract_wish(letter) | ||
47 | self.wishes[kid] = wish | 47 | self.wishes[kid] = wish | ||
48 | 48 | ||||
49 | def __iter__(self): | 49 | def __iter__(self): | ||
50 | return iter(self.wishes.values()) # is this right | 50 | return iter(self.wishes.values()) # is this right | ||
51 | 51 | ||||
52 | def xmas(self): | 52 | def xmas(self): | ||
53 | for every_kid in Kid.get_all_kids(): | 53 | for every_kid in Kid.get_all_kids(): | ||
54 | if every_kid not in self.kids_ages: | 54 | if every_kid not in self.kids_ages: | ||
55 | self.kids_ages[every_kid] = 1 | 55 | self.kids_ages[every_kid] = 1 | ||
56 | else: | 56 | else: | ||
57 | self.kids_ages[every_kid] += 1 | 57 | self.kids_ages[every_kid] += 1 | ||
58 | 58 | ||||
59 | if not self.wishes: | 59 | if not self.wishes: | ||
60 | return | 60 | return | ||
61 | 61 | ||||
62 | wish_counts = defaultdict(int) | 62 | wish_counts = defaultdict(int) | ||
63 | for wish in self.wishes.values(): | 63 | for wish in self.wishes.values(): | ||
64 | wish_counts[wish] += 1 | 64 | wish_counts[wish] += 1 | ||
65 | most_wished = max(wish_counts.items(), key=lambda x: x[1])[0] # is this actually right | 65 | most_wished = max(wish_counts.items(), key=lambda x: x[1])[0] # is this actually right | ||
66 | 66 | ||||
67 | for every_kid in Kid.get_all_kids(): | 67 | for every_kid in Kid.get_all_kids(): | ||
68 | if self.kids_ages[every_kid] > 5: | 68 | if self.kids_ages[every_kid] > 5: | ||
69 | continue | 69 | continue | ||
70 | 70 | ||||
71 | if is_bad_kid(every_kid): | 71 | if is_bad_kid(every_kid): | ||
72 | every_kid("coal") | 72 | every_kid("coal") | ||
73 | 73 | ||||
74 | else: | 74 | else: | ||
75 | wish = self.wishes.get(every_kid) | 75 | wish = self.wishes.get(every_kid) | ||
76 | if wish: | 76 | if wish: | ||
77 | every_kid(wish) | 77 | every_kid(wish) | ||
78 | else: | 78 | else: | ||
79 | every_kid(most_wished) | 79 | every_kid(most_wished) | ||
80 | 80 | ||||
81 | every_kid._threw_exception = False | 81 | every_kid._threw_exception = False | ||
82 | 82 | ||||
83 | self.wishes.clear() | 83 | self.wishes.clear() | ||
84 | 84 | ||||
85 | 85 | ||||
86 | class Kid(type): | 86 | class Kid(type): | ||
87 | _all_kids = set() | 87 | _all_kids = set() | ||
88 | 88 | ||||
89 | def __new__(cls, name, bases, dct): | 89 | def __new__(cls, name, bases, dct): | ||
90 | for attr_name, attr_value in dct.items(): | 90 | for attr_name, attr_value in dct.items(): | ||
91 | if callable(attr_value) and not attr_name.startswith("_"): | 91 | if callable(attr_value) and not attr_name.startswith("_"): | ||
92 | dct[attr_name] = track_exceptions(attr_value) | 92 | dct[attr_name] = track_exceptions(attr_value) | ||
93 | return super().__new__(cls, name, bases, dct) | 93 | return super().__new__(cls, name, bases, dct) | ||
94 | 94 | ||||
95 | def __call__(cls, *args, **kwargs): | 95 | def __call__(cls, *args, **kwargs): | ||
96 | instance = super().__call__(*args, **kwargs) | 96 | instance = super().__call__(*args, **kwargs) | ||
97 | Kid._all_kids.add(instance) | 97 | Kid._all_kids.add(instance) | ||
98 | instance._threw_exception = False | 98 | instance._threw_exception = False | ||
99 | if not callable(instance): | 99 | if not callable(instance): | ||
100 | raise NotImplementedError("Стига де! Това все пак са деца! Трябва да можем да им викаме!") | 100 | raise NotImplementedError("Стига де! Това все пак са деца! Трябва да можем да им викаме!") | ||
101 | return instance | 101 | return instance | ||
102 | 102 | ||||
103 | @staticmethod | 103 | @staticmethod | ||
104 | def get_all_kids(): | 104 | def get_all_kids(): | ||
105 | return Kid._all_kids | 105 | return Kid._all_kids |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
t | 1 | import re | t | 1 | import re |
2 | from collections import defaultdict | 2 | from collections import defaultdict | ||
3 | 3 | ||||
4 | 4 | ||||
5 | def extract_wish(text): | 5 | def extract_wish(text): | ||
6 | match = re.search(r'["\']([a-zA-Z0-9 ]+)["\']', text) | 6 | match = re.search(r'["\']([a-zA-Z0-9 ]+)["\']', text) | ||
7 | return match.group(1) | 7 | return match.group(1) | ||
8 | 8 | ||||
9 | 9 | ||||
10 | def extract_kid_id(text): | 10 | def extract_kid_id(text): | ||
11 | match = re.search(r'\b(\d+)\b', text) | 11 | match = re.search(r'\b(\d+)\b', text) | ||
12 | return int(match.group(1)) | 12 | return int(match.group(1)) | ||
13 | 13 | ||||
14 | 14 | ||||
15 | def track_exceptions(method): # as in the lection | 15 | def track_exceptions(method): # as in the lection | ||
16 | def wrapper(self, *args, **kwargs): | 16 | def wrapper(self, *args, **kwargs): | ||
17 | try: | 17 | try: | ||
18 | return method(self, *args, **kwargs) | 18 | return method(self, *args, **kwargs) | ||
19 | except Exception: | 19 | except Exception: | ||
20 | self._threw_exception = True | 20 | self._threw_exception = True | ||
21 | raise | 21 | raise | ||
22 | return wrapper | 22 | return wrapper | ||
23 | 23 | ||||
24 | 24 | ||||
25 | def is_bad_kid(kid): | 25 | def is_bad_kid(kid): | ||
26 | return kid._threw_exception | 26 | return kid._threw_exception | ||
27 | 27 | ||||
28 | 28 | ||||
29 | class Santa(object): | 29 | class Santa(object): | ||
30 | def __new__(cls): | 30 | def __new__(cls): | ||
31 | if not hasattr(cls, 'instance'): | 31 | if not hasattr(cls, 'instance'): | ||
32 | cls.instance = super(Santa, cls).__new__(cls) | 32 | cls.instance = super(Santa, cls).__new__(cls) | ||
33 | return cls.instance | 33 | return cls.instance | ||
34 | 34 | ||||
35 | def __init__(self): | 35 | def __init__(self): | ||
36 | self.wishes = {} | 36 | self.wishes = {} | ||
37 | self.kids_ages = {} | 37 | self.kids_ages = {} | ||
38 | 38 | ||||
39 | def __call__(self, kid, talk): | 39 | def __call__(self, kid, talk): | ||
40 | wish = extract_wish(talk) | 40 | wish = extract_wish(talk) | ||
41 | self.wishes[kid] = wish | 41 | self.wishes[kid] = wish | ||
42 | 42 | ||||
43 | def __matmul__(self, letter): | 43 | def __matmul__(self, letter): | ||
44 | kid_id = extract_kid_id(letter) | 44 | kid_id = extract_kid_id(letter) | ||
45 | kid = next((child for child in Kid.get_all_kids() if id(child) == kid_id), None) | 45 | kid = next((child for child in Kid.get_all_kids() if id(child) == kid_id), None) | ||
46 | wish = extract_wish(letter) | 46 | wish = extract_wish(letter) | ||
47 | self.wishes[kid] = wish | 47 | self.wishes[kid] = wish | ||
48 | 48 | ||||
49 | def __iter__(self): | 49 | def __iter__(self): | ||
50 | return iter(self.wishes.values()) # is this right | 50 | return iter(self.wishes.values()) # is this right | ||
51 | 51 | ||||
52 | def xmas(self): | 52 | def xmas(self): | ||
53 | for every_kid in Kid.get_all_kids(): | 53 | for every_kid in Kid.get_all_kids(): | ||
54 | if every_kid not in self.kids_ages: | 54 | if every_kid not in self.kids_ages: | ||
55 | self.kids_ages[every_kid] = 1 | 55 | self.kids_ages[every_kid] = 1 | ||
56 | else: | 56 | else: | ||
57 | self.kids_ages[every_kid] += 1 | 57 | self.kids_ages[every_kid] += 1 | ||
58 | 58 | ||||
59 | if not self.wishes: | 59 | if not self.wishes: | ||
60 | return | 60 | return | ||
61 | 61 | ||||
62 | wish_counts = defaultdict(int) | 62 | wish_counts = defaultdict(int) | ||
63 | for wish in self.wishes.values(): | 63 | for wish in self.wishes.values(): | ||
64 | wish_counts[wish] += 1 | 64 | wish_counts[wish] += 1 | ||
65 | most_wished = max(wish_counts.items(), key=lambda x: x[1])[0] # is this actually right | 65 | most_wished = max(wish_counts.items(), key=lambda x: x[1])[0] # is this actually right | ||
66 | 66 | ||||
67 | for every_kid in Kid.get_all_kids(): | 67 | for every_kid in Kid.get_all_kids(): | ||
68 | if self.kids_ages[every_kid] > 5: | 68 | if self.kids_ages[every_kid] > 5: | ||
69 | continue | 69 | continue | ||
70 | 70 | ||||
71 | if is_bad_kid(every_kid): | 71 | if is_bad_kid(every_kid): | ||
72 | every_kid("coal") | 72 | every_kid("coal") | ||
73 | 73 | ||||
74 | else: | 74 | else: | ||
75 | wish = self.wishes.get(every_kid) | 75 | wish = self.wishes.get(every_kid) | ||
76 | if wish: | 76 | if wish: | ||
77 | every_kid(wish) | 77 | every_kid(wish) | ||
78 | else: | 78 | else: | ||
79 | every_kid(most_wished) | 79 | every_kid(most_wished) | ||
80 | 80 | ||||
81 | every_kid._threw_exception = False | 81 | every_kid._threw_exception = False | ||
82 | 82 | ||||
83 | self.wishes.clear() | 83 | self.wishes.clear() | ||
84 | 84 | ||||
85 | 85 | ||||
86 | class Kid(type): | 86 | class Kid(type): | ||
87 | _all_kids = set() | 87 | _all_kids = set() | ||
88 | 88 | ||||
89 | def __new__(cls, name, bases, dct): | 89 | def __new__(cls, name, bases, dct): | ||
90 | for attr_name, attr_value in dct.items(): | 90 | for attr_name, attr_value in dct.items(): | ||
91 | if callable(attr_value) and not attr_name.startswith("_"): | 91 | if callable(attr_value) and not attr_name.startswith("_"): | ||
92 | dct[attr_name] = track_exceptions(attr_value) | 92 | dct[attr_name] = track_exceptions(attr_value) | ||
93 | return super().__new__(cls, name, bases, dct) | 93 | return super().__new__(cls, name, bases, dct) | ||
94 | 94 | ||||
95 | def __call__(cls, *args, **kwargs): | 95 | def __call__(cls, *args, **kwargs): | ||
96 | instance = super().__call__(*args, **kwargs) | 96 | instance = super().__call__(*args, **kwargs) | ||
97 | Kid._all_kids.add(instance) | 97 | Kid._all_kids.add(instance) | ||
98 | instance._threw_exception = False | 98 | instance._threw_exception = False | ||
99 | if not callable(instance): | 99 | if not callable(instance): | ||
100 | raise NotImplementedError("Стига де! Това все пак са деца! Трябва да можем да им викаме!") | 100 | raise NotImplementedError("Стига де! Това все пак са деца! Трябва да можем да им викаме!") | ||
101 | return instance | 101 | return instance | ||
102 | 102 | ||||
103 | @staticmethod | 103 | @staticmethod | ||
104 | def get_all_kids(): | 104 | def get_all_kids(): | ||
105 | return Kid._all_kids | 105 | return Kid._all_kids |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
t | 1 | import re | t | 1 | import re |
2 | from collections import defaultdict | 2 | from collections import defaultdict | ||
3 | 3 | ||||
4 | 4 | ||||
5 | def extract_wish(text): | 5 | def extract_wish(text): | ||
6 | match = re.search(r'["\']([a-zA-Z0-9 ]+)["\']', text) | 6 | match = re.search(r'["\']([a-zA-Z0-9 ]+)["\']', text) | ||
7 | return match.group(1) | 7 | return match.group(1) | ||
8 | 8 | ||||
9 | 9 | ||||
10 | def extract_kid_id(text): | 10 | def extract_kid_id(text): | ||
11 | match = re.search(r'\b(\d+)\b', text) | 11 | match = re.search(r'\b(\d+)\b', text) | ||
12 | return int(match.group(1)) | 12 | return int(match.group(1)) | ||
13 | 13 | ||||
14 | 14 | ||||
15 | def track_exceptions(method): # as in the lection | 15 | def track_exceptions(method): # as in the lection | ||
16 | def wrapper(self, *args, **kwargs): | 16 | def wrapper(self, *args, **kwargs): | ||
17 | try: | 17 | try: | ||
18 | return method(self, *args, **kwargs) | 18 | return method(self, *args, **kwargs) | ||
19 | except Exception: | 19 | except Exception: | ||
20 | self._threw_exception = True | 20 | self._threw_exception = True | ||
21 | raise | 21 | raise | ||
22 | return wrapper | 22 | return wrapper | ||
23 | 23 | ||||
24 | 24 | ||||
25 | def is_bad_kid(kid): | 25 | def is_bad_kid(kid): | ||
26 | return kid._threw_exception | 26 | return kid._threw_exception | ||
27 | 27 | ||||
28 | 28 | ||||
29 | class Santa(object): | 29 | class Santa(object): | ||
30 | def __new__(cls): | 30 | def __new__(cls): | ||
31 | if not hasattr(cls, 'instance'): | 31 | if not hasattr(cls, 'instance'): | ||
32 | cls.instance = super(Santa, cls).__new__(cls) | 32 | cls.instance = super(Santa, cls).__new__(cls) | ||
33 | return cls.instance | 33 | return cls.instance | ||
34 | 34 | ||||
35 | def __init__(self): | 35 | def __init__(self): | ||
36 | self.wishes = {} | 36 | self.wishes = {} | ||
37 | self.kids_ages = {} | 37 | self.kids_ages = {} | ||
38 | 38 | ||||
39 | def __call__(self, kid, talk): | 39 | def __call__(self, kid, talk): | ||
40 | wish = extract_wish(talk) | 40 | wish = extract_wish(talk) | ||
41 | self.wishes[kid] = wish | 41 | self.wishes[kid] = wish | ||
42 | 42 | ||||
43 | def __matmul__(self, letter): | 43 | def __matmul__(self, letter): | ||
44 | kid_id = extract_kid_id(letter) | 44 | kid_id = extract_kid_id(letter) | ||
45 | kid = next((child for child in Kid.get_all_kids() if id(child) == kid_id), None) | 45 | kid = next((child for child in Kid.get_all_kids() if id(child) == kid_id), None) | ||
46 | wish = extract_wish(letter) | 46 | wish = extract_wish(letter) | ||
47 | self.wishes[kid] = wish | 47 | self.wishes[kid] = wish | ||
48 | 48 | ||||
49 | def __iter__(self): | 49 | def __iter__(self): | ||
50 | return iter(self.wishes.values()) # is this right | 50 | return iter(self.wishes.values()) # is this right | ||
51 | 51 | ||||
52 | def xmas(self): | 52 | def xmas(self): | ||
53 | for every_kid in Kid.get_all_kids(): | 53 | for every_kid in Kid.get_all_kids(): | ||
54 | if every_kid not in self.kids_ages: | 54 | if every_kid not in self.kids_ages: | ||
55 | self.kids_ages[every_kid] = 1 | 55 | self.kids_ages[every_kid] = 1 | ||
56 | else: | 56 | else: | ||
57 | self.kids_ages[every_kid] += 1 | 57 | self.kids_ages[every_kid] += 1 | ||
58 | 58 | ||||
59 | if not self.wishes: | 59 | if not self.wishes: | ||
60 | return | 60 | return | ||
61 | 61 | ||||
62 | wish_counts = defaultdict(int) | 62 | wish_counts = defaultdict(int) | ||
63 | for wish in self.wishes.values(): | 63 | for wish in self.wishes.values(): | ||
64 | wish_counts[wish] += 1 | 64 | wish_counts[wish] += 1 | ||
65 | most_wished = max(wish_counts.items(), key=lambda x: x[1])[0] # is this actually right | 65 | most_wished = max(wish_counts.items(), key=lambda x: x[1])[0] # is this actually right | ||
66 | 66 | ||||
67 | for every_kid in Kid.get_all_kids(): | 67 | for every_kid in Kid.get_all_kids(): | ||
68 | if self.kids_ages[every_kid] > 5: | 68 | if self.kids_ages[every_kid] > 5: | ||
69 | continue | 69 | continue | ||
70 | 70 | ||||
71 | if is_bad_kid(every_kid): | 71 | if is_bad_kid(every_kid): | ||
72 | every_kid("coal") | 72 | every_kid("coal") | ||
73 | 73 | ||||
74 | else: | 74 | else: | ||
75 | wish = self.wishes.get(every_kid) | 75 | wish = self.wishes.get(every_kid) | ||
76 | if wish: | 76 | if wish: | ||
77 | every_kid(wish) | 77 | every_kid(wish) | ||
78 | else: | 78 | else: | ||
79 | every_kid(most_wished) | 79 | every_kid(most_wished) | ||
80 | 80 | ||||
81 | every_kid._threw_exception = False | 81 | every_kid._threw_exception = False | ||
82 | 82 | ||||
83 | self.wishes.clear() | 83 | self.wishes.clear() | ||
84 | 84 | ||||
85 | 85 | ||||
86 | class Kid(type): | 86 | class Kid(type): | ||
87 | _all_kids = set() | 87 | _all_kids = set() | ||
88 | 88 | ||||
89 | def __new__(cls, name, bases, dct): | 89 | def __new__(cls, name, bases, dct): | ||
90 | for attr_name, attr_value in dct.items(): | 90 | for attr_name, attr_value in dct.items(): | ||
91 | if callable(attr_value) and not attr_name.startswith("_"): | 91 | if callable(attr_value) and not attr_name.startswith("_"): | ||
92 | dct[attr_name] = track_exceptions(attr_value) | 92 | dct[attr_name] = track_exceptions(attr_value) | ||
93 | return super().__new__(cls, name, bases, dct) | 93 | return super().__new__(cls, name, bases, dct) | ||
94 | 94 | ||||
95 | def __call__(cls, *args, **kwargs): | 95 | def __call__(cls, *args, **kwargs): | ||
96 | instance = super().__call__(*args, **kwargs) | 96 | instance = super().__call__(*args, **kwargs) | ||
97 | Kid._all_kids.add(instance) | 97 | Kid._all_kids.add(instance) | ||
98 | instance._threw_exception = False | 98 | instance._threw_exception = False | ||
99 | if not callable(instance): | 99 | if not callable(instance): | ||
100 | raise NotImplementedError("Стига де! Това все пак са деца! Трябва да можем да им викаме!") | 100 | raise NotImplementedError("Стига де! Това все пак са деца! Трябва да можем да им викаме!") | ||
101 | return instance | 101 | return instance | ||
102 | 102 | ||||
103 | @staticmethod | 103 | @staticmethod | ||
104 | def get_all_kids(): | 104 | def get_all_kids(): | ||
105 | return Kid._all_kids | 105 | return Kid._all_kids |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
t | 1 | import re | t | 1 | import re |
2 | from collections import defaultdict | 2 | from collections import defaultdict | ||
3 | 3 | ||||
4 | 4 | ||||
5 | def extract_wish(text): | 5 | def extract_wish(text): | ||
6 | match = re.search(r'["\']([a-zA-Z0-9 ]+)["\']', text) | 6 | match = re.search(r'["\']([a-zA-Z0-9 ]+)["\']', text) | ||
7 | return match.group(1) | 7 | return match.group(1) | ||
8 | 8 | ||||
9 | 9 | ||||
10 | def extract_kid_id(text): | 10 | def extract_kid_id(text): | ||
11 | match = re.search(r'\b(\d+)\b', text) | 11 | match = re.search(r'\b(\d+)\b', text) | ||
12 | return int(match.group(1)) | 12 | return int(match.group(1)) | ||
13 | 13 | ||||
14 | 14 | ||||
15 | def track_exceptions(method): # as in the lection | 15 | def track_exceptions(method): # as in the lection | ||
16 | def wrapper(self, *args, **kwargs): | 16 | def wrapper(self, *args, **kwargs): | ||
17 | try: | 17 | try: | ||
18 | return method(self, *args, **kwargs) | 18 | return method(self, *args, **kwargs) | ||
19 | except Exception: | 19 | except Exception: | ||
20 | self._threw_exception = True | 20 | self._threw_exception = True | ||
21 | raise | 21 | raise | ||
22 | return wrapper | 22 | return wrapper | ||
23 | 23 | ||||
24 | 24 | ||||
25 | def is_bad_kid(kid): | 25 | def is_bad_kid(kid): | ||
26 | return kid._threw_exception | 26 | return kid._threw_exception | ||
27 | 27 | ||||
28 | 28 | ||||
29 | class Santa(object): | 29 | class Santa(object): | ||
30 | def __new__(cls): | 30 | def __new__(cls): | ||
31 | if not hasattr(cls, 'instance'): | 31 | if not hasattr(cls, 'instance'): | ||
32 | cls.instance = super(Santa, cls).__new__(cls) | 32 | cls.instance = super(Santa, cls).__new__(cls) | ||
33 | return cls.instance | 33 | return cls.instance | ||
34 | 34 | ||||
35 | def __init__(self): | 35 | def __init__(self): | ||
36 | self.wishes = {} | 36 | self.wishes = {} | ||
37 | self.kids_ages = {} | 37 | self.kids_ages = {} | ||
38 | 38 | ||||
39 | def __call__(self, kid, talk): | 39 | def __call__(self, kid, talk): | ||
40 | wish = extract_wish(talk) | 40 | wish = extract_wish(talk) | ||
41 | self.wishes[kid] = wish | 41 | self.wishes[kid] = wish | ||
42 | 42 | ||||
43 | def __matmul__(self, letter): | 43 | def __matmul__(self, letter): | ||
44 | kid_id = extract_kid_id(letter) | 44 | kid_id = extract_kid_id(letter) | ||
45 | kid = next((child for child in Kid.get_all_kids() if id(child) == kid_id), None) | 45 | kid = next((child for child in Kid.get_all_kids() if id(child) == kid_id), None) | ||
46 | wish = extract_wish(letter) | 46 | wish = extract_wish(letter) | ||
47 | self.wishes[kid] = wish | 47 | self.wishes[kid] = wish | ||
48 | 48 | ||||
49 | def __iter__(self): | 49 | def __iter__(self): | ||
50 | return iter(self.wishes.values()) # is this right | 50 | return iter(self.wishes.values()) # is this right | ||
51 | 51 | ||||
52 | def xmas(self): | 52 | def xmas(self): | ||
53 | for every_kid in Kid.get_all_kids(): | 53 | for every_kid in Kid.get_all_kids(): | ||
54 | if every_kid not in self.kids_ages: | 54 | if every_kid not in self.kids_ages: | ||
55 | self.kids_ages[every_kid] = 1 | 55 | self.kids_ages[every_kid] = 1 | ||
56 | else: | 56 | else: | ||
57 | self.kids_ages[every_kid] += 1 | 57 | self.kids_ages[every_kid] += 1 | ||
58 | 58 | ||||
59 | if not self.wishes: | 59 | if not self.wishes: | ||
60 | return | 60 | return | ||
61 | 61 | ||||
62 | wish_counts = defaultdict(int) | 62 | wish_counts = defaultdict(int) | ||
63 | for wish in self.wishes.values(): | 63 | for wish in self.wishes.values(): | ||
64 | wish_counts[wish] += 1 | 64 | wish_counts[wish] += 1 | ||
65 | most_wished = max(wish_counts.items(), key=lambda x: x[1])[0] # is this actually right | 65 | most_wished = max(wish_counts.items(), key=lambda x: x[1])[0] # is this actually right | ||
66 | 66 | ||||
67 | for every_kid in Kid.get_all_kids(): | 67 | for every_kid in Kid.get_all_kids(): | ||
68 | if self.kids_ages[every_kid] > 5: | 68 | if self.kids_ages[every_kid] > 5: | ||
69 | continue | 69 | continue | ||
70 | 70 | ||||
71 | if is_bad_kid(every_kid): | 71 | if is_bad_kid(every_kid): | ||
72 | every_kid("coal") | 72 | every_kid("coal") | ||
73 | 73 | ||||
74 | else: | 74 | else: | ||
75 | wish = self.wishes.get(every_kid) | 75 | wish = self.wishes.get(every_kid) | ||
76 | if wish: | 76 | if wish: | ||
77 | every_kid(wish) | 77 | every_kid(wish) | ||
78 | else: | 78 | else: | ||
79 | every_kid(most_wished) | 79 | every_kid(most_wished) | ||
80 | 80 | ||||
81 | every_kid._threw_exception = False | 81 | every_kid._threw_exception = False | ||
82 | 82 | ||||
83 | self.wishes.clear() | 83 | self.wishes.clear() | ||
84 | 84 | ||||
85 | 85 | ||||
86 | class Kid(type): | 86 | class Kid(type): | ||
87 | _all_kids = set() | 87 | _all_kids = set() | ||
88 | 88 | ||||
89 | def __new__(cls, name, bases, dct): | 89 | def __new__(cls, name, bases, dct): | ||
90 | for attr_name, attr_value in dct.items(): | 90 | for attr_name, attr_value in dct.items(): | ||
91 | if callable(attr_value) and not attr_name.startswith("_"): | 91 | if callable(attr_value) and not attr_name.startswith("_"): | ||
92 | dct[attr_name] = track_exceptions(attr_value) | 92 | dct[attr_name] = track_exceptions(attr_value) | ||
93 | return super().__new__(cls, name, bases, dct) | 93 | return super().__new__(cls, name, bases, dct) | ||
94 | 94 | ||||
95 | def __call__(cls, *args, **kwargs): | 95 | def __call__(cls, *args, **kwargs): | ||
96 | instance = super().__call__(*args, **kwargs) | 96 | instance = super().__call__(*args, **kwargs) | ||
97 | Kid._all_kids.add(instance) | 97 | Kid._all_kids.add(instance) | ||
98 | instance._threw_exception = False | 98 | instance._threw_exception = False | ||
99 | if not callable(instance): | 99 | if not callable(instance): | ||
100 | raise NotImplementedError("Стига де! Това все пак са деца! Трябва да можем да им викаме!") | 100 | raise NotImplementedError("Стига де! Това все пак са деца! Трябва да можем да им викаме!") | ||
101 | return instance | 101 | return instance | ||
102 | 102 | ||||
103 | @staticmethod | 103 | @staticmethod | ||
104 | def get_all_kids(): | 104 | def get_all_kids(): | ||
105 | return Kid._all_kids | 105 | return Kid._all_kids |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
19.12.2024 16:21
19.12.2024 16:23
19.12.2024 16:26
19.12.2024 16:25
19.12.2024 16:28
19.12.2024 16:29
19.12.2024 16:35
19.12.2024 16:37
19.12.2024 16:39