I have two scripts main1 and main2 that need to be separated. They both call middle script (e.g. connection to database and query). The query itself comes from the sub1.py for main1 and sub2.py for main2.
Currently, I call middle with parameter indicating the origin. Could this be simplified?
#main1.py
import middle
middle.myfunction(arg_i, 'main1')
#main2.py
import middle
middle.myfunction(arg_i, 'main2')
#middle.py
import sub1, sub2
def myfunction(arg1, arg2):
if arg2 == 'main1':
...
sub1.anotherfunction
elif arg2 == 'main2':
...
sub2.yetanotherfunction
A potential solution is to duplicate myfunction in middle.py, i.e. create myfunction_main1 and myfunction_main2. But that does not seem elegant.
Bonus: I have additional functions in middle.py that are relevant only for main1, not main2. Since I am calling all functions from main1 by loop, I need to establish arg2 in them as well, else I get error on missing argument.
funs = [middle.fun1, middle.fun2, middle.myfunction]
for i in range(3):
funs[i](arg_i, 'main1')