Run python scripts cell by cell.
Project description
csc
- Tools for non standard Python execution
Install with
pip install csc
Patterns
Load a script as a module
train_script = csc.load("train.py")
train_script.train_func()
Extracting local variable from functions
def add(x, y):
z = x + y
return z
res = csc.call(add, 1, 2)
assert res.__return__ == 3
assert res.x == 1
assert res.y == 2
assert res.z == 3
Execute scripts with code cells
Consider a script to define and train a model
#%% [setup] Parameters
...
#%% [setup] Setup
...
#%% Train
...
#%% Save
...
To run the parameters cell, then overwrite the parameters, and finally run set, use:
script = csc.Script("external_script.py")
script["Parameters"].run()
script.ns.hidden_units = 64
script["Setup":].run()
Splicing scripts
Same effect as in the previous example:
script = csc.Script("external_script.py")
with csc.splice(script, "Parameters"):
script.ns.hidden_units = 64
Using cell tags
To only define the model without training or saving the results, a subset of the script can be selected via tags:
# execute any cell tagged with "setup"
script[lambda: tags & {"setup"}].run()
# can also be combined with splicing to modify the parameters
with csc.splice(script[lambda: tags & {"setup"}], "Parameters"):
script.ns.hidden_units = 64
Creating temporary modules
In a Jupyter notebook, first register the magic function
import csc
csc.autoconfig()
Afterwards a module can be defined via
%%csc.module my_cool_module
def add(x, y):
return x + y
It can be used as any other module. For example:
import my_cool_module
assert my_cool_module.add(1, 2) == 3
License
This package is licensed under the MIT License. See LICENSE
for details.
The function csc._utils.capture_frame
is adapted from this stackoverflow
post by Niklas Rosenstein
licensed under CC-BY-SA 4.0.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.