1import re
2
3
4class Singleton(type):
5 _instances = {}
6
7 def __call__(cls, *args, **kwargs):
8
9 if cls not in cls._instances:
10 instance = super().__call__(*args, **kwargs)
11 cls._instances[cls] = instance
12 return cls._instances[cls]
13
14
15class Santa(metaclass=Singleton):
16 __wishes = {}
17 __all_wishes = []
18 __ages_of_all_kids = {}
19
20 def __iter__(self):
21 self.num = 0
22 return self
23
24 def __next__(self):
25 if self.num < len(self.__all_wishes):
26 result = self.__all_wishes[self.num]
27 self.num += 1
28 return result
29 else:
30 raise StopIteration
31
32 def __matmul__(self, other):
33 if isinstance(other, str):
34 result = re.search(r'(["\'])[a-zA-Z0-9 ]+(\1)', other)
35 wish = result.group()[1:-1]
36 id_of_kid = re.findall(r'\n\s*(\d+)', other)[0]
37 self.__wishes[int(id_of_kid)] = wish
38 self.__all_wishes.append(wish)
39
40 def __call__(self, kid, letter):
41 result = re.search(r'(["\'])[a-zA-Z0-9 ]+(\1)', letter)
42 wish = result.group()[1:-1]
43 self.__wishes[id(kid)] = wish
44 self.__all_wishes.append(wish)
45
46 def xmas(self):
47 for kid in Kid.instances:
48 if self.__ages_of_all_kids.get(id(kid)) is None:
49 self.__ages_of_all_kids[id(kid)] = 0
50 self.__ages_of_all_kids[id(kid)] += 1
51 if self.__all_wishes and self.__ages_of_all_kids[id(kid)] <= 5:
52 if self.__wishes.get(id(kid)) is not None:
53 try:
54 kid(self.__wishes[id(kid)])
55 except Exception:
56 raise NotImplementedError(f'Instance {kid} is not callable!!')
57
58 else:
59 most_common_wish = max(self.__all_wishes, key=self.__all_wishes.count)
60 kid(most_common_wish)
61 self.__all_wishes.clear()
62
63
64
65class Kid(type):
66 instances = []
67 def __call__(cls, *args, **kwargs):
68 instance = super().__call__(*args, **kwargs)
69 cls.instances.append(instance)
70 return instance
.F.FF..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_call_and_mail_same_kid (test.TestSanta.test_call_and_mail_same_kid)
Test that calls and mails work for the same kid.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 71, in test_call_and_mail_same_kid
self.assertEqual(list(self.santa), ['toy1'])
AssertionError: Lists differ: ['toy1', 'toy1'] != ['toy1']
First list contains 1 additional elements.
First extra element 1:
'toy1'
- ['toy1', 'toy1']
+ ['toy1']
======================================================================
FAIL: test_iterable (test.TestSanta.test_iterable)
Ensure Santa can be iterated multiple times including overwriting presents.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 129, in test_iterable
self.assertEqual(list(self.santa), ['something', 'something else'])
AssertionError: Lists differ: ['something', 'something not used', 'something else'] != ['something', 'something else']
First differing element 1:
'something not used'
'something else'
First list contains 1 additional elements.
First extra element 2:
'something else'
- ['something', 'something not used', 'something else']
+ ['something', 'something else']
======================================================================
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: ['toy2', 'toy3', 'toy1', 'toy2v2'] != ['toy2v2', 'toy3', 'toy1']
First differing element 0:
'toy2'
'toy2v2'
First list contains 1 additional elements.
First extra element 3:
'toy2v2'
- ['toy2', 'toy3', 'toy1', 'toy2v2']
+ ['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 227, in test_xmass_naughty
self.assertEqual(kid1.SECRET_PRESENT, 'coal')
AssertionError: 'sirenie' != 'coal'
- sirenie
+ coal
----------------------------------------------------------------------
Ran 20 tests in 0.017s
FAILED (failures=5)
19.12.2024 15:25
19.12.2024 15:25
19.12.2024 15:26