1import re
2
3all_children_with_age = {}
4store_children = {}
5
6class Santa():
7 presents_to_be_given = {}
8 the_most_wanted_presents = {}
9 def __new__(cls, *args, **kwargs):
10 if not hasattr(cls, 'instance'):
11 cls.instance = super(Santa, cls).__new__(cls)
12 return cls.instance
13
14 def __iter__(self):
15 return self.the_most_wanted_presents.__iter__()
16
17 def __call__(self, kid, letter):
18 present = self.__present(letter)
19 if present:
20 self.presents_to_be_given[id(kid)] = present
21 current = self.the_most_wanted_presents.get(present, 0)
22 self.the_most_wanted_presents[present] = current + 1
23
24 def __signiture(self, letter):
25 match = re.search(r'\n\s*\b(\d+)\b\s*', letter)
26 if match:
27 return match.group(1)
28 return ''
29
30 def __present(self, letter):
31 match = re.search(r'["\'](([a-zA-Z0-9]+\s*)*)["\']', letter)
32 if match:
33 return match.group(1)
34 return ''
35
36 def __matmul__(self, letter):
37 present = self.__present(letter)
38 id = self.__signiture(letter)
39 if present and id:
40 self.presents_to_be_given[id] = present
41 current = self.the_most_wanted_presents.get(present, 0)
42 self.the_most_wanted_presents[present] = current + 1
43
44 def xmas(self):
45 for kid_id, age in all_children_with_age.items():
46 if age < 5 and not self.__check_for_exception(store_children[kid_id]):
47 store_children[kid_id]()(self.__give_present(kid_id))
48 else:
49 store_children[kid_id]()('coal')
50 all_children_with_age[kid_id] += 1
51 self.presents_to_be_given.clear()
52 self.the_most_wanted_presents.clear()
53
54 def __get_the_most_wanted_present(self):
55 max_wished = 0
56 max_present = ''
57 for present, wished in self.the_most_wanted_presents.items():
58 if wished > max_wished:
59 max_present = present
60 max_wished = wished
61 return max_present
62
63 def __give_present(self, kid_id):
64 if not kid_id in self.presents_to_be_given:
65 return self.__get_the_most_wanted_present()
66 else:
67 return self.presents_to_be_given[kid_id]
68
69 def __check_for_exception(self, kid):
70 for name, value in kid.__dict__.items():
71 if callable(value) and not name.startswith('_'):
72 code = getattr(value, "__code__", None)
73 if code and 'raise' in code.co_names:
74 return True
75 return False
76
77
78class Kid(type):
79 def __new__(cls, name, bases, attr_dict):
80 if not '__call__' in attr_dict:
81 raise NotImplementedError('There is not __call__ method')
82 return type.__new__(cls, name, bases, attr_dict)
83
84#Не знам как да взема id на инстанцията, така не е валидно, но ако го взимам при подаването на
85#заявки към санта, то ще работи само при обажданията, но не при писмата и няма как да включа тези, които нямат желания
86 def __init__(self, *args, **kwargs):
87 all_children_with_age[id(self)] = 0
88 store_children[id(self)] = self
....F..FF.FFFF..FF.F
======================================================================
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_signature_matching (test.TestSanta.test_signature_matching)
Test matching present in the letter / call.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 118, in test_signature_matching
self.assertEqual(kid1.SECRET_PRESENT, 'toy1')
AssertionError: None != 'toy1'
======================================================================
FAIL: test_xmass (test.TestSanta.test_xmass)
Test a simple Christmas case.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 141, in test_xmass
self.assertEqual(kid1.SECRET_PRESENT, 'toy1')
AssertionError: None != 'toy1'
======================================================================
FAIL: test_xmass_kid_with_multiple_wishes (test.TestSanta.test_xmass_kid_with_multiple_wishes)
Test a Christmas with a kid who sends multiple wishes.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 187, in test_xmass_kid_with_multiple_wishes
self.assertEqual(kid1.SECRET_PRESENT, 'toy3')
AssertionError: None != 'toy3'
======================================================================
FAIL: test_xmass_kid_without_a_wish (test.TestSanta.test_xmass_kid_without_a_wish)
Test a Christmas with a kids that hasn't sent a wish.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 175, in test_xmass_kid_without_a_wish
self.assertEqual(kid1.SECRET_PRESENT, 'toy1')
AssertionError: None != '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: None != 'coal'
======================================================================
FAIL: test_xmass_private_with_error (test.TestSanta.test_xmass_private_with_error)
Test a Christmas with not-so-naughty kids.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 240, in test_xmass_private_with_error
self.assertEqual(kid1.SECRET_PRESENT, 'sirenie')
AssertionError: None != 'sirenie'
======================================================================
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 248, in test_xmass_public_with_no_error
self.assertEqual(kid1.SECRET_PRESENT, 'sirenie')
AssertionError: None != 'sirenie'
======================================================================
FAIL: test_xmass_years_under_5 (test.TestSanta.test_xmass_years_under_5)
Test with passing years with a kid under 5 years old.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 194, in test_xmass_years_under_5
self.assertEqual(kid1.SECRET_PRESENT, 'toy1')
AssertionError: None != 'toy1'
----------------------------------------------------------------------
Ran 20 tests in 0.027s
FAILED (failures=10)
19.12.2024 17:31
19.12.2024 17:32
19.12.2024 17:35
19.12.2024 18:13
19.12.2024 18:17
19.12.2024 18:16
19.12.2024 18:17
19.12.2024 18:18
19.12.2024 18:19