Skip to main content

Explore new syntax!

licence version pyversions docs

macro-polo brings Rust-inspired compile-time macros to Python. It’s currently in very early alpha, but even if it ever gets a stable release, you probably shouldn’t use it in any serious project. Even if you find a legitimate use case, the complete lack of tooling support almost definitely outweighs the benefits. That said, if you do decide to use it, I’d love to know why!

Installation

pip install macro-polo

Usage

macro-polo is modular, and can be extended at multiple levels. See the API Reference for more details.

The simplest way to use it is to add a coding: macro-polo comment to the top of your source file (in one of the first two lines). You can then declare and invoke macros using the macro_rules! syntax.

Example (examples/macro_rules/bijection.py):

# coding: macro-polo
"""A basic demonstration of `macro_rules!`."""


macro_rules! bijection:
    [$($key:tt: $val:tt),* $(,)?]:
        (
            {$($key: $val),*},
            {$($val: $key),*}
        )


macro_rules! debug_print:
    [$($expr:tt)*]:
        print(
            stringify!($($expr)*), '=>', repr($($expr)*),
            file=__import__('sys').stderr,
        )


names_to_colors, colors_to_names = bijection! {
    'red': (1, 0, 0),
    'green': (0, 1, 0),
    'blue': (0, 0, 1),
}


debug_print!(names_to_colors)
debug_print!(colors_to_names)

debug_print!(names_to_colors['green'])
debug_print!(colors_to_names[(0, 0, 1)])
$ python3 examples/bijection.py
names_to_colors  => {'red': (1, 0, 0), 'green': (0, 1, 0), 'blue': (0, 0, 1)}
colors_to_names  => {(1, 0, 0): 'red', (0, 1, 0): 'green', (0, 0, 1): 'blue'}
names_to_colors ['green'] => (0, 1, 0)
colors_to_names [(0 ,0 ,1 )] => 'blue'

Viewing the generated code:

$ python3 -m macro_polo examples/bijection.py | ruff format -
names_to_colors, colors_to_names = (
    {'red': (1, 0, 0), 'green': (0, 1, 0), 'blue': (0, 0, 1)},
    {(1, 0, 0): 'red', (0, 1, 0): 'green', (0, 0, 1): 'blue'},
)
print(
    'names_to_colors',
    '=>',
    repr(names_to_colors),
    file=__import__('sys').stderr,
)
print(
    'colors_to_names',
    '=>',
    repr(colors_to_names),
    file=__import__('sys').stderr,
)
print(
    "names_to_colors ['green']",
    '=>',
    repr(names_to_colors['green']),
    file=__import__('sys').stderr,
)
print(
    'colors_to_names [(0 ,0 ,1 )]',
    '=>',
    repr(colors_to_names[(0, 0, 1)]),
    file=__import__('sys').stderr,
)

Interested?

Check out the full documentation and tutorial.

Metadata

Release files for macro-polo 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for macro-polo 0.3.0
File Size Uploaded
macro_polo-0.3.0.tar.gz 325.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for macro-polo 0.3.0
File Interpreter ABI Platform
macro_polo-0.3.0-py3-none-any.whl Python 3 none any Details

Total release size: 353.4 kB

Release files / macro_polo-0.3.0.tar.gz

Download URL macro_polo-0.3.0.tar.gz
Size 325.2 kB
Tags Source
SHA-256 checksum
How to use checksums
d68a3e9a741459eea96f3d1fff2922e42f2c9b64900fb1f724244e5bdaddfac5
BLAKE2b-256 checksum
How to use checksums
25dda57df631cc00785d507e72b16a09ce6757e9bc962d1caebe86bf9081b225
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.9

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Apr 2, 2025.

Transparency log

Release files / macro_polo-0.3.0-py3-none-any.whl

Download URL macro_polo-0.3.0-py3-none-any.whl
Size 28.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4bb1aa6d86132b1ac8fe03d99428b38b03bc6433bbfbac3c1222e28c4db45733
BLAKE2b-256 checksum
How to use checksums
1f914b14b97bc9df7a96c4ecd329986d33813d2f9980c756dac1997538ea6abb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.9

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Apr 2, 2025.

Transparency log

Release history Release notifications | RSS feed

This release

0.3.0 This release

2 release files

0.2.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page