%%writefile magic command in regular Python

Viewed 4075

I am copying Python code from Jupyter Notebook to .py file to run it from a terminal. I could not find a way how this line should be implemented:

%%writefile -a submission.py

What is the same code in regular Python as %%writefile magic command does?

1 Answers

Replaced this:

%%writefile -a submission.py

    model = Net()
    model = model.float()
    model.load_state_dict(state_dict)
    model = model.to('cpu')
    model = model.eval()
    obs = tensor(obs['board']).reshape(1, 1, config.rows, config.columns).float()
    obs = obs / 2
    action = model(obs)
    return int(action)

by this:

submission_ending = '''    model = Net()
    model = model.float()
    model.load_state_dict(state_dict)
    model = model.to('cpu')
    model = model.eval()
    obs = tensor(obs['board']).reshape(1, 1, config.rows, config.columns).float()
    obs = obs / 2
    action = model(obs)
    return int(action)'''

with open('submission.py', mode='a') as file:
    file.write(submission_ending)
Related