I have a main file called gui.py
I have multiple solution files in ./solutions each called "problem_" + num + .py (ex: problem_1.py)
I need to call a method from a solution file based on user input of the problem number. Thererfore I need to:
- Record the user integer input (1,2,3 etc...) (ex. 1)
- Make sure the correct module is imported (ex.
from solutions.problem_1 import *) - call the method in the imported file (ex.
solutions.problem_1.sol_1_text())
How could I achieve this dynamic method call from an external module?
I've tried:
# FOLLOWING CODE NEEDS TO BE DYNAMIC
from solutions.problem_1 import *
method_to_call = getattr(solutions.problem_1, 'sol_1_text')
result = getattr(solutions.problem_1, 'sol_1_text')()
for a full look at the GUI code https://github.com/spicyNoodles15/PyProjectEuler/blob/main/gui.py
SOLVED
I was able to solve this by creating the dynamic string and then using the exec() method
exec('solutions.problem_' + values['-INPUT-'] + '.solution()')