Skip to content

Tsche/magic_codec

Repository files navigation

magic codec - preprocessing for the python interpreter

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

Loading builtins

Currently the following preprocessors are available:

  • braces Python with braces - inspired by Bython
    • magic line: # coding: magic.braces
    • example: python tests/braces/test.by
  • incdec Extends python with unary prefix ++i and postfix i++ increment/decrement expressions
    • magic line: # coding: magic.incdec
    • example: python tests/incdec/incdec.py
  • cpp lets the Python interpreter interpret C++ via cppyy
    • magic line: #define CODEC "coding:magic.cpp"
    • example: python tests/cpp/test.cpp
  • toml validate toml files using json schemas
    • magic line: # coding: magic.toml
    • example: python tests/toml/data_valid.toml -s tests/toml/schema.json

Builtins can be loaded by setting the codec to magic.builtin_name where builtin_name is the name of the builtin.

Loading extensions

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.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published