Is it possible to refactor multiinheritance without dummy classes? Maybe anybody have similar issue or have experienxe to tackle it, or at least tell me which way to look??
Code from __init__.py
JavaScript
x
29
29
1
from configuration.global_vars import IS_JENKINS_JOB, IS_TEST, IS_DOCKER, IS_TLF
2
from .base_runner import BaseRunner
3
from .base_runner_DOCKER import BaseRunnerDOCKER
4
from .base_runner_JENKINS import BaseRunnerJENKINS
5
from .base_runner_PROD import BaseRunnerPROD
6
from .base_runner_TEST import BaseRunnerTEST
7
from .base_runner_TLF import BaseRunnerTLF
8
9
10
class Dummy1:
11
pass
12
13
14
class Dummy2:
15
pass
16
17
18
class Dummy3:
19
pass
20
21
22
class CombinedBase(
23
BaseRunnerJENKINS if IS_JENKINS_JOB else Dummy1,
24
BaseRunnerDOCKER if IS_DOCKER else Dummy2,
25
BaseRunnerTLF if IS_TLF else Dummy3,
26
BaseRunnerTEST if IS_TEST else BaseRunnerPROD,
27
BaseRunner):
28
pass
29
Advertisement
Answer
It is relatively easy to create a type dynamically in python.
For example:
JavaScript
1
18
18
1
# you can use whatever logic to dynamically create the list of bases
2
base_classes = [
3
BaseRunnerJENKINS,
4
BaseRunnerTLF,
5
BaseRunner
6
]
7
8
# if you need to add custom methods to your new class:
9
class MyCustomClass:
10
def method(self, *args):
11
pass
12
13
# Create CombinedBase, inheriting from the MyCustomClass and the dynamic list.
14
CombinedBase = type('CombinedBase', (MyCustomClass, *base_classes), {})
15
16
print(CombinedBase.__bases__)
17
# (__main__.MyCustomClass, __main__.BaseRunnerJENKINS, __main__.BaseRunnerTLF, __main__.BaseRunner)
18