Skip to main content

macros.py

License: MIT PyPI version PyPI downloads Repo Size

Macros in python

macros.py is an implemention of macros similar to ones found in C using a custom codec to implement a feature into python

Guido van Rossum would hate me

Examples

Max function-macro

# coding: macros

macro def MAX(a, b):
    (a if a > b else b)

# print the max of 1 and 5
def max_1_and_5():
    print(MAX(1, 5))

max_1_and_5()

# output:
# 5

This code gets translated to python-readable code:

# coding: macros

# print the max of 1 and 5
def max_1_and_5():
    print((1 if 1 > 5 else 5))

max_1_and_5()

# output:
# 5

More examples can be found in the examples directory

How it works

You might be wondering, how the hell did you make macros in python. It's possible due to a quirk in python that allows custom encodings to be created and preprocess the code before it is run. When a macro is defined it doesn't actually define it as a python variable, it saves the bytecode and name of a macro in a list of macros that is checked against when token of type tokenize.NAME is seen. When a macro is used, it yields the tokens of the macro in place (and if the macro has args, replaces those).

Installing

From pip

py -m pip install -U macros.py

From source

git clone https://github.com/spookybear0/macros.py
cd macros.py
python3 -m pip install -U .

A side note

This library is a joke, please do not ever use this in a real project.

This code is also of terrible quality, but I might decide to improve it soon

Metadata

Release files for macros.py 0.3.1

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

Source distribution (sdist)

Source distribution for macros.py 0.3.1
File Size Uploaded
macros.py-0.3.1.tar.gz 6.0 kB Details

Release files / macros.py-0.3.1.tar.gz

Download URL macros.py-0.3.1.tar.gz
Size 6.0 kB
Tags Source
SHA-256 checksum
How to use checksums
5e3d6e4f441fd41c24864a3978ea4baa753e2a688a0198c5576386f6aa4155ac
BLAKE2b-256 checksum
How to use checksums
6b45b4d06a2de1f6402cd834dd0686fc7c3b5b503d8598cc129c3ccfc254db05
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.7.0 importlib_metadata/4.8.2 pkginfo/1.8.2 requests/2.24.0 requests-toolbelt/0.9.1 tqdm/4.49.0 CPython/3.8.5

Release history Release notifications | RSS feed

This release

0.3.1 This release

1 release file

0.3.0

1 release file

0.2.0

1 release file

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