Домашни > Подаръци ще има за всички от сърце > Решения > Решението на Валентина Петрова

Резултати
9 точки от тестове
0 точки от учител

9 точки общо

18 успешни теста
2 неуспешни теста
Код
Скрий всички коментари

  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

Извинявам се за спама, но или компютърът, или сайтът ми бъгна.
История

t1import ret1import re
2from collections import defaultdict2from collections import defaultdict
33
44
5def extract_wish(text):5def 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)
88
99
10def extract_kid_id(text):10def 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))
1313
1414
15def track_exceptions(method):  # as in the lection15def 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 = True20            self._threw_exception = True
21            raise21            raise
22    return wrapper22    return wrapper
2323
2424
25def is_bad_kid(kid):25def is_bad_kid(kid):
26    return kid._threw_exception26    return kid._threw_exception
2727
2828
29class Santa(object):29class 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.instance33        return cls.instance
3434
35    def __init__(self):35    def __init__(self):
36        self.wishes = {}36        self.wishes = {}
37        self.kids_ages = {}37        self.kids_ages = {}
3838
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] = wish41        self.wishes[kid] = wish
4242
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] = wish47        self.wishes[kid] = wish
4848
49    def __iter__(self):49    def __iter__(self):
50        return iter(self.wishes.values())     # is this right50        return iter(self.wishes.values())     # is this right
5151
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] = 155                self.kids_ages[every_kid] = 1
56            else:56            else:
57                self.kids_ages[every_kid] += 157                self.kids_ages[every_kid] += 1
5858
59        if not self.wishes:59        if not self.wishes:
60            return60            return
6161
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] += 164            wish_counts[wish] += 1
65        most_wished = max(wish_counts.items(), key=lambda x: x[1])[0]  # is this actually right65        most_wished = max(wish_counts.items(), key=lambda x: x[1])[0]  # is this actually right
6666
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                continue69                continue
7070
71            if is_bad_kid(every_kid):71            if is_bad_kid(every_kid):
72                every_kid("coal")72                every_kid("coal")
7373
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)
8080
81            every_kid._threw_exception = False81            every_kid._threw_exception = False
8282
83        self.wishes.clear()83        self.wishes.clear()
8484
8585
86class Kid(type):86class Kid(type):
87    _all_kids = set()87    _all_kids = set()
8888
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)
9494
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 = False98        instance._threw_exception = False
99        if not callable(instance):99        if not callable(instance):
100            raise NotImplementedError("Стига де! Това все пак са деца! Трябва да можем да им викаме!")100            raise NotImplementedError("Стига де! Това все пак са деца! Трябва да можем да им викаме!")
101        return instance101        return instance
102102
103    @staticmethod103    @staticmethod
104    def get_all_kids():104    def get_all_kids():
105        return Kid._all_kids105        return Kid._all_kids
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

t1import ret1import re
2from collections import defaultdict2from collections import defaultdict
33
44
5def extract_wish(text):5def 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)
88
99
10def extract_kid_id(text):10def 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))
1313
1414
15def track_exceptions(method):  # as in the lection15def 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 = True20            self._threw_exception = True
21            raise21            raise
22    return wrapper22    return wrapper
2323
2424
25def is_bad_kid(kid):25def is_bad_kid(kid):
26    return kid._threw_exception26    return kid._threw_exception
2727
2828
29class Santa(object):29class 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.instance33        return cls.instance
3434
35    def __init__(self):35    def __init__(self):
36        self.wishes = {}36        self.wishes = {}
37        self.kids_ages = {}37        self.kids_ages = {}
3838
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] = wish41        self.wishes[kid] = wish
4242
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] = wish47        self.wishes[kid] = wish
4848
49    def __iter__(self):49    def __iter__(self):
50        return iter(self.wishes.values())     # is this right50        return iter(self.wishes.values())     # is this right
5151
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] = 155                self.kids_ages[every_kid] = 1
56            else:56            else:
57                self.kids_ages[every_kid] += 157                self.kids_ages[every_kid] += 1
5858
59        if not self.wishes:59        if not self.wishes:
60            return60            return
6161
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] += 164            wish_counts[wish] += 1
65        most_wished = max(wish_counts.items(), key=lambda x: x[1])[0]  # is this actually right65        most_wished = max(wish_counts.items(), key=lambda x: x[1])[0]  # is this actually right
6666
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                continue69                continue
7070
71            if is_bad_kid(every_kid):71            if is_bad_kid(every_kid):
72                every_kid("coal")72                every_kid("coal")
7373
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)
8080
81            every_kid._threw_exception = False81            every_kid._threw_exception = False
8282
83        self.wishes.clear()83        self.wishes.clear()
8484
8585
86class Kid(type):86class Kid(type):
87    _all_kids = set()87    _all_kids = set()
8888
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)
9494
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 = False98        instance._threw_exception = False
99        if not callable(instance):99        if not callable(instance):
100            raise NotImplementedError("Стига де! Това все пак са деца! Трябва да можем да им викаме!")100            raise NotImplementedError("Стига де! Това все пак са деца! Трябва да можем да им викаме!")
101        return instance101        return instance
102102
103    @staticmethod103    @staticmethod
104    def get_all_kids():104    def get_all_kids():
105        return Kid._all_kids105        return Kid._all_kids
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

