|
Python preprocessor
Run python code inside files
|
view on
github
|
The pyp application functions similar to how PHP functions, except it uses python and is designed for stand-alone file processing.
Arbitrary python code can be placed in <? python_code; ?> tags, and then running pyp input.txt output.txt will process the input file into a new output file.
Current downloads are for Windows, and work on Windows 7 x86/x64.
Note: rename the file you download to pyp.exe for ease of use.
For extra build versions, check out the bin directory
There are two forms of tags that can be used: execution tags and evaluation tags.
<? ?> tags.
<?= ?> tags.
Tag continuations are used to turn text content outside of tag blocks into a string to be used in an expression. They can be used with both execution tags and evaluation tags.
Tag continuations are formed by adding an ellipsis ... to the starting/ending tags surrounding the content to be stringified. For example, the following are identical:
<? use_code( ...?>convert this to a string<?... ); ?>
<? use_code("convert this to a string"); ?>
There can also be nested tags, albeit not usually necessary:
<? use_code( ...?>convert <?="this"?> to a string<?... ); ?>
<? use_code( ...?>convert this to a string<?... ); ?>
<? use_code("convert this to a string"); ?>
Invalid, mismatched, or differing type tag continuations will all result in an error.
For code evaluation, a python variable called pyp is added into the global scope. It represents a module which has two functions: write and include.
pyp.write(content) : Noneprint or sys.stdout.write statements, as they still direct to the default stdout.TypeError is thrown if an invalid value is passed.
pyp.include(filename) : Noneimport statement instead.TypeError / TypeError : an invalid value was passedMemoryError / MemoryError : ran out of memoryIOError / PermissionError : input read or output write errorIOError / FileNotFoundError : specified filename was not foundThe pyp command line is simple and provides a few customization options. The basic style of the command line can be one of the following:
Additional info can be obtained by executing pyp --usage.
--input <path> – Specify the input filenamepath is "-", stdin is used.
--output <path> – Specify the output filenamepath is "-", stdout is used.
--read-block-size <size> – The size (in characters) of a file read block; default is 10240
--read-block-count <count> – The initial number of read blocks to cycle between; default is 2--no-continuations – Disables tag continuations
--inline-errors – Inlines errors into the output file instead of stderr
--inline-error-modifer <method> – Method of modifying error messages when they are inlined<" to "<")html" and "none".
--encoding <mode> – Method of encoding unicode values into byte valuesutf-8"
--encoding-errors <mode> – Method dealing with encoding errorsstrict"