1# Jokes aside
2def logic_mixin_factory(mass, mass_attr_name, material, material_attr_name, float_method_name):
3 """Returns a class that has a method is_a_witch that returns True if the class has the passed in attributes"""
4 class Witch:
5 """A Mixin that sets all the attributes and methods passed in to the factory."""
6 def __init__(self):
7 setattr(self, mass_attr_name, mass)
8 setattr(self, material_attr_name, material)
9 setattr(self, float_method_name, lambda _: print('this is indeed a method'))
10
11 def is_a_witch(self):
12 """Returns True if the class has the passed in attributes and methods."""
13 if isinstance(self, Witch):
14 return "Burn her!"
15
16 try:
17 if (getattr(self, mass_attr_name) != mass or
18 getattr(self, material_attr_name) != material or
19 not callable(getattr(self, float_method_name))):
20 return "No, but it's a pity, cuz she looks like a witch!"
21 except AttributeError:
22 return "No, but it's a pity, cuz she looks like a witch!"
23
24 return "Burn her!"
25
26 return Witch
F
======================================================================
FAIL: test_realcase (test.TestWitchMixinFactory)
Real test for the Witch factory.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 30, in test_realcase
self.assertEqual(Nothing().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 22:24Ок, ок, сега разбрах :smile:, но имай предвид, че ако нещо наследява вещица, то винаги ще е вещица.
Не искам в стремежа си да направиш яка шега, което вече е факт, да се спънеш.
|
Филип Филчев
08.11.2023 21:49Ах шегата е, че каквото е None ще принти Java. Ама това може и да ми чупи решенито 😂
А за стрнговете да, забравих ги. От що се мъчех да натамъня шегата
|
Георги Кунчев
08.11.2023 21:41Объркан съм от това решение. Дори не виждам стринговете, които сме дефинирали, че трябва да връщаш.
И не, това не е хейт, защото си кръстил класа си `Java`. Този факт го оценявам :smile:
|
t | t | 1 | # Jokes aside | ||
1 | def logic_mixin_factory(mass, mass_attr_name, material, material_attr_name, float_method_name): | 2 | def logic_mixin_factory(mass, mass_attr_name, material, material_attr_name, float_method_name): | ||
2 | """Returns a class that has a method is_a_witch that returns True if the class has the passed in attributes""" | 3 | """Returns a class that has a method is_a_witch that returns True if the class has the passed in attributes""" | ||
3 | class Witch: | 4 | class Witch: | ||
4 | """A Mixin that sets all the attributes and methods passed in to the factory.""" | 5 | """A Mixin that sets all the attributes and methods passed in to the factory.""" | ||
5 | def __init__(self): | 6 | def __init__(self): | ||
6 | setattr(self, mass_attr_name, mass) | 7 | setattr(self, mass_attr_name, mass) | ||
7 | setattr(self, material_attr_name, material) | 8 | setattr(self, material_attr_name, material) | ||
8 | setattr(self, float_method_name, lambda _: print('this is indeed a method')) | 9 | setattr(self, float_method_name, lambda _: print('this is indeed a method')) | ||
9 | 10 | ||||
10 | def is_a_witch(self): | 11 | def is_a_witch(self): | ||
11 | """Returns True if the class has the passed in attributes and methods.""" | 12 | """Returns True if the class has the passed in attributes and methods.""" | ||
12 | if isinstance(self, Witch): | 13 | if isinstance(self, Witch): | ||
13 | return "Burn her!" | 14 | return "Burn her!" | ||
14 | 15 | ||||
15 | try: | 16 | try: | ||
16 | if (getattr(self, mass_attr_name) != mass or | 17 | if (getattr(self, mass_attr_name) != mass or | ||
17 | getattr(self, material_attr_name) != material or | 18 | getattr(self, material_attr_name) != material or | ||
18 | not callable(getattr(self, float_method_name))): | 19 | not callable(getattr(self, float_method_name))): | ||
19 | return "No, but it's a pity, cuz she looks like a witch!" | 20 | return "No, but it's a pity, cuz she looks like a witch!" | ||
20 | except AttributeError: | 21 | except AttributeError: | ||
21 | return "No, but it's a pity, cuz she looks like a witch!" | 22 | return "No, but it's a pity, cuz she looks like a witch!" | ||
22 | 23 | ||||
23 | return "Burn her!" | 24 | return "Burn her!" | ||
24 | 25 | ||||
25 | return Witch | 26 | return Witch |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
f | 1 | def logic_mixin_factory(mass, mass_attr_name, material, material_attr_name, float_method_name): | f | 1 | def logic_mixin_factory(mass, mass_attr_name, material, material_attr_name, float_method_name): |
2 | """Returns a class that has a method is_a_witch that returns True if the class has the passed in attributes""" | 2 | """Returns a class that has a method is_a_witch that returns True if the class has the passed in attributes""" | ||
3 | class Witch: | 3 | class Witch: | ||
4 | """A Mixin that sets all the attributes and methods passed in to the factory.""" | 4 | """A Mixin that sets all the attributes and methods passed in to the factory.""" | ||
5 | def __init__(self): | 5 | def __init__(self): | ||
6 | setattr(self, mass_attr_name, mass) | 6 | setattr(self, mass_attr_name, mass) | ||
7 | setattr(self, material_attr_name, material) | 7 | setattr(self, material_attr_name, material) | ||
8 | setattr(self, float_method_name, lambda _: print('this is indeed a method')) | 8 | setattr(self, float_method_name, lambda _: print('this is indeed a method')) | ||
9 | 9 | ||||
10 | def is_a_witch(self): | 10 | def is_a_witch(self): | ||
11 | """Returns True if the class has the passed in attributes and methods.""" | 11 | """Returns True if the class has the passed in attributes and methods.""" | ||
t | t | 12 | if isinstance(self, Witch): | ||
13 | return "Burn her!" | ||||
14 | |||||
12 | try: | 15 | try: | ||
13 | if (getattr(self, mass_attr_name) != mass or | 16 | if (getattr(self, mass_attr_name) != mass or | ||
14 | getattr(self, material_attr_name) != material or | 17 | getattr(self, material_attr_name) != material or | ||
15 | not callable(getattr(self, float_method_name))): | 18 | not callable(getattr(self, float_method_name))): | ||
16 | return "No, but it's a pity, cuz she looks like a witch!" | 19 | return "No, but it's a pity, cuz she looks like a witch!" | ||
17 | except AttributeError: | 20 | except AttributeError: | ||
18 | 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!" | ||
19 | 22 | ||||
20 | return "Burn her!" | 23 | return "Burn her!" | ||
21 | 24 | ||||
22 | return Witch | 25 | return Witch |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
f | 1 | def logic_mixin_factory(mass, mass_attr_name, material, material_attr_name, float_method_name): | f | 1 | def logic_mixin_factory(mass, mass_attr_name, material, material_attr_name, float_method_name): |
2 | """Returns a class that has a method is_a_witch that returns True if the class has the passed in attributes""" | 2 | """Returns a class that has a method is_a_witch that returns True if the class has the passed in attributes""" | ||
n | 3 | class Java: | n | 3 | class Witch: |
4 | """A Mixin that sets all the attributes and methods passed in to the factory. | 4 | """A Mixin that sets all the attributes and methods passed in to the factory.""" | ||
5 | Otherwise, set them to Java. :) | ||||
6 | """ | ||||
7 | def __init__(self): | 5 | def __init__(self): | ||
n | 8 | setattr(self, mass_attr_name or 'camelCaseJavaAttribute', mass or 'java') | n | 6 | setattr(self, mass_attr_name, mass) |
9 | setattr(self, material_attr_name or 'camelCaseJavaAttribute', material or 'java') | 7 | setattr(self, material_attr_name, material) | ||
10 | setattr(self, float_method_name or 'camelCaseJavaMethod', self.camelCaseJavaMethod) | 8 | setattr(self, float_method_name, lambda _: print('this is indeed a method')) | ||
11 | |||||
12 | def camelCaseJavaMethod(self): | ||||
13 | """Prints Java if the method name is not passed in, otherwise prints Python.""" | ||||
14 | print('Java' if not float_method_name else 'Python') | ||||
15 | 9 | ||||
16 | def is_a_witch(self): | 10 | def is_a_witch(self): | ||
17 | """Returns True if the class has the passed in attributes and methods.""" | 11 | """Returns True if the class has the passed in attributes and methods.""" | ||
n | 18 | if (not hasattr(self, float_method_name) and | n | 12 | try: |
19 | not hasattr(self, mass_attr_name) and | 13 | if (getattr(self, mass_attr_name) != mass or | ||
20 | not hasattr(self, material_attr_name)): | 14 | getattr(self, material_attr_name) != material or | ||
15 | not callable(getattr(self, float_method_name))): | ||||
21 | return "No, but it's a pity, cuz she looks like a witch!" | 16 | return "No, but it's a pity, cuz she looks like a witch!" | ||
22 | 17 | except AttributeError: | |||
23 | if (getattr(self, mass_attr_name) != mass or | ||||
24 | getattr(self, material_attr_name) != material or | ||||
25 | not callable(getattr(self, float_method_name))): | ||||
26 | return "No, but it's a pity, cuz she looks like a witch!" | 18 | return "No, but it's a pity, cuz she looks like a witch!" | ||
27 | 19 | ||||
28 | return "Burn her!" | 20 | return "Burn her!" | ||
29 | 21 | ||||
t | 30 | return Java | t | 22 | return Witch |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
f | 1 | def logic_mixin_factory(mass, mass_attr_name, material, material_attr_name, float_method_name): | f | 1 | def logic_mixin_factory(mass, mass_attr_name, material, material_attr_name, float_method_name): |
2 | """Returns a class that has a method is_a_witch that returns True if the class has the passed in attributes""" | 2 | """Returns a class that has a method is_a_witch that returns True if the class has the passed in attributes""" | ||
3 | class Java: | 3 | class Java: | ||
4 | """A Mixin that sets all the attributes and methods passed in to the factory. | 4 | """A Mixin that sets all the attributes and methods passed in to the factory. | ||
5 | Otherwise, set them to Java. :) | 5 | Otherwise, set them to Java. :) | ||
6 | """ | 6 | """ | ||
7 | def __init__(self): | 7 | def __init__(self): | ||
8 | setattr(self, mass_attr_name or 'camelCaseJavaAttribute', mass or 'java') | 8 | setattr(self, mass_attr_name or 'camelCaseJavaAttribute', mass or 'java') | ||
9 | setattr(self, material_attr_name or 'camelCaseJavaAttribute', material or 'java') | 9 | setattr(self, material_attr_name or 'camelCaseJavaAttribute', material or 'java') | ||
10 | setattr(self, float_method_name or 'camelCaseJavaMethod', self.camelCaseJavaMethod) | 10 | setattr(self, float_method_name or 'camelCaseJavaMethod', self.camelCaseJavaMethod) | ||
11 | 11 | ||||
12 | def camelCaseJavaMethod(self): | 12 | def camelCaseJavaMethod(self): | ||
13 | """Prints Java if the method name is not passed in, otherwise prints Python.""" | 13 | """Prints Java if the method name is not passed in, otherwise prints Python.""" | ||
14 | print('Java' if not float_method_name else 'Python') | 14 | print('Java' if not float_method_name else 'Python') | ||
15 | 15 | ||||
16 | def is_a_witch(self): | 16 | def is_a_witch(self): | ||
17 | """Returns True if the class has the passed in attributes and methods.""" | 17 | """Returns True if the class has the passed in attributes and methods.""" | ||
18 | if (not hasattr(self, float_method_name) and | 18 | if (not hasattr(self, float_method_name) and | ||
19 | not hasattr(self, mass_attr_name) and | 19 | not hasattr(self, mass_attr_name) and | ||
20 | not hasattr(self, material_attr_name)): | 20 | not hasattr(self, material_attr_name)): | ||
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 | if (getattr(self, mass_attr_name) != mass or | 23 | if (getattr(self, mass_attr_name) != mass or | ||
24 | getattr(self, material_attr_name) != material or | 24 | getattr(self, material_attr_name) != material or | ||
25 | not callable(getattr(self, float_method_name))): | 25 | not callable(getattr(self, float_method_name))): | ||
26 | return "No, but it's a pity, cuz she looks like a witch!" | 26 | return "No, but it's a pity, cuz she looks like a witch!" | ||
27 | 27 | ||||
28 | return "Burn her!" | 28 | return "Burn her!" | ||
29 | 29 | ||||
30 | return Java | 30 | return Java | ||
t | 31 | t | |||
32 | |||||
33 | Mixin = logic_mixin_factory(1, 'mass', 'wood', 'material', 'float_method_name') | ||||
34 | |||||
35 | class Deriv(Mixin): | ||||
36 | pass | ||||
37 | |||||
38 | d = Deriv() | ||||
39 | |||||
40 | print(d.is_a_witch()) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
f | 1 | def logic_mixin_factory(mass, mass_attr_name, material, material_attr_name, float_method_name): | f | 1 | def logic_mixin_factory(mass, mass_attr_name, material, material_attr_name, float_method_name): |
2 | """Returns a class that has a method is_a_witch that returns True if the class has the passed in attributes""" | 2 | """Returns a class that has a method is_a_witch that returns True if the class has the passed in attributes""" | ||
3 | class Java: | 3 | class Java: | ||
4 | """A Mixin that sets all the attributes and methods passed in to the factory. | 4 | """A Mixin that sets all the attributes and methods passed in to the factory. | ||
5 | Otherwise, set them to Java. :) | 5 | Otherwise, set them to Java. :) | ||
6 | """ | 6 | """ | ||
7 | def __init__(self): | 7 | def __init__(self): | ||
8 | setattr(self, mass_attr_name or 'camelCaseJavaAttribute', mass or 'java') | 8 | setattr(self, mass_attr_name or 'camelCaseJavaAttribute', mass or 'java') | ||
9 | setattr(self, material_attr_name or 'camelCaseJavaAttribute', material or 'java') | 9 | setattr(self, material_attr_name or 'camelCaseJavaAttribute', material or 'java') | ||
10 | setattr(self, float_method_name or 'camelCaseJavaMethod', self.camelCaseJavaMethod) | 10 | setattr(self, float_method_name or 'camelCaseJavaMethod', self.camelCaseJavaMethod) | ||
11 | 11 | ||||
12 | def camelCaseJavaMethod(self): | 12 | def camelCaseJavaMethod(self): | ||
13 | """Prints Java if the method name is not passed in, otherwise prints Python.""" | 13 | """Prints Java if the method name is not passed in, otherwise prints Python.""" | ||
14 | print('Java' if not float_method_name else 'Python') | 14 | print('Java' if not float_method_name else 'Python') | ||
15 | 15 | ||||
16 | def is_a_witch(self): | 16 | def is_a_witch(self): | ||
17 | """Returns True if the class has the passed in attributes and methods.""" | 17 | """Returns True if the class has the passed in attributes and methods.""" | ||
18 | if (not hasattr(self, float_method_name) and | 18 | if (not hasattr(self, float_method_name) and | ||
19 | not hasattr(self, mass_attr_name) and | 19 | not hasattr(self, mass_attr_name) and | ||
20 | not hasattr(self, material_attr_name)): | 20 | not hasattr(self, material_attr_name)): | ||
n | 21 | return False | n | 21 | return "No, but it's a pity, cuz she looks like a witch!" |
22 | 22 | ||||
23 | if (getattr(self, mass_attr_name) != mass or | 23 | if (getattr(self, mass_attr_name) != mass or | ||
24 | getattr(self, material_attr_name) != material or | 24 | getattr(self, material_attr_name) != material or | ||
25 | not callable(getattr(self, float_method_name))): | 25 | not callable(getattr(self, float_method_name))): | ||
n | 26 | return False | n | 26 | return "No, but it's a pity, cuz she looks like a witch!" |
27 | 27 | ||||
n | 28 | return True | n | 28 | return "Burn her!" |
29 | 29 | ||||
30 | return Java | 30 | return Java | ||
t | t | 31 | |||
32 | |||||
33 | Mixin = logic_mixin_factory(1, 'mass', 'wood', 'material', 'float_method_name') | ||||
34 | |||||
35 | class Deriv(Mixin): | ||||
36 | pass | ||||
37 | |||||
38 | d = Deriv() | ||||
39 | |||||
40 | print(d.is_a_witch()) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
f | 1 | def logic_mixin_factory(mass, mass_attr_name, material, material_attr_name, float_method_name): | f | 1 | def logic_mixin_factory(mass, mass_attr_name, material, material_attr_name, float_method_name): |
2 | """Returns a class that has a method is_a_witch that returns True if the class has the passed in attributes""" | 2 | """Returns a class that has a method is_a_witch that returns True if the class has the passed in attributes""" | ||
3 | class Java: | 3 | class Java: | ||
4 | """A Mixin that sets all the attributes and methods passed in to the factory. | 4 | """A Mixin that sets all the attributes and methods passed in to the factory. | ||
5 | Otherwise, set them to Java. :) | 5 | Otherwise, set them to Java. :) | ||
6 | """ | 6 | """ | ||
7 | def __init__(self): | 7 | def __init__(self): | ||
8 | setattr(self, mass_attr_name or 'camelCaseJavaAttribute', mass or 'java') | 8 | setattr(self, mass_attr_name or 'camelCaseJavaAttribute', mass or 'java') | ||
9 | setattr(self, material_attr_name or 'camelCaseJavaAttribute', material or 'java') | 9 | setattr(self, material_attr_name or 'camelCaseJavaAttribute', material or 'java') | ||
10 | setattr(self, float_method_name or 'camelCaseJavaMethod', self.camelCaseJavaMethod) | 10 | setattr(self, float_method_name or 'camelCaseJavaMethod', self.camelCaseJavaMethod) | ||
11 | 11 | ||||
12 | def camelCaseJavaMethod(self): | 12 | def camelCaseJavaMethod(self): | ||
13 | """Prints Java if the method name is not passed in, otherwise prints Python.""" | 13 | """Prints Java if the method name is not passed in, otherwise prints Python.""" | ||
14 | print('Java' if not float_method_name else 'Python') | 14 | print('Java' if not float_method_name else 'Python') | ||
15 | 15 | ||||
16 | def is_a_witch(self): | 16 | def is_a_witch(self): | ||
17 | """Returns True if the class has the passed in attributes and methods.""" | 17 | """Returns True if the class has the passed in attributes and methods.""" | ||
18 | if (not hasattr(self, float_method_name) and | 18 | if (not hasattr(self, float_method_name) and | ||
19 | not hasattr(self, mass_attr_name) and | 19 | not hasattr(self, mass_attr_name) and | ||
20 | not hasattr(self, material_attr_name)): | 20 | not hasattr(self, material_attr_name)): | ||
21 | return False | 21 | return False | ||
22 | 22 | ||||
23 | if (getattr(self, mass_attr_name) != mass or | 23 | if (getattr(self, mass_attr_name) != mass or | ||
24 | getattr(self, material_attr_name) != material or | 24 | getattr(self, material_attr_name) != material or | ||
25 | not callable(getattr(self, float_method_name))): | 25 | not callable(getattr(self, float_method_name))): | ||
26 | return False | 26 | return False | ||
27 | 27 | ||||
28 | return True | 28 | return True | ||
29 | 29 | ||||
30 | return Java | 30 | return Java | ||
t | 31 | t | |||
32 | |||||
33 | mixin = logic_mixin_factory('mass', 'mass_attr_name', 'material', 'material_attr_name', 'float_method_name') | ||||
34 | |||||
35 | class MixinClass(mixin): | ||||
36 | pass | ||||
37 | |||||
38 | class NotMixinClass: | ||||
39 | pass | ||||
40 | |||||
41 | M = NotMixinClass() | ||||
42 | m = MixinClass() | ||||
43 | M.__setattr__('float_method_name', m.float_method_name) | ||||
44 | M.__setattr__('is_a_witch', m.is_a_witch) | ||||
45 | |||||
46 | print(m.material_attr_name) | ||||
47 | m.float_method_name() | ||||
48 | |||||
49 | print(M.is_a_witch()) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
f | 1 | def logic_mixin_factory(mass, mass_attr_name, material, material_attr_name, float_method_name): | f | 1 | def logic_mixin_factory(mass, mass_attr_name, material, material_attr_name, float_method_name): |
2 | """Returns a class that has a method is_a_witch that returns True if the class has the passed in attributes""" | 2 | """Returns a class that has a method is_a_witch that returns True if the class has the passed in attributes""" | ||
3 | class Java: | 3 | class Java: | ||
n | n | 4 | """A Mixin that sets all the attributes and methods passed in to the factory. | ||
5 | Otherwise, set them to Java. :) | ||||
6 | """ | ||||
4 | def __init__(self): | 7 | def __init__(self): | ||
n | 5 | setattr(self, mass_attr_name, mass) | n | 8 | setattr(self, mass_attr_name or 'camelCaseJavaAttribute', mass or 'java') |
6 | setattr(self, material_attr_name, material) | 9 | setattr(self, material_attr_name or 'camelCaseJavaAttribute', material or 'java') | ||
7 | setattr(self, float_method_name, self.java) | 10 | setattr(self, float_method_name or 'camelCaseJavaMethod', self.camelCaseJavaMethod) | ||
8 | 11 | ||||
n | 9 | def java(self): | n | 12 | def camelCaseJavaMethod(self): |
10 | print('java') | 13 | """Prints Java if the method name is not passed in, otherwise prints Python.""" | ||
14 | print('Java' if not float_method_name else 'Python') | ||||
11 | 15 | ||||
12 | def is_a_witch(self): | 16 | def is_a_witch(self): | ||
n | n | 17 | """Returns True if the class has the passed in attributes and methods.""" | ||
13 | if (not hasattr(self, float_method_name) and | 18 | if (not hasattr(self, float_method_name) and | ||
14 | not hasattr(self, mass_attr_name) and | 19 | not hasattr(self, mass_attr_name) and | ||
15 | not hasattr(self, material_attr_name)): | 20 | not hasattr(self, material_attr_name)): | ||
16 | return False | 21 | return False | ||
17 | 22 | ||||
18 | if (getattr(self, mass_attr_name) != mass or | 23 | if (getattr(self, mass_attr_name) != mass or | ||
19 | getattr(self, material_attr_name) != material or | 24 | getattr(self, material_attr_name) != material or | ||
20 | not callable(getattr(self, float_method_name))): | 25 | not callable(getattr(self, float_method_name))): | ||
21 | return False | 26 | return False | ||
22 | 27 | ||||
23 | return True | 28 | return True | ||
24 | 29 | ||||
25 | return Java | 30 | return Java | ||
t | t | 31 | |||
32 | |||||
33 | mixin = logic_mixin_factory('mass', 'mass_attr_name', 'material', 'material_attr_name', 'float_method_name') | ||||
34 | |||||
35 | class MixinClass(mixin): | ||||
36 | pass | ||||
37 | |||||
38 | class NotMixinClass: | ||||
39 | pass | ||||
40 | |||||
41 | M = NotMixinClass() | ||||
42 | m = MixinClass() | ||||
43 | M.__setattr__('float_method_name', m.float_method_name) | ||||
44 | M.__setattr__('is_a_witch', m.is_a_witch) | ||||
45 | |||||
46 | print(m.material_attr_name) | ||||
47 | m.float_method_name() | ||||
48 | |||||
49 | print(M.is_a_witch()) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|