t1import ret1import re
2from collections import defaultdict2from collections import defaultdict
33
44
5def extract_wish(text):5def 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)
88
99
10def extract_kid_id(text):10def 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))
1313
1414
15def track_exceptions(method):  # as in the lection15def 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 = True20            self._threw_exception = True
21            raise21            raise
22    return wrapper22    return wrapper
2323
2424
25def is_bad_kid(kid):25def is_bad_kid(kid):
26    return kid._threw_exception26    return kid._threw_exception
2727
2828
29class Santa(object):29class 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.instance33        return cls.instance
3434
35    def __init__(self):35    def __init__(self):
36        self.wishes = {}36        self.wishes = {}
37        self.kids_ages = {}37        self.kids_ages = {}
3838
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] = wish41        self.wishes[kid] = wish
4242
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] = wish47        self.wishes[kid] = wish
4848
49    def __iter__(self):49    def __iter__(self):
50        return iter(self.wishes.values())     # is this right50        return iter(self.wishes.values())     # is this right
5151
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] = 155                self.kids_ages[every_kid] = 1
56            else:56            else:
57                self.kids_ages[every_kid] += 157                self.kids_ages[every_kid] += 1
5858
59        if not self.wishes:59        if not self.wishes:
60            return60            return
6161
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] += 164            wish_counts[wish] += 1
65        most_wished = max(wish_counts.items(), key=lambda x: x[1])[0]  # is this actually right65        most_wished = max(wish_counts.items(), key=lambda x: x[1])[0]  # is this actually right
6666
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                continue69                continue
7070
71            if is_bad_kid(every_kid):71            if is_bad_kid(every_kid):
72                every_kid("coal")72                every_kid("coal")
7373
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)
8080
81            every_kid._threw_exception = False81            every_kid._threw_exception = False
8282
83        self.wishes.clear()83        self.wishes.clear()
8484
8585
86class Kid(type):86class Kid(type):
87    _all_kids = set()87    _all_kids = set()
8888
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)
9494
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 = False98        instance._threw_exception = False
99        if not callable(instance):99        if not callable(instance):
100            raise NotImplementedError("Стига де! Това все пак са деца! Трябва да можем да им викаме!")100            raise NotImplementedError("Стига де! Това все пак са деца! Трябва да можем да им викаме!")
101        return instance101        return instance
102102
103    @staticmethod103    @staticmethod
104    def get_all_kids():104    def get_all_kids():
105        return Kid._all_kids105        return Kid._all_kids
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

