1import re
2MAX_AGE = 5
3
4class Santa(object):
5 """"Singleton class. Contains all kids and their wishes"""
6 _instance = None
7
8 #kid => present
9 _kids_presents = {}
10
11 def __find_signiture(self, letter):
12 return re.search(r'^\s*(\d+)\s*$', letter, flags=re.MULTILINE).group(1)
13
14 def __find_present(self, letter):
15 return re.search(r'(["\'])([A-Za-z0-9\s]*)\1', letter).group(2)
16
17 def __new__(cls):
18 if not cls._instance:
19 cls._instance = object.__new__(cls)
20 return cls._instance
21
22 def new_kid(self, kid):
23 self._kids_presents[kid] = None
24
25 def __matmul__(self, letter):
26 """Letter to Santa"""
27 signature = self.__find_signiture(letter)
28 present = self.__find_present(letter)
29 for kid in self._kids_presents.keys():
30 if str(id(kid)) == signature:
31 self._kids_presents[kid] = present
32 break
33
34 def __call__(self, kid, wish):
35 """Phone Santa"""
36 present = self.__find_present(wish)
37 self._kids_presents[kid] = present
38
39 def __iter__(self):
40 return self._generator()
41
42 def _generator(self):
43 for kid, present in self._kids_presents.items():
44 if present is not None:
45 yield present
46
47 def __most_wanted(self):
48 presents_counter = {}
49 curr_present = None
50 curr_max_count = 0
51 for kid, present in self._kids_presents.items():
52 if present:
53 presents_counter[present] = presents_counter.get(present, 0) + 1
54 if presents_counter[present] > curr_max_count:
55 curr_present = present
56 curr_max_count = presents_counter[present]
57 return curr_present
58
59 def xmas(self):
60 def_present = self.__most_wanted()
61
62 for kid, present in self._kids_presents.items():
63 if kid.age <= MAX_AGE and def_present:
64 if kid.naughty:
65 present = 'coal'
66 if not present:
67 present = def_present
68 kid(present)
69 self._kids_presents[kid] = None
70 kid.age += 1
71 kid.naughty = False
72
73
74class Kid(type):
75 """"Meta class. Ensures there is a call method in the class that will inherit the metacls. Has logic if a kid has thrown an exception to mark him as naughty"""
76 def __call__(cls, *args, **kwargs):
77 instance = super().__call__(*args, **kwargs)
78 Santa().new_kid(instance)
79 instance.age = 0
80 instance.naughty = False
81 return instance
82
83 def __new__(cls, name, bases, attr_dict):
84 if '__call__' not in attr_dict:
85 raise NotImplementedError('Оправи си детето?! Не чува, като го викаш!')
86 for key, value in attr_dict.items():
87 # because otherwise it wrappes all my dunders :((
88 if callable(value) and not str(key).startswith('_'):
89 func = value
90 def wrapped(self, *args, **kwargs):
91 try:
92 func(self, *args, **kwargs)
93 self.naughty = False
94 except Exception as e:
95 self.naughty = True
96 raise e
97 attr_dict[key] = wrapped
98 return super().__new__(cls, name, bases, attr_dict)
.......F.....F...FF.
======================================================================
FAIL: test_santa_gift_order (test.TestSanta.test_santa_gift_order)
Test ordering of the Santa iterator.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 260, in test_santa_gift_order
self.assertEqual(list(self.santa), ["toy2v2", "toy3", "toy1"])
AssertionError: Lists differ: ['toy1', 'toy2v2', 'toy3'] != ['toy2v2', 'toy3', 'toy1']
First differing element 0:
'toy1'
'toy2v2'
- ['toy1', 'toy2v2', 'toy3']
+ ['toy2v2', 'toy3', 'toy1']
======================================================================
FAIL: test_xmass_naughty (test.TestSanta.test_xmass_naughty)
Test a Christmas with naughty kids.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 222, in test_xmass_naughty
with self.assertRaises(ZeroDivisionError):
AssertionError: ZeroDivisionError not raised
======================================================================
FAIL: test_xmass_public_with_no_error (test.TestSanta.test_xmass_public_with_no_error)
Test a Christmas with not-so-naughty kids.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 246, in test_xmass_public_with_no_error
self.assertEqual(kid1.public_without_error(), 42)
AssertionError: None != 42
======================================================================
FAIL: test_xmass_years_5_and_over (test.TestSanta.test_xmass_years_5_and_over)
Test with passing years with kid aged 5 and over.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 213, in test_xmass_years_5_and_over
self.assertEqual(kid1.SECRET_PRESENT, None)
AssertionError: 'toy' != None
----------------------------------------------------------------------
Ran 20 tests in 0.024s
FAILED (failures=4)
t | 1 | from unittest.mock import Mock | t | 1 | import re |
2 | mock = Mock() | 2 | MAX_AGE = 5 | ||
3 | mock.assertt() | 3 | |||
4 | class Santa(object): | ||||
5 | """"Singleton class. Contains all kids and their wishes""" | ||||
6 | _instance = None | ||||
7 | |||||
8 | #kid => present | ||||
9 | _kids_presents = {} | ||||
10 | |||||
11 | def __find_signiture(self, letter): | ||||
12 | return re.search(r'^\s*(\d+)\s*$', letter, flags=re.MULTILINE).group(1) | ||||
13 | |||||
14 | def __find_present(self, letter): | ||||
15 | return re.search(r'(["\'])([A-Za-z0-9\s]*)\1', letter).group(2) | ||||
16 | |||||
17 | def __new__(cls): | ||||
18 | if not cls._instance: | ||||
19 | cls._instance = object.__new__(cls) | ||||
20 | return cls._instance | ||||
21 | |||||
22 | def new_kid(self, kid): | ||||
23 | self._kids_presents[kid] = None | ||||
24 | |||||
25 | def __matmul__(self, letter): | ||||
26 | """Letter to Santa""" | ||||
27 | signature = self.__find_signiture(letter) | ||||
28 | present = self.__find_present(letter) | ||||
29 | for kid in self._kids_presents.keys(): | ||||
30 | if str(id(kid)) == signature: | ||||
31 | self._kids_presents[kid] = present | ||||
32 | break | ||||
33 | |||||
34 | def __call__(self, kid, wish): | ||||
35 | """Phone Santa""" | ||||
36 | present = self.__find_present(wish) | ||||
37 | self._kids_presents[kid] = present | ||||
38 | |||||
39 | def __iter__(self): | ||||
40 | return self._generator() | ||||
41 | |||||
42 | def _generator(self): | ||||
43 | for kid, present in self._kids_presents.items(): | ||||
44 | if present is not None: | ||||
45 | yield present | ||||
46 | |||||
47 | def __most_wanted(self): | ||||
48 | presents_counter = {} | ||||
49 | curr_present = None | ||||
50 | curr_max_count = 0 | ||||
51 | for kid, present in self._kids_presents.items(): | ||||
52 | if present: | ||||
53 | presents_counter[present] = presents_counter.get(present, 0) + 1 | ||||
54 | if presents_counter[present] > curr_max_count: | ||||
55 | curr_present = present | ||||
56 | curr_max_count = presents_counter[present] | ||||
57 | return curr_present | ||||
58 | |||||
59 | def xmas(self): | ||||
60 | def_present = self.__most_wanted() | ||||
61 | |||||
62 | for kid, present in self._kids_presents.items(): | ||||
63 | if kid.age <= MAX_AGE and def_present: | ||||
64 | if kid.naughty: | ||||
65 | present = 'coal' | ||||
66 | if not present: | ||||
67 | present = def_present | ||||
68 | kid(present) | ||||
69 | self._kids_presents[kid] = None | ||||
70 | kid.age += 1 | ||||
71 | kid.naughty = False | ||||
72 | |||||
73 | |||||
74 | class Kid(type): | ||||
75 | """"Meta class. Ensures there is a call method in the class that will inherit the metacls. Has logic if a kid has thrown an exception to mark him as naughty""" | ||||
76 | def __call__(cls, *args, **kwargs): | ||||
77 | instance = super().__call__(*args, **kwargs) | ||||
78 | Santa().new_kid(instance) | ||||
79 | instance.age = 0 | ||||
80 | instance.naughty = False | ||||
81 | return instance | ||||
82 | |||||
83 | def __new__(cls, name, bases, attr_dict): | ||||
84 | if '__call__' not in attr_dict: | ||||
85 | raise NotImplementedError('Оправи си детето?! Не чува, като го викаш!') | ||||
86 | for key, value in attr_dict.items(): | ||||
87 | # because otherwise it wrappes all my dunders :(( | ||||
88 | if callable(value) and not str(key).startswith('_'): | ||||
89 | func = value | ||||
90 | def wrapped(self, *args, **kwargs): | ||||
91 | try: | ||||
92 | func(self, *args, **kwargs) | ||||
93 | self.naughty = False | ||||
94 | except Exception as e: | ||||
95 | self.naughty = True | ||||
96 | raise e | ||||
97 | attr_dict[key] = wrapped | ||||
98 | return super().__new__(cls, name, bases, attr_dict) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
19.12.2024 15:33
19.12.2024 15:33
19.12.2024 15:36
19.12.2024 15:34
19.12.2024 15:35
19.12.2024 15:35
19.12.2024 15:36
19.12.2024 15:37
19.12.2024 15:37
19.12.2024 15:38
19.12.2024 15:39