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

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

0 точки общо

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

 1def logic_mixin_factory(mass, mass_attr_name, material, material_attr_name,
 2                        float_method_name):
 3    class LogicMixin:
 4
 5        def __init__(self):
 6            #self.mass = mass
 7            self.mass_attr_name = mass
 8            #self.material = material
 9            self.material_attr_name = material
10
11        def is_a_witch(self):
12
13            if (hasattr(self, mass_attr_name) and 
14                getattr(self, mass_attr_name) == mass):
15                return "Burn her!"
16            if (hasattr(self, material_attr_name) and 
17                getattr(self, material_attr_name) == material):
18                return "Burn her!"
19            if hasattr(self, float_method_name):
20                return "Burn her!"
21            return "No, but it's a pity, cuz she looks like a witch!"
22
23    return LogicMixin
24
25log_mixin = logic_mixin_factory(20, 'mass', 'wood', 'material', 'float')
26
27class Woman(log_mixin):
28    pass
29
30woman = Woman()
31print(woman.is_a_witch())

No, but it's a pity, cuz she looks like a witch!
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)

Дискусия
История

f1def logic_mixin_factory(mass, mass_attr_name, material, material_attr_name,f1def logic_mixin_factory(mass, mass_attr_name, material, material_attr_name,
2                        float_method_name):2                        float_method_name):
3    class LogicMixin:3    class LogicMixin:
44
5        def __init__(self):5        def __init__(self):
6            #self.mass = mass6            #self.mass = mass
7            self.mass_attr_name = mass7            self.mass_attr_name = mass
8            #self.material = material8            #self.material = material
9            self.material_attr_name = material9            self.material_attr_name = material
1010
11        def is_a_witch(self):11        def is_a_witch(self):
1212
n13            if hasattr(self, mass_attr_name) \n13            if (hasattr(self, mass_attr_name) and 
14                and getattr(self, mass_attr_name) == mass:14                getattr(self, mass_attr_name) == mass):
15                return "Burn her!"15                return "Burn her!"
t16            if hasattr(self, material_attr_name) \t16            if (hasattr(self, material_attr_name) and 
17                and getattr(self, material_attr_name) == material:17                getattr(self, material_attr_name) == material):
18                return "Burn her!"18                return "Burn her!"
19            if hasattr(self, float_method_name):19            if hasattr(self, float_method_name):
20                return "Burn her!"20                return "Burn her!"
21            return "No, but it's a pity, cuz she looks like a witch!"21            return "No, but it's a pity, cuz she looks like a witch!"
2222
23    return LogicMixin23    return LogicMixin
2424
25log_mixin = logic_mixin_factory(20, 'mass', 'wood', 'material', 'float')25log_mixin = logic_mixin_factory(20, 'mass', 'wood', 'material', 'float')
2626
27class Woman(log_mixin):27class Woman(log_mixin):
28    pass28    pass
2929
30woman = Woman()30woman = Woman()
31print(woman.is_a_witch())31print(woman.is_a_witch())
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op