Skip to main content

Rust-style macros for Python

Project description

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.

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

macro_polo-0.3.0.tar.gz (325.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

macro_polo-0.3.0-py3-none-any.whl (28.2 kB view details)

Uploaded Python 3

File details

Details for the file macro_polo-0.3.0.tar.gz.

File metadata

  • Download URL: macro_polo-0.3.0.tar.gz
  • Upload date:
  • Size: 325.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for macro_polo-0.3.0.tar.gz
Algorithm Hash digest
SHA256 d68a3e9a741459eea96f3d1fff2922e42f2c9b64900fb1f724244e5bdaddfac5
MD5 ec2c49de033ff3f2443148e7867b39ac
BLAKE2b-256 25dda57df631cc00785d507e72b16a09ce6757e9bc962d1caebe86bf9081b225

See more details on using hashes here.

Provenance

The following attestation bundles were made for macro_polo-0.3.0.tar.gz:

Publisher: release.yml on BenjyWiener/macro-polo

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file macro_polo-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: macro_polo-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 28.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for macro_polo-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4bb1aa6d86132b1ac8fe03d99428b38b03bc6433bbfbac3c1222e28c4db45733
MD5 834b8e4549e6896c9b9e5727e57bad3f
BLAKE2b-256 1f914b14b97bc9df7a96c4ecd329986d33813d2f9980c756dac1997538ea6abb

See more details on using hashes here.

Provenance

The following attestation bundles were made for macro_polo-0.3.0-py3-none-any.whl:

Publisher: release.yml on BenjyWiener/macro-polo

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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