Skip to main content

Execute python scripts cell by cell

Project description

csc - Execute python scripts cell by cell

Install with

pip install csc

Usage

Consider the following training script

#: parameters
...

#: setup
...

#: train
...

#: save
...

To run the the script cell by cell, use:

script = csc.Script("experiment.py")
script.run("parameters")
script.run("setup")
script.run("train")
script.run("save")

Splicing scripts

Different scripts can be "spliced" together by specifying multiple scripts. The first script acts as the base script and defines the available cells. Subsequent scripts, or spliced scripts, can extend the cells of the base script. All scripts share a single scope. For each cell, first the code of the base script is executed and then the code of the spliced scripts.

# file: parameters.py
#: parameters
batch_size = ...
scripts = csc.Script(["experiment.py", "parameters.py"])

# executes first the 'parameters' cell of 'experiment.py', then the
# 'parameters' cell of 'parameters.py'
scripts.run("parameters")

Command line usage

csc include a command line script splice and execute scripts. Usage:

$ python -m csc base_script.py spliced_script_1.py spliced_script_2.py

To splice in parameters, csc offers a shortcut: arguments specified via -p STATEMENT are concatenated into a script with a single 'parameters' cell that executes the given statements.

# this call
$ python -m csc base_script.py -p batch_size=100 -p learning_rate=3e-4

# is equivalent to
$ python -m csc base_script.py parameters.py
$ cat parameters.py
#: parameters
batch_size=100
learning_rate=3e-4

API Reference

csc.Script(base_script: Union[str, pathlib.Path, FileSource, InlineSource], /, *spliced_scripts, cell_marker: str = '#:', register: bool = False)

A Python script that can be executed cell by cell

Cells are defined via comments (per default '#: {CELL_NAME}').

csc.Script.list(self) -> list[str]

List all cells of the script

Only cells of the base script are considered.

csc.Script.run(self, *cell_names) -> None

Run cells of the script by name

csc.Script.eval(self, expr: str) -> Any

Evaluate an expression the scope of the script

csc.FileSource(path: pathlib.Path, *, cell_marker: str)

Define a script via a file

csc.InlineSource(text: str, *, cell_marker: str)

Define a script via its source

License

This package is licensed under the MIT License. See LICENSE for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

csc-24.2.0.tar.gz (6.0 kB view details)

Uploaded Source

Built Distribution

csc-24.2.0-py3-none-any.whl (7.0 kB view details)

Uploaded Python 3

File details

Details for the file csc-24.2.0.tar.gz.

File metadata

  • Download URL: csc-24.2.0.tar.gz
  • Upload date:
  • Size: 6.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.8

File hashes

Hashes for csc-24.2.0.tar.gz
Algorithm Hash digest
SHA256 f2bacd05a6863ecdc8a2d424801afa8e1acf92eb0664e1a8d7a171e212af980e
MD5 817fd5cf3c3b1397c3f450b703d339e2
BLAKE2b-256 755856df12002f778dd866ab0b2aaedb8ba80d92bfca7c86df072f3221178730

See more details on using hashes here.

File details

Details for the file csc-24.2.0-py3-none-any.whl.

File metadata

  • Download URL: csc-24.2.0-py3-none-any.whl
  • Upload date:
  • Size: 7.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.8

File hashes

Hashes for csc-24.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c32d3bff9162d5a86edc068d0eff31cfcab8b073f5cd11a1f710774ff5a78aca
MD5 f76da2ac146184424b1a1d9469328925
BLAKE2b-256 d3ae33397bef2a5472b8dbcc425ce9116e31946418709b2b96fd4b0bf7503110

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page