t1import ret1import re
2from collections import defaultdict2from collections import defaultdict
33
44
5def extract_wish(text):5def 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)
88
99
10def extract_kid_id(text):10def 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))
1313
1414
15def track_exceptions(method):  # as in the lection15def 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 = True20            self._threw_exception = True
21            raise21            raise
22    return wrapper22    return wrapper
2323
2424
25def is_bad_kid(kid):25def is_bad_kid(kid):
26    return kid._threw_exception26    return kid._threw_exception
2727
2828
29class Santa(object):29class 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.instance33        return cls.instance
3434
35    def __init__(self):35    def __init__(self):
36        self.wishes = {}36        self.wishes = {}
37        self.kids_ages = {}37        self.kids_ages = {}
3838
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] = wish41        self.wishes[kid] = wish
4242
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] = wish47        self.wishes[kid] = wish
4848
49    def __iter__(self):49    def __iter__(self):
50        return iter(self.wishes.values())     # is this right50        return iter(self.wishes.values())     # is this right
5151
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] = 155                self.kids_ages[every_kid] = 1
56            else:56            else:
57                self.kids_ages[every_kid] += 157                self.kids_ages[every_kid] += 1
5858
59        if not self.wishes:59        if not self.wishes:
60            return60            return
6161
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] += 164            wish_counts[wish] += 1
65        most_wished = max(wish_counts.items(), key=lambda x: x[1])[0]  # is this actually right65        most_wished = max(wish_counts.items(), key=lambda x: x[1])[0]  # is this actually right
6666
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                continue69                continue
7070
71            if is_bad_kid(every_kid):71            if is_bad_kid(every_kid):
72                every_kid("coal")72                every_kid("coal")
7373
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)
8080
81            every_kid._threw_exception = False81            every_kid._threw_exception = False
8282
83        self.wishes.clear()83        self.wishes.clear()
8484
8585
86class Kid(type):86class Kid(type):
87    _all_kids = set()87    _all_kids = set()
8888
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)
9494
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 = False98        instance._threw_exception = False
99        if not callable(instance):99        if not callable(instance):
100            raise NotImplementedError("Стига де! Това все пак са деца! Трябва да можем да им викаме!")100            raise NotImplementedError("Стига де! Това все пак са деца! Трябва да можем да им викаме!")
101        return instance101        return instance
102102
103    @staticmethod103    @staticmethod
104    def get_all_kids():104    def get_all_kids():
105        return Kid._all_kids105        return Kid._all_kids
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

t1import ret1import re
2from collections import defaultdict2from collections import defaultdict
33
44
5def extract_wish(text):5def 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)
88
99
10def extract_kid_id(text):10def 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))
1313
1414
15def track_exceptions(method):  # as in the lection15def 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 = True20            self._threw_exception = True
21            raise21            raise
22    return wrapper22    return wrapper
2323
2424
25def is_bad_kid(kid):25def is_bad_kid(kid):
26    return kid._threw_exception26    return kid._threw_exception
2727
2828
29class Santa(object):29class 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.instance33        return cls.instance
3434
35    def __init__(self):35    def __init__(self):
36        self.wishes = {}36        self.wishes = {}
37        self.kids_ages = {}37        self.kids_ages = {}
3838
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] = wish41        self.wishes[kid] = wish
4242
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] = wish47        self.wishes[kid] = wish
4848
49    def __iter__(self):49    def __iter__(self):
50        return iter(self.wishes.values())     # is this right50        return iter(self.wishes.values())     # is this right
5151
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] = 155                self.kids_ages[every_kid] = 1
56            else:56            else:
57                self.kids_ages[every_kid] += 157                self.kids_ages[every_kid] += 1
5858
59        if not self.wishes:59        if not self.wishes:
60            return60            return
6161
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] += 164            wish_counts[wish] += 1
65        most_wished = max(wish_counts.items(), key=lambda x: x[1])[0]  # is this actually right65        most_wished = max(wish_counts.items(), key=lambda x: x[1])[0]  # is this actually right
6666
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                continue69                continue
7070
71            if is_bad_kid(every_kid):71            if is_bad_kid(every_kid):
72                every_kid("coal")72                every_kid("coal")
7373
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)
8080
81            every_kid._threw_exception = False81            every_kid._threw_exception = False
8282
83        self.wishes.clear()83        self.wishes.clear()
8484
8585
86class Kid(type):86class Kid(type):
87    _all_kids = set()87    _all_kids = set()
8888
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)
9494
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 = False98        instance._threw_exception = False
99        if not callable(instance):99        if not callable(instance):
100            raise NotImplementedError("Стига де! Това все пак са деца! Трябва да можем да им викаме!")100            raise NotImplementedError("Стига де! Това все пак са деца! Трябва да можем да им викаме!")
101        return instance101        return instance
102102
103    @staticmethod103    @staticmethod
104    def get_all_kids():104    def get_all_kids():
105        return Kid._all_kids105        return Kid._all_kids
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

