magic_codec is a small utility to make writng preprocessors for Python easier. This uses a custom codec to kick off preprocessing before passing the result to the Python interpreter. You can find a more in-depth explanation of how this works over at pydong.org
Currently the following preprocessors are available:
- braces Python with braces - inspired by Bython
- magic line:
# coding: magic.braces
- example:
python tests/braces/test.by
- magic line:
- incdec Extends python with unary prefix
++i
and postfixi++
increment/decrement expressions- magic line:
# coding: magic.incdec
- example:
python tests/incdec/incdec.py
- magic line:
- cpp lets the Python interpreter interpret C++ via cppyy
- magic line:
#define CODEC "coding:magic.cpp"
- example:
python tests/cpp/test.cpp
- magic line:
- toml validate toml files using json schemas
- magic line:
# coding: magic.toml
- example:
python tests/toml/data_valid.toml -s tests/toml/schema.json
- magic line:
Builtins can be loaded by setting the codec to magic.builtin_name
where builtin_name
is the name of the builtin.
To extend magic_codec with your own preprocessors, you can create another Python package whose name is prefixed with magic_
. Setting the codec to magic_foo
would load the magic_foo
package and check if it has a function preprocess
.
The expected signature of preprocess
is as follows:
def preprocess(data: str) -> str:
raise NotImplementedError
You can find an example extension in example.