0

Is there any package in python that can read a python script and give the ability to modify it? Something like the following:

my_script: PythonScript = read_script("my_script.py")
list_of_functions: [PythonFunction] = my_script.functions
for func in list_of_functions:
   print(func.name)
   print(func.body)
list_of_functions[0].name = "new_function_name"
my_script.functions = list_of_functions

So again what I am looking for is a package that can read a python script and give the ability to modify it, not necessary the same way I did in my example, I just have a lot of scripts and I am looking for a way to do a fix on all of them without using find and replace in an IDE nor reading them as text files for example. Somehow traversing a python script from python code? I do not know what keywords should I use to do a proper search either.

M.Alsioufi
  • 565
  • 2
  • 7
  • 16
  • Looks like there are similar question here https://stackoverflow.com/questions/41858147/how-to-modify-imported-source-code-on-the-fly, https://stackoverflow.com/questions/139180/how-to-list-all-functions-in-a-python-module , I think you can play around with inspect to solve your problem – nithin Nov 05 '19 at 08:46
  • I am not looking for modifying imported packages. But I will give it a look maybe I can get some ideas – M.Alsioufi Nov 05 '19 at 09:02
  • you just need list of the functions ? – nithin Nov 05 '19 at 09:38
  • No, I need to also have the code of the function, and to be able to modify it as well. Maybe there is nothing like this since this a very rare problem? – M.Alsioufi Nov 05 '19 at 10:14

0 Answers0