No project description provided
Project description
import-transforms
is a Python library for intercepting and transforming source code at import time. The main use case is unit-syntax, which modifies the Python syntax to support units of measure.
Usage
Transforms are a function that takes the raw module source as input and returns the transformed source as a str
or ast.AST
. So for example, this transform would print the name of the module when it is imported:
def print_name(source, path):
return f'print(f"{__name__}")\n' + source
Transforms are registed with a glob-style module pattern:
- "foo" matches just that single module.
- "foo.*" matches all sub-modules of "foo" (but not "foo" itself).
- "*" will match all modules.
Typically you'll want to register the transform in your pacakge's __init__.py
, with a pattern that applies to just the sub-modules of your package, e.g:
register_package_source_transform(__name__, my_transform)
Example
TODO
- check/support bytecode cached files
- how do I define a type parameter that matches a function?
- bare module import fails check_module?
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
Built Distribution
Hashes for import_transforms-0.5-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 743137a4fb88c12b5103c94a7f9e4154f887420dda2f3e00f6997e4c80f551ce |
|
MD5 | e13a39591d0dec31ca7c8db887cfa7c9 |
|
BLAKE2b-256 | 50f41b07bf422132dbc2496b72d1d215ac536013cc5401a65897f678a82f3b41 |