Предизвикателства > She's a witch! > Решения > Решението на Дария Лазарова

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

0 точки общо

0 успешни теста
1 неуспешни теста
Код

 1def logic_mixin_factory(mass, mass_attr_name, material, material_attr_name, float_method_name):
 2    class LogicMixin:
 3        def _is_valid_witch(self, curr_combination):
 4            if isinstance(curr_combination, str):
 5                return hasattr(self, curr_combination)
 6            attr_name, attr_value = curr_combination
 7            return attr_name in self.__dict__ and self.__dict__[attr_name] == attr_value
 8        def is_a_witch(self):
 9            combinations = (mass_attr_name, mass), (material_attr_name, material), float_method_name
10            for curr_combination in combinations:
11                if self._is_valid_witch(curr_combination):
12                    return "Burn her!"
13            return "No, but it's a pity, cuz she looks like a witch!"
14    return LogicMixin

F
======================================================================
FAIL: test_realcase (test.TestWitchMixinFactory)
Real test for the Witch factory.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 50, in test_realcase
self.assertEqual(FloatFalse().is_a_witch(), self.RESULT_FALSE)
AssertionError: 'Burn her!' != "No, but it's a pity, cuz she looks like a witch!"
- Burn her!
+ No, but it's a pity, cuz she looks like a witch!

----------------------------------------------------------------------
Ran 1 test in 0.000s

FAILED (failures=1)

Дискусия
Георги Кунчев
08.11.2023 21:03

Определено предпочитам първата версия. Но ата функция и а прекалено дълги редове, а и не мисля, че е удачно да живее извън `logic_mixin_factory`
История

n1def is_valid_witch(obj, attr_name, value = None):n
2    try:
3        if getattr(obj, attr_name) == value and value is not None or value == None and getattr(obj, attr_name):
4            return True
5    except AttributeError:
6        return False
7    return False
8 
9def logic_mixin_factory(mass, mass_attr_name, material, material_attr_name, float_method_name):1def logic_mixin_factory(mass, mass_attr_name, material, material_attr_name, float_method_name):
10    class LogicMixin:2    class LogicMixin:
nn3        def _is_valid_witch(self, curr_combination):
4            if isinstance(curr_combination, str):
5                return hasattr(self, curr_combination)
6            attr_name, attr_value = curr_combination
7            return attr_name in self.__dict__ and self.__dict__[attr_name] == attr_value
11        def is_a_witch(self):8        def is_a_witch(self):
n12            if(is_valid_witch(self, mass_attr_name, mass) or is_valid_witch(self, material_attr_name, material) or is_valid_witch(self, float_method_name)):n9            combinations = (mass_attr_name, mass), (material_attr_name, material), float_method_name
10            for curr_combination in combinations:
11                if self._is_valid_witch(curr_combination):
13                return "Burn her!"12                    return "Burn her!"
14            
15            return "No, but it's a pity, cuz she looks like a witch!"13            return "No, but it's a pity, cuz she looks like a witch!"
16    return LogicMixin14    return LogicMixin
1715
t18 t
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

nn1def is_valid_witch(obj, attr_name, value = None):
2    try:
3        if getattr(obj, attr_name) == value and value is not None or value == None and getattr(obj, attr_name):
4            return True
5    except AttributeError:
6        return False
7    return False
8 
1def logic_mixin_factory(mass, mass_attr_name, material, material_attr_name, float_method_name ):9def logic_mixin_factory(mass, mass_attr_name, material, material_attr_name, float_method_name):
2    class LogicMixin:10    class LogicMixin:
3        def is_a_witch(self):11        def is_a_witch(self):
n4            try:n12            if(is_valid_witch(self, mass_attr_name, mass) or is_valid_witch(self, material_attr_name, material) or is_valid_witch(self, float_method_name)):
5                if getattr(self, mass_attr_name) == mass:
6                    return "Burn her!"13                return "Burn her!"
7            except AttributeError:
8                pass
9            try:
10                if getattr(self, material_attr_name) == material:
11                    return "Burn her!"
12            except AttributeError:
13                pass
14            try:
15                if getattr(self, float_method_name):
16                    return "Burn her!"
17            except AttributeError:
18                pass
19            14            
20            return "No, but it's a pity, cuz she looks like a witch!"15            return "No, but it's a pity, cuz she looks like a witch!"
21    return LogicMixin16    return LogicMixin
2217
tt18 
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op