0

I have a project which executes large number of python files in a folder. All of them have a line PATH="defaultstring". I want to make it more dynamic i.e. replace "defaultstring" in all the python files to some "otherstring" provided on run time. If no string is provided on runtime then "defaultstring" will be default value.

I am building the project using gradle. One of the thing I can do is execute some python script say "main.py" before those group of files are executed. "main.py" can iterate to all the files, open them, and replace PATH="defaultstring" to "otherstring".

Is it possible to do this without changing all those files in folder every time I run ? This is because if I change "defaultstring" to "otherstring" in first run and in second run suppose I don't give any runtime input then by default PATH="otherstring" will be executed but default value I want to keep is "defaultstring". I can change "otherstring" to "defaultstring" in this case but I want to do with some method which does not iterate through all files and change those lines.

Can we do this using fixture injection (which I don't have much idea about so any other technique will also be helpful).

  • Though I have accepted an answer, It will be helpful for everyone in general if others have some ideas to share. You can have my upvote for that. –  Jun 09 '21 at 15:44

1 Answers1

0

Assuming that your files have some naming pattern that is unique and can be recognised you can do the following:

  1. Find all your files using glob or something similar
  2. Import the files dynamically
  3. Change the variable using the global keyword

If you create a function that does these steps and execute it as the first action of your code in the main function, you should be able to change the variable PATH globally at runtime without interacting with the file stored on your hard drive (and consequently not changing the default value).

Edit: While the above approach will hopefully work, it is nevertheless bad practice. It would be better if you have a single file that contains such variables that all other files import. Then you only have to change a single variable and the intention is much clearer than in the version that you originally intended to do.

Edit2: You can structure your project to contain the following files with respective functions or variables. This setup allows you to change the variable PATH at runtime upon starting the script if all file import a common file containing path. The other option of changing a variable in each file is more cumbersome and I would only add the option if really necessary.

globals.py

PATH = 'original/path'

def modify_path(new_path):
    global PATH
    PATH = new_path

some_file1.py

import globals
def get_path():
    return globals.PATH

some_file2.py

import globals
def get_path():
    return globals.PATH

main.py

import globals

if necessary:
    globals.modify_path('new/path')
C Hecht
  • 932
  • 5
  • 14
  • C Hecht thanks for the answer, could you give sample code to illustrate this, I am not able to figure out how it is different from importing a file in all other python file and making change in that single file. –  Jun 09 '21 at 09:09
  • For this to work do I need to execute my other python files from main.py itself ? I think that is the case but actually I cannot do it since I am running shell script to build my project and there are lot other files which can call run any other file at any time –  Jun 09 '21 at 10:15
  • I think you need to be clear about what you are trying to achieve. From what I understand, you are trying to not modify the actual file, but only a variable during runtime. For that you need to know where your program enters. Will edit a bit of sample code that illustrates the second option as it is cleaner anyways. For full code samples, please include sample code of your own. – C Hecht Jun 09 '21 at 10:25
  • so suppose I have file test.py and one.py . In one.py I declare x=5 and print it. Now according to your answer I should import one.py in test.py and I can change value x=10 . Then I can execute one.py and it will work with x=10. can you illustrate how to do this since I am not able to do it –  Jun 09 '21 at 10:31
  • Please see my edit to my answer and check if that is sufficient for your understanding – C Hecht Jun 09 '21 at 10:32
  • In this case (as shown in example) we have to write import statement in every file but then we can do thing you suggested in Edit1. –  Jun 09 '21 at 15:43