t1import ret1import re
2from collections import defaultdict2from collections import defaultdict
33
44
5def extract_wish(text):5def 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)
88
99
10def extract_kid_id(text):10def 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))
1313
1414
15def track_exceptions(method):  # as in the lection15def 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 = True20            self._threw_exception = True
21            raise21            raise
22    return wrapper22    return wrapper
2323
2424
25def is_bad_kid(kid):25def is_bad_kid(kid):
26    return kid._threw_exception26    return kid._threw_exception
2727
2828
29class Santa(object):29class 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.instance33        return cls.instance
3434
35    def __init__(self):35    def __init__(self):
36        self.wishes = {}36        self.wishes = {}
37        self.kids_ages = {}37        self.kids_ages = {}
3838
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] = wish41        self.wishes[kid] = wish
4242
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] = wish47        self.wishes[kid] = wish
4848
49    def __iter__(self):49    def __iter__(self):
50        return iter(self.wishes.values())     # is this right50        return iter(self.wishes.values())     # is this right
5151
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] = 155                self.kids_ages[every_kid] = 1
56            else:56            else:
57                self.kids_ages[every_kid] += 157                self.kids_ages[every_kid] += 1
5858
59        if not self.wishes:59        if not self.wishes:
60            return60            return
6161
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] += 164            wish_counts[wish] += 1
65        most_wished = max(wish_counts.items(), key=lambda x: x[1])[0]  # is this actually right65        most_wished = max(wish_counts.items(), key=lambda x: x[1])[0]  # is this actually right
6666
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                continue69                continue
7070
71            if is_bad_kid(every_kid):71            if is_bad_kid(every_kid):
72                every_kid("coal")72                every_kid("coal")
7373
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)
8080
81            every_kid._threw_exception = False81            every_kid._threw_exception = False
8282
83        self.wishes.clear()83        self.wishes.clear()
8484
8585
86class Kid(type):86class Kid(type):
87    _all_kids = set()87    _all_kids = set()
8888
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)
9494
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 = False98        instance._threw_exception = False
99        if not callable(instance):99        if not callable(instance):
100            raise NotImplementedError("Стига де! Това все пак са деца! Трябва да можем да им викаме!")100            raise NotImplementedError("Стига де! Това все пак са деца! Трябва да можем да им викаме!")
101        return instance101        return instance
102102
103    @staticmethod103    @staticmethod
104    def get_all_kids():104    def get_all_kids():
105        return Kid._all_kids105        return Kid._all_kids
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

