Skip to content

Instantly share code, notes, and snippets.

@brunovcosta
Created September 12, 2022 18:23
Show Gist options
  • Save brunovcosta/12ec569289c016fb041c8c629a814bcc to your computer and use it in GitHub Desktop.
Save brunovcosta/12ec569289c016fb041c8c629a814bcc to your computer and use it in GitHub Desktop.
receives a python code file and executes statement by statement with checkpoint
import fire
import pdb
import pickle
import sys
import ast
import os
class Executor:
def run(self,
source_path,
state_path=None,
line_from=0,
line_to=None,
reset_state=False):
source = open(source_path, "r").read()
tree = ast.parse(source)
if os.path.exists(state_path) and not reset_state:
with open(state_path, "rb") as state_file:
state = pickle.load(state_file)
else:
state = {}
for stmt in tree.body[line_from:line_to]:
module = ast.Module(body=[stmt], type_ignores=tree.type_ignores)
bytecode = compile(module, 'main', 'exec')
exec(bytecode, state)
with open(state_path, "wb") as state_file:
pickle.dump(state, state_file)
if __name__ == '__main__':
fire.Fire(Executor)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment