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.5.tar.gz (3.5 kB view details)

Uploaded Source

Built Distribution

import_transforms-0.5-py3-none-any.whl (4.1 kB view details)

Uploaded Python 3

File details

Details for the file import_transforms-0.5.tar.gz.

File metadata

  • Download URL: import_transforms-0.5.tar.gz
  • Upload date:
  • Size: 3.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for import_transforms-0.5.tar.gz
Algorithm Hash digest
SHA256 4c9762f844245bd8f73a13c272b122923e17f58709fca845bfbf71862e5ee68b
MD5 8d4ef0d33805c9d79451c32ec462b9fd
BLAKE2b-256 080620b5b117d90c3007a39c5ecb2259fcc5af6c0c498a6a241f2e1f68256421

See more details on using hashes here.

File details

Details for the file import_transforms-0.5-py3-none-any.whl.

File metadata

File hashes

Hashes for import_transforms-0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 743137a4fb88c12b5103c94a7f9e4154f887420dda2f3e00f6997e4c80f551ce
MD5 e13a39591d0dec31ca7c8db887cfa7c9
BLAKE2b-256 50f41b07bf422132dbc2496b72d1d215ac536013cc5401a65897f678a82f3b41

See more details on using hashes here.

Supported by

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