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`
|
n | 1 | def 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 | |||||
9 | def logic_mixin_factory(mass, mass_attr_name, material, material_attr_name, float_method_name): | 1 | def logic_mixin_factory(mass, mass_attr_name, material, material_attr_name, float_method_name): | ||
10 | class LogicMixin: | 2 | class LogicMixin: | ||
n | n | 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 | ||||
11 | def is_a_witch(self): | 8 | def is_a_witch(self): | ||
n | 12 | 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)): | n | 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): | ||||
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 LogicMixin | 14 | return LogicMixin | ||
17 | 15 | ||||
t | 18 | t |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
n | n | 1 | def 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 | |||||
1 | def logic_mixin_factory(mass, mass_attr_name, material, material_attr_name, float_method_name ): | 9 | def 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): | ||
n | 4 | try: | n | 12 | 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 LogicMixin | 16 | return LogicMixin | ||
22 | 17 | ||||
t | t | 18 |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|