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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file cg_atv2_python_insert-1!1.0.1.tar.gz
.
File metadata
- Download URL: cg_atv2_python_insert-1!1.0.1.tar.gz
- Upload date:
- Size: 5.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.2 CPython/3.11.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 12800daebcbe9d93bd59d485c4d125d78c739c2fc5108d793f011017bcf2929c |
|
MD5 | 1793b6a2320527666b172473dcf5c1cf |
|
BLAKE2b-256 | 9f044acab2d9e26e771bd93a069eadcbe2da9f99b4dc1a727cb45bc4b7cd3294 |
File details
Details for the file cg_atv2_python_insert-1!1.0.1-py3-none-any.whl
.
File metadata
- Download URL: cg_atv2_python_insert-1!1.0.1-py3-none-any.whl
- Upload date:
- Size: 6.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.2 CPython/3.11.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1adb880d436e06dc39b1978af2c96a327aab4210b7b040b2f531dfd65af6aa51 |
|
MD5 | 42b3130df5761f55bc0a9669caa9a943 |
|
BLAKE2b-256 | 65772edc428effa60a2867032c9b60bde033f20b743d0aee9c2e0c08fa282a07 |