I have been thinking about this for too long and had no idea, perhaps some of you may be able to help.
I have a python script folder, all of which have the same environment (literally, I generated it from a shell script), but have one piece that is different from everyone. In other words:
Top piece of code (always the same) Middle piece of code (changes from file to file) Bottom piece of code (always the same)
And today I realized that this is a bad idea, for example, if I want to change something from the top or bottom, I need to write a shell script to do this. (Not that it is complicated, it just seems very bad code wise).
So what I want to do is there is one external python script that looks like this:
Top piece of code Dynamic function that calls the middle piece of code (based on a parameter) Bottom piece of code
And then every other python file in the folder may just be the middle part of the code. However, the normal module will not work here (if I'm not mistaken), because I will get the code that I need to execute from the argument, which will be a string, and therefore, I don’t know which function will work until runtime.
So, I came up with two more solutions:
- I could write a bunch of if statements, one to run each script based on a specific parameter. I rejected this as it is even worse than the previous project.
I could use:
os.command (sys.argv [0] scriptName.py)
which will run the script, but calling python to call python doesn't seem very elegant to me.
Does anyone have any other ideas? Thanks.
source share