0

Quite often I do an anlysis with certain settings and store the plots or results in a folder which is separated from the codebase. To keep track of chosen settings I save the script like so:

import MyClass as my

model = my(some_parameters)
model.run()

with open(os.path.join(save_path, 'used_script_for_docu.py'), 'w') as copied_script_file:
    with open(os.path.abspath(__file__), 'r') as script_file:
        script_content = script_file.read()
    copied_script_file.write(script_content)

Is it somehow possible to add a function to MyClass which can save the script file like explained above? Can the instance of MyClass (model) somehow acces the script file with the current parameters settings? Or are there any other methods to save the script automatically without having to type/copy the lines from above from script to script?

EDIT: I would prefer to store the script file and not the states/parameters of the model because the script might contain settings/procedures which were not anticipated in beforehand and can threfore not be saved automatically from the namespace of e.g. model

Moritz
  • 5,130
  • 10
  • 40
  • 81
  • (1) *Is it somehow possible to add a function to MyClass […]* - yes, of course. Did you try it? What was the problem? (2) It would probably be easier and more maintainable/scalable to use some sort of version control (e.g. Git) for the scripts and store a reference to the specific revision with the results; or use a dedicated configuration management system. – mkrieger1 Dec 12 '19 at 14:23
  • Possible duplicate of https://stackoverflow.com/questions/23321100/best-way-to-have-a-python-script-copy-itself – mkrieger1 Dec 12 '19 at 14:27
  • Does this answer your question? [Best way to have a python script copy itself?](https://stackoverflow.com/questions/23321100/best-way-to-have-a-python-script-copy-itself) – Moritz Dec 25 '19 at 22:57

0 Answers0