t1import ret1import re
2from collections import defaultdict2from collections import defaultdict
33
44
5def extract_wish(text):5def 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)
88
99
10def extract_kid_id(text):10def 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))
1313
1414
15def track_exceptions(method):  # as in the lection15def 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 = True20            self._threw_exception = True
21            raise21            raise
22    return wrapper22    return wrapper
2323
2424
25def is_bad_kid(kid):25def is_bad_kid(kid):
26    return kid._threw_exception26    return kid._threw_exception
2727
2828
29class Santa(object):29class 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.instance33        return cls.instance
3434
35    def __init__(self):35    def __init__(self):
36        self.wishes = {}36        self.wishes = {}
37        self.kids_ages = {}37        self.kids_ages = {}
3838
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] = wish41        self.wishes[kid] = wish
4242
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] = wish47        self.wishes[kid] = wish
4848
49    def __iter__(self):49    def __iter__(self):
50        return iter(self.wishes.values())     # is this right50        return iter(self.wishes.values())     # is this right
5151
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] = 155                self.kids_ages[every_kid] = 1
56            else:56            else:
57                self.kids_ages[every_kid] += 157                self.kids_ages[every_kid] += 1
5858
59        if not self.wishes:59        if not self.wishes:
60            return60            return
6161
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] += 164            wish_counts[wish] += 1
65        most_wished = max(wish_counts.items(), key=lambda x: x[1])[0]  # is this actually right65        most_wished = max(wish_counts.items(), key=lambda x: x[1])[0]  # is this actually right
6666
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                continue69                continue
7070
71            if is_bad_kid(every_kid):71            if is_bad_kid(every_kid):
72                every_kid("coal")72                every_kid("coal")
7373
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)
8080
81            every_kid._threw_exception = False81            every_kid._threw_exception = False
8282
83        self.wishes.clear()83        self.wishes.clear()
8484
8585
86class Kid(type):86class Kid(type):
87    _all_kids = set()87    _all_kids = set()
8888
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)
9494
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 = False98        instance._threw_exception = False
99        if not callable(instance):99        if not callable(instance):
100            raise NotImplementedError("Стига де! Това все пак са деца! Трябва да можем да им викаме!")100            raise NotImplementedError("Стига де! Това все пак са деца! Трябва да можем да им викаме!")
101        return instance101        return instance
102102
103    @staticmethod103    @staticmethod
104    def get_all_kids():104    def get_all_kids():
105        return Kid._all_kids105        return Kid._all_kids
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

t1import ret1import re
2from collections import defaultdict2from collections import defaultdict
33
44
5def extract_wish(text):5def 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)
88
99
10def extract_kid_id(text):10def 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))
1313
1414
15def track_exceptions(method):  # as in the lection15def 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 = True20            self._threw_exception = True
21            raise21            raise
22    return wrapper22    return wrapper
2323
2424
25def is_bad_kid(kid):25def is_bad_kid(kid):
26    return kid._threw_exception26    return kid._threw_exception
2727
2828
29class Santa(object):29class 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.instance33        return cls.instance
3434
35    def __init__(self):35    def __init__(self):
36        self.wishes = {}36        self.wishes = {}
37        self.kids_ages = {}37        self.kids_ages = {}
3838
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] = wish41        self.wishes[kid] = wish
4242
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] = wish47        self.wishes[kid] = wish
4848
49    def __iter__(self):49    def __iter__(self):
50        return iter(self.wishes.values())     # is this right50        return iter(self.wishes.values())     # is this right
5151
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] = 155                self.kids_ages[every_kid] = 1
56            else:56            else:
57                self.kids_ages[every_kid] += 157                self.kids_ages[every_kid] += 1
5858
59        if not self.wishes:59        if not self.wishes:
60            return60            return
6161
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] += 164            wish_counts[wish] += 1
65        most_wished = max(wish_counts.items(), key=lambda x: x[1])[0]  # is this actually right65        most_wished = max(wish_counts.items(), key=lambda x: x[1])[0]  # is this actually right
6666
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                continue69                continue
7070
71            if is_bad_kid(every_kid):71            if is_bad_kid(every_kid):
72                every_kid("coal")72                every_kid("coal")
7373
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)
8080
81            every_kid._threw_exception = False81            every_kid._threw_exception = False
8282
83        self.wishes.clear()83        self.wishes.clear()
8484
8585
86class Kid(type):86class Kid(type):
87    _all_kids = set()87    _all_kids = set()
8888
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)
9494
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 = False98        instance._threw_exception = False
99        if not callable(instance):99        if not callable(instance):
100            raise NotImplementedError("Стига де! Това все пак са деца! Трябва да можем да им викаме!")100            raise NotImplementedError("Стига де! Това все пак са деца! Трябва да можем да им викаме!")
101        return instance101        return instance
102102
103    @staticmethod103    @staticmethod
104    def get_all_kids():104    def get_all_kids():
105        return Kid._all_kids105        return Kid._all_kids
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

