Skip to main content

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

import_transforms-0.2.4.tar.gz (3.9 kB view hashes)

Uploaded Source

Built Distribution

import_transforms-0.2.4-py3-none-any.whl (4.1 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page