Rust-style macros for Python
Project description
Explore new syntax!
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
Release history Release notifications | RSS feed
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d68a3e9a741459eea96f3d1fff2922e42f2c9b64900fb1f724244e5bdaddfac5
|
|
| MD5 |
ec2c49de033ff3f2443148e7867b39ac
|
|
| BLAKE2b-256 |
25dda57df631cc00785d507e72b16a09ce6757e9bc962d1caebe86bf9081b225
|
Provenance
The following attestation bundles were made for macro_polo-0.3.0.tar.gz:
Publisher:
release.yml on BenjyWiener/macro-polo
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
macro_polo-0.3.0.tar.gz -
Subject digest:
d68a3e9a741459eea96f3d1fff2922e42f2c9b64900fb1f724244e5bdaddfac5 - Sigstore transparency entry: 191637868
- Sigstore integration time:
-
Permalink:
BenjyWiener/macro-polo@683b515d63574528f10da19066121f8afd6f9342 -
Branch / Tag:
refs/tags/0.3.0 - Owner: https://github.com/BenjyWiener
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@683b515d63574528f10da19066121f8afd6f9342 -
Trigger Event:
push
-
Statement type:
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4bb1aa6d86132b1ac8fe03d99428b38b03bc6433bbfbac3c1222e28c4db45733
|
|
| MD5 |
834b8e4549e6896c9b9e5727e57bad3f
|
|
| BLAKE2b-256 |
1f914b14b97bc9df7a96c4ecd329986d33813d2f9980c756dac1997538ea6abb
|
Provenance
The following attestation bundles were made for macro_polo-0.3.0-py3-none-any.whl:
Publisher:
release.yml on BenjyWiener/macro-polo
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
macro_polo-0.3.0-py3-none-any.whl -
Subject digest:
4bb1aa6d86132b1ac8fe03d99428b38b03bc6433bbfbac3c1222e28c4db45733 - Sigstore transparency entry: 191637875
- Sigstore integration time:
-
Permalink:
BenjyWiener/macro-polo@683b515d63574528f10da19066121f8afd6f9342 -
Branch / Tag:
refs/tags/0.3.0 - Owner: https://github.com/BenjyWiener
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@683b515d63574528f10da19066121f8afd6f9342 -
Trigger Event:
push
-
Statement type: