.. algorithm:: .. summary:: .. relatedalgorithms:: .. properties:: Description ----------- Retrieves the algorithm history of the workspace and saves it to a Python script file or Python variable. A time range can be specified which will restrict the algorithms in the script to those which were executed between the given times. If no end time was specified then algorithms from the start time up to the current time will be included in the generated script. Start and end times are given in ISO8601 format: YYYY-MM-DD HH:mm:ss, for example 3:25 PM on July the 4th 2014 would be 2014-07-04 15:25:00. The generated script includes a few lines of header comments at the top. The ``ExcludeHeader`` parameter can be used to optionally exclude these. Usage ----- **Example - generate a python script for a workspace:** .. testcode:: ExGeneratePythonScriptSimple #create a workspace and run some operations on it ws = CreateSampleWorkspace() ws = CropWorkspace(ws, XMin=7828.162291, XMax=11980.906921) ws = Power(ws, Exponent=1.5) ws = RenameWorkspace(ws, OutputWorkspace="MyTestWorkspace") script_text = GeneratePythonScript(ws) print(script_text.strip()) Output: .. testoutput:: ExGeneratePythonScriptSimple :options: +ELLIPSIS # Python script generated by Mantid # Version ... # SHA-1 ... from mantid.simpleapi import * CreateSampleWorkspace(OutputWorkspace='ws') CropWorkspace(InputWorkspace='ws', OutputWorkspace='ws', XMin=7828.1622909999996, XMax=11980.906921) Power(InputWorkspace='ws', OutputWorkspace='ws', Exponent=1.5) RenameWorkspace(InputWorkspace='ws', OutputWorkspace='MyTestWorkspace') .. testcleanup:: ExGeneratePythonScriptSimple import os def removeFiles(files): for ws in files: try: path = os.path.join(os.path.expanduser("~"), ws) os.remove(path) except: pass removeFiles(['myscript.py']) **Example - generate a python script giving a range of start times:** .. testcode:: ExGeneratePythonScriptWithTimeRanges import time # Do some operations on the workspace with a pause between them ws = CreateSampleWorkspace() ws = CropWorkspace(ws, XMin=7828.162291, XMax=11980.906921) time.sleep(2) ws = Power(ws, Exponent=1.5) ws = RenameWorkspace(ws, OutputWorkspace="MyTestWorkspace") # Get the execution time of the last algorithm and subtract 1 second history = mtd['MyTestWorkspace'].getHistory() last = history.getAlgorithmHistory(history.size() - 1) from_time = last.executionDate() - int(1e9) # Generate a script with a given start time script_text = GeneratePythonScript(ws, StartTimestamp=str(from_time)) print(script_text.strip()) Output: .. testoutput:: ExGeneratePythonScriptWithTimeRanges :options: +ELLIPSIS # Python script generated by Mantid # Version ... # SHA-1 ... from mantid.simpleapi import * Power(InputWorkspace='ws', OutputWorkspace='ws', Exponent=1.5) RenameWorkspace(InputWorkspace='ws', OutputWorkspace='MyTestWorkspace') .. testcleanup:: ExGeneratePythonScriptWithTimeRanges import os def removeFiles(files): for ws in files: try: path = os.path.join(os.path.expanduser("~"), ws) os.remove(path) except: pass removeFiles(['myscript.py']) **Example - generate a python script and save it to file:** .. testcode:: ExGeneratePythonScriptFile import os #create a workspace and run some operations on it ws = CreateSampleWorkspace() ws = CropWorkspace(ws, XMin=7828.162291, XMax=11980.906921) ws = Power(ws, Exponent=1.5) ws = RenameWorkspace(ws, OutputWorkspace="MyTestWorkspace") path = os.path.join(os.path.expanduser("~"), 'myscript.py') GeneratePythonScript(ws, Filename=path) with open (path, 'r') as script: print(script.read().strip()) Output: .. testoutput:: ExGeneratePythonScriptFile :options: +ELLIPSIS # Python script generated by Mantid # Version ... # SHA-1 ... from mantid.simpleapi import * CreateSampleWorkspace(OutputWorkspace='ws') CropWorkspace(InputWorkspace='ws', OutputWorkspace='ws', XMin=7828.1622909999996, XMax=11980.906921) Power(InputWorkspace='ws', OutputWorkspace='ws', Exponent=1.5) RenameWorkspace(InputWorkspace='ws', OutputWorkspace='MyTestWorkspace') .. testcleanup:: ExGeneratePythonScriptFile import os def removeFiles(files): for ws in files: try: path = os.path.join(os.path.expanduser("~"), ws) os.remove(path) except: pass removeFiles(['myscript.py']) .. categories:: .. sourcelink::