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)
f | 1 | def logic_mixin_factory(mass, mass_attr_name, material, material_attr_name, | f | 1 | def 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: | ||
4 | 4 | ||||
5 | def __init__(self): | 5 | def __init__(self): | ||
6 | #self.mass = mass | 6 | #self.mass = mass | ||
7 | self.mass_attr_name = mass | 7 | self.mass_attr_name = mass | ||
8 | #self.material = material | 8 | #self.material = material | ||
9 | self.material_attr_name = material | 9 | self.material_attr_name = material | ||
10 | 10 | ||||
11 | def is_a_witch(self): | 11 | def is_a_witch(self): | ||
12 | 12 | ||||
n | 13 | if hasattr(self, mass_attr_name) \ | n | 13 | 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!" | ||
t | 16 | if hasattr(self, material_attr_name) \ | t | 16 | 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!" | ||
22 | 22 | ||||
23 | return LogicMixin | 23 | return LogicMixin | ||
24 | 24 | ||||
25 | log_mixin = logic_mixin_factory(20, 'mass', 'wood', 'material', 'float') | 25 | log_mixin = logic_mixin_factory(20, 'mass', 'wood', 'material', 'float') | ||
26 | 26 | ||||
27 | class Woman(log_mixin): | 27 | class Woman(log_mixin): | ||
28 | pass | 28 | pass | ||
29 | 29 | ||||
30 | woman = Woman() | 30 | woman = Woman() | ||
31 | print(woman.is_a_witch()) | 31 | print(woman.is_a_witch()) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|