Reputation: 21
I'm in the process of building a program that combines 3 other programs based on the user's input. The program is to build a sales model depending on which role they are. There a 3 roles they could be so I wrote 3 separate programs and then I'm writing a program to execute one of the program that is for their role. My ultimate goal is to make this program a web app.
I'm wondering if this is the best approach or should I have gone about it differently. If there is a better way where do I go to learn about it?
So the combined program accesses 3 different programs based on the user's input. If they are a BDR it runs the WDP_BDR.py program. If they are an AE it runs the WDP_AE.py program. If they are a Full Cycle AE it runs the WDP_FullCycle.py program.
Here's the code I wrote for the combined program
model_selection = input("Press 1 if you are a BDR\nPress 2 if you are an AE\nPress 3 if you are full cycle: ")
def bdr():
import WDP_BDR
def ae():
import WDP_AE
def fullcycle():
import WDP_FullCycle
if model_selection == '1':
bdr()
elif model_selection == '2':
ae()
elif model_selection == '3':
fullcycle()
I tested it and it works perfectly, however, I want to make sure I'm doing it the best way. This is the first project that I'm working on and learning as I go. I figured it would be best to dive into a project and learn what I need to learn instead of going through hours of theoretical learning prior to attempting my first project.
Thank you in advance for providing your input!
Upvotes: 0
Views: 558
Reputation: 1196
I think it is not a typical thing to use an import
statement in order to run code. It may also not work in case that you run your program multiple times, because the import will be done only one times.
The more usual approach would be to put the code that you want to run inside of a method and then call that method, like this:
import WDP_BDR
import WDP_AE
import WDP_FullCycle
model_selection = input("Press 1 if you are a BDR\nPress 2 if you are an AE\nPress 3 if you are full cycle: ")
def bdr():
WDP_BDR.do_stuff()
def ae():
WDP_AE.do_stuff()
def fullcycle():
WDP_FullCycle.do_stuff()
if model_selection == '1':
bdr()
elif model_selection == '2':
ae()
elif model_selection == '3':
fullcycle()
EDIT
The above suggestion implies that the modules WDP_BDR, WDP_AE and WDP_FullCycle are modified such that all the code that is currently in them is instead put into a function do_stuff()
(of course, you could pick a different name for that function). This way, the code in these modules is not called during the import
statement, but only as soon as these functions are called.
For example, if the module WDP_BDR.py would currently read
import numpy as np
result = np.sqrt(5)
print("result is", result)
then you would need to change that to
import numpy as np
def do_stuff():
result = np.sqrt(5)
print("result is", result)
I think this is also what Marcucus_ tried to suggest.
Upvotes: 0
Reputation: 3396
It's quite similar but you do not need the unnecessary function definitions:
model_selection = input("Press 1 if you are a BDR\nPress 2 if you are an AE\nPress 3 if you are full cycle: ")
if model_selection == '1':
import WDP_BDR
elif model_selection == '2':
import WDP_AE
elif model_selection == '3':
import WDP_FullCycle
Upvotes: 0
Reputation: 88
I think you have to do something like that to make it more readable:
import WDP_BDR
import WDP_AE
import WDP_FullCycle
model_selection = input("Press 1 if you are a BDR\nPress 2 if you are an AE\nPress 3 if you are full cycle: ")
if model_selection == '1':
WDP_BDR.{name of your function in WDP_BDR.py}
elif model_selection == '2':
WDP_AE.{name of your function in WDP_AE.py}
elif model_selection == '3':
WDP_FullCycle.{name of your function in WDP_FullCycle.py}
else:
return
Upvotes: 1