Skip to main content

No project description provided

Project description

cg_atv2_python_insert

This package provides functionality to parse a Python script, identify specific 'magic comments', and substitute these comments with code from other files. It's designed to be used in environments where code templates and supplementary files are involved, such as automated grading or code templating systems.

The script supports command-line interaction, allowing users to specify the file to be processed. It then reads through the specified Python script, looks for magic comments in the format # CG_INSERT filename, and replaces these comments with the contents of the referenced file, executing it as part of the script.

The file to be processed cannot contain any call to sys.exit(), quit(), exit(), os._exit() or any method that raises the SystemExit error. This will raise a ValueError if the filled in template is then run.

Usage:

Run the script from the command line with the filename as an argument.

  • ./example.py
foo = 4

# CG_INSERT other.py

print (foo+bar)
  • ./other.py
bar = 3

Running python -m cg_atv2_python_insert example.py results in the following file being generated

  • ./filled_example.py
foo = 4

try:
    exec(compile(open('other.py').read(), 'other.py', 'exec'), globals())
except SystemExit as e:
    raise ValueError("Call not allowed") from e

print (foo+bar)

If the filled template is then run, the result printed on screen will be 7

Module contains:

  • CLI To generate a python file where each magic comment is substituted by the correct exec call.

Limitation:

This module is not intended to handle complex substitutions or manage dependencies between inserted scripts. The magic comment needs to be placed at the top level, outside of any function or class.

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

cg_atv2_python_insert-1!1.0.1.tar.gz (5.0 kB view details)

Uploaded Source

Built Distribution

cg_atv2_python_insert-1!1.0.1-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file cg_atv2_python_insert-1!1.0.1.tar.gz.

File metadata

File hashes

Hashes for cg_atv2_python_insert-1!1.0.1.tar.gz
Algorithm Hash digest
SHA256 12800daebcbe9d93bd59d485c4d125d78c739c2fc5108d793f011017bcf2929c
MD5 1793b6a2320527666b172473dcf5c1cf
BLAKE2b-256 9f044acab2d9e26e771bd93a069eadcbe2da9f99b4dc1a727cb45bc4b7cd3294

See more details on using hashes here.

File details

Details for the file cg_atv2_python_insert-1!1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for cg_atv2_python_insert-1!1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1adb880d436e06dc39b1978af2c96a327aab4210b7b040b2f531dfd65af6aa51
MD5 42b3130df5761f55bc0a9669caa9a943
BLAKE2b-256 65772edc428effa60a2867032c9b60bde033f20b743d0aee9c2e0c08fa282a07

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