Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

%%writefile magic command in regular Python

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?

like image 885
Joe Rakhimov Avatar asked Sep 06 '25 00:09

Joe Rakhimov


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)
like image 134
Joe Rakhimov Avatar answered Sep 07 '25 21:09

Joe Rakhimov