t1import ret1import re
2from collections import defaultdict2from collections import defaultdict
33
44
5def extract_wish(text):5def 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)
88
99
10def extract_kid_id(text):10def 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))
1313
1414
15def track_exceptions(method):  # as in the lection15def 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 = True20            self._threw_exception = True
21            raise21            raise
22    return wrapper22    return wrapper
2323
2424
25def is_bad_kid(kid):25def is_bad_kid(kid):
26    return kid._threw_exception26    return kid._threw_exception
2727
2828
29class Santa(object):29class 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.instance33        return cls.instance
3434
35    def __init__(self):35    def __init__(self):
36        self.wishes = {}36        self.wishes = {}
37        self.kids_ages = {}37        self.kids_ages = {}
3838
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] = wish41        self.wishes[kid] = wish
4242
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] = wish47        self.wishes[kid] = wish
4848
49    def __iter__(self):49    def __iter__(self):
50        return iter(self.wishes.values())     # is this right50        return iter(self.wishes.values())     # is this right
5151
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] = 155                self.kids_ages[every_kid] = 1
56            else:56            else:
57                self.kids_ages[every_kid] += 157                self.kids_ages[every_kid] += 1
5858
59        if not self.wishes:59        if not self.wishes:
60            return60            return
6161
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] += 164            wish_counts[wish] += 1
65        most_wished = max(wish_counts.items(), key=lambda x: x[1])[0]  # is this actually right65        most_wished = max(wish_counts.items(), key=lambda x: x[1])[0]  # is this actually right
6666
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                continue69                continue
7070
71            if is_bad_kid(every_kid):71            if is_bad_kid(every_kid):
72                every_kid("coal")72                every_kid("coal")
7373
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)
8080
81            every_kid._threw_exception = False81            every_kid._threw_exception = False
8282
83        self.wishes.clear()83        self.wishes.clear()
8484
8585
86class Kid(type):86class Kid(type):
87    _all_kids = set()87    _all_kids = set()
8888
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)
9494
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 = False98        instance._threw_exception = False
99        if not callable(instance):99        if not callable(instance):
100            raise NotImplementedError("Стига де! Това все пак са деца! Трябва да можем да им викаме!")100            raise NotImplementedError("Стига де! Това все пак са деца! Трябва да можем да им викаме!")
101        return instance101        return instance
102102
103    @staticmethod103    @staticmethod
104    def get_all_kids():104    def get_all_kids():
105        return Kid._all_kids105        return Kid._all_kids
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

t1import ret1import re
2from collections import defaultdict2from collections import defaultdict
33
44
5def extract_wish(text):5def 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)
88
99
10def extract_kid_id(text):10def 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))
1313
1414
15def track_exceptions(method):  # as in the lection15def 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 = True20            self._threw_exception = True
21            raise21            raise
22    return wrapper22    return wrapper
2323
2424
25def is_bad_kid(kid):25def is_bad_kid(kid):
26    return kid._threw_exception26    return kid._threw_exception
2727
2828
29class Santa(object):29class 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.instance33        return cls.instance
3434
35    def __init__(self):35    def __init__(self):
36        self.wishes = {}36        self.wishes = {}
37        self.kids_ages = {}37        self.kids_ages = {}
3838
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] = wish41        self.wishes[kid] = wish
4242
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] = wish47        self.wishes[kid] = wish
4848
49    def __iter__(self):49    def __iter__(self):
50        return iter(self.wishes.values())     # is this right50        return iter(self.wishes.values())     # is this right
5151
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] = 155                self.kids_ages[every_kid] = 1
56            else:56            else:
57                self.kids_ages[every_kid] += 157                self.kids_ages[every_kid] += 1
5858
59        if not self.wishes:59        if not self.wishes:
60            return60            return
6161
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] += 164            wish_counts[wish] += 1
65        most_wished = max(wish_counts.items(), key=lambda x: x[1])[0]  # is this actually right65        most_wished = max(wish_counts.items(), key=lambda x: x[1])[0]  # is this actually right
6666
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                continue69                continue
7070
71            if is_bad_kid(every_kid):71            if is_bad_kid(every_kid):
72                every_kid("coal")72                every_kid("coal")
7373
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)
8080
81            every_kid._threw_exception = False81            every_kid._threw_exception = False
8282
83        self.wishes.clear()83        self.wishes.clear()
8484
8585
86class Kid(type):86class Kid(type):
87    _all_kids = set()87    _all_kids = set()
8888
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)
9494
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 = False98        instance._threw_exception = False
99        if not callable(instance):99        if not callable(instance):
100            raise NotImplementedError("Стига де! Това все пак са деца! Трябва да можем да им викаме!")100            raise NotImplementedError("Стига де! Това все пак са деца! Трябва да можем да им викаме!")
101        return instance101        return instance
102102
103    @staticmethod103    @staticmethod
104    def get_all_kids():104    def get_all_kids():
105        return Kid._all_kids105        return Kid._all_kids
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

t1import ret1import re
2from collections import defaultdict2from collections import defaultdict
33
44
5def extract_wish(text):5def 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)
88
99
10def extract_kid_id(text):10def 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))
1313
1414
15def track_exceptions(method):  # as in the lection15def 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 = True20            self._threw_exception = True
21            raise21            raise
22    return wrapper22    return wrapper
2323
2424
25def is_bad_kid(kid):25def is_bad_kid(kid):
26    return kid._threw_exception26    return kid._threw_exception
2727
2828
29class Santa(object):29class 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.instance33        return cls.instance
3434
35    def __init__(self):35    def __init__(self):
36        self.wishes = {}36        self.wishes = {}
37        self.kids_ages = {}37        self.kids_ages = {}
3838
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] = wish41        self.wishes[kid] = wish
4242
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] = wish47        self.wishes[kid] = wish
4848
49    def __iter__(self):49    def __iter__(self):
50        return iter(self.wishes.values())     # is this right50        return iter(self.wishes.values())     # is this right
5151
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] = 155                self.kids_ages[every_kid] = 1
56            else:56            else:
57                self.kids_ages[every_kid] += 157                self.kids_ages[every_kid] += 1
5858
59        if not self.wishes:59        if not self.wishes:
60            return60            return
6161
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] += 164            wish_counts[wish] += 1
65        most_wished = max(wish_counts.items(), key=lambda x: x[1])[0]  # is this actually right65        most_wished = max(wish_counts.items(), key=lambda x: x[1])[0]  # is this actually right
6666
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                continue69                continue
7070
71            if is_bad_kid(every_kid):71            if is_bad_kid(every_kid):
72                every_kid("coal")72                every_kid("coal")
7373
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)
8080
81            every_kid._threw_exception = False81            every_kid._threw_exception = False
8282
83        self.wishes.clear()83        self.wishes.clear()
8484
8585
86class Kid(type):86class Kid(type):
87    _all_kids = set()87    _all_kids = set()
8888
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)
9494
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 = False98        instance._threw_exception = False
99        if not callable(instance):99        if not callable(instance):
100            raise NotImplementedError("Стига де! Това все пак са деца! Трябва да можем да им викаме!")100            raise NotImplementedError("Стига де! Това все пак са деца! Трябва да можем да им викаме!")
101        return instance101        return instance
102102
103    @staticmethod103    @staticmethod
104    def get_all_kids():104    def get_all_kids():
105        return Kid._all_kids105        return Kid._all_kids
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

t1import ret1import re
2from collections import defaultdict2from collections import defaultdict
33
44
5def extract_wish(text):5def 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)
88
99
10def extract_kid_id(text):10def 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))
1313
1414
15def track_exceptions(method):  # as in the lection15def 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 = True20            self._threw_exception = True
21            raise21            raise
22    return wrapper22    return wrapper
2323
2424
25def is_bad_kid(kid):25def is_bad_kid(kid):
26    return kid._threw_exception26    return kid._threw_exception
2727
2828
29class Santa(object):29class 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.instance33        return cls.instance
3434
35    def __init__(self):35    def __init__(self):
36        self.wishes = {}36        self.wishes = {}
37        self.kids_ages = {}37        self.kids_ages = {}
3838
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] = wish41        self.wishes[kid] = wish
4242
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] = wish47        self.wishes[kid] = wish
4848
49    def __iter__(self):49    def __iter__(self):
50        return iter(self.wishes.values())     # is this right50        return iter(self.wishes.values())     # is this right
5151
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] = 155                self.kids_ages[every_kid] = 1
56            else:56            else:
57                self.kids_ages[every_kid] += 157                self.kids_ages[every_kid] += 1
5858
59        if not self.wishes:59        if not self.wishes:
60            return60            return
6161
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] += 164            wish_counts[wish] += 1
65        most_wished = max(wish_counts.items(), key=lambda x: x[1])[0]  # is this actually right65        most_wished = max(wish_counts.items(), key=lambda x: x[1])[0]  # is this actually right
6666
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                continue69                continue
7070
71            if is_bad_kid(every_kid):71            if is_bad_kid(every_kid):
72                every_kid("coal")72                every_kid("coal")
7373
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)
8080
81            every_kid._threw_exception = False81            every_kid._threw_exception = False
8282
83        self.wishes.clear()83        self.wishes.clear()
8484
8585
86class Kid(type):86class Kid(type):
87    _all_kids = set()87    _all_kids = set()
8888
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)
9494
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 = False98        instance._threw_exception = False
99        if not callable(instance):99        if not callable(instance):
100            raise NotImplementedError("Стига де! Това все пак са деца! Трябва да можем да им викаме!")100            raise NotImplementedError("Стига де! Това все пак са деца! Трябва да можем да им викаме!")
101        return instance101        return instance
102102
103    @staticmethod103    @staticmethod
104    def get_all_kids():104    def get_all_kids():
105        return Kid._all_kids105        return Kid._all_kids
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

t1import ret1import re
2from collections import defaultdict2from collections import defaultdict
33
44
5def extract_wish(text):5def 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)
88
99
10def extract_kid_id(text):10def 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))
1313
1414
15def track_exceptions(method):  # as in the lection15def 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 = True20            self._threw_exception = True
21            raise21            raise
22    return wrapper22    return wrapper
2323
2424
25def is_bad_kid(kid):25def is_bad_kid(kid):
26    return kid._threw_exception26    return kid._threw_exception
2727
2828
29class Santa(object):29class 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.instance33        return cls.instance
3434
35    def __init__(self):35    def __init__(self):
36        self.wishes = {}36        self.wishes = {}
37        self.kids_ages = {}37        self.kids_ages = {}
3838
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] = wish41        self.wishes[kid] = wish
4242
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] = wish47        self.wishes[kid] = wish
4848
49    def __iter__(self):49    def __iter__(self):
50        return iter(self.wishes.values())     # is this right50        return iter(self.wishes.values())     # is this right
5151
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] = 155                self.kids_ages[every_kid] = 1
56            else:56            else:
57                self.kids_ages[every_kid] += 157                self.kids_ages[every_kid] += 1
5858
59        if not self.wishes:59        if not self.wishes:
60            return60            return
6161
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] += 164            wish_counts[wish] += 1
65        most_wished = max(wish_counts.items(), key=lambda x: x[1])[0]  # is this actually right65        most_wished = max(wish_counts.items(), key=lambda x: x[1])[0]  # is this actually right
6666
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                continue69                continue
7070
71            if is_bad_kid(every_kid):71            if is_bad_kid(every_kid):
72                every_kid("coal")72                every_kid("coal")
7373
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)
8080
81            every_kid._threw_exception = False81            every_kid._threw_exception = False
8282
83        self.wishes.clear()83        self.wishes.clear()
8484
8585
86class Kid(type):86class Kid(type):
87    _all_kids = set()87    _all_kids = set()
8888
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)
9494
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 = False98        instance._threw_exception = False
99        if not callable(instance):99        if not callable(instance):
100            raise NotImplementedError("Стига де! Това все пак са деца! Трябва да можем да им викаме!")100            raise NotImplementedError("Стига де! Това все пак са деца! Трябва да можем да им викаме!")
101        return instance101        return instance
102102
103    @staticmethod103    @staticmethod
104    def get_all_kids():104    def get_all_kids():
105        return Kid._all_kids105        return Kid._all_kids
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op