Skip to main content

Macros for Python

Project description

macros.py

Macros in python

macros.py is an implemention of macros similar to ones found in C using ! to define macros

Guido van Rossum would hate me

Examples

Max function-macro

# coding: macros

!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

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 source

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

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

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

macros.py-0.1.0.tar.gz (5.2 kB view details)

Uploaded Source

Built Distribution

macros.py-0.1.0-py3.10.egg (6.9 kB view details)

Uploaded Source

File details

Details for the file macros.py-0.1.0.tar.gz.

File metadata

  • Download URL: macros.py-0.1.0.tar.gz
  • Upload date:
  • Size: 5.2 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for macros.py-0.1.0.tar.gz
Algorithm Hash digest
SHA256 137688430c7243aa94b74f629c14ee7507614d8e28a457bcca09c3f98d182e67
MD5 79edb31e0cbf39e2b74b691a6a7dca20
BLAKE2b-256 7694f35f1bd5eb618c5401dba5c73c4a5f262a47b3acdc2248ccd3a18db9d5ba

See more details on using hashes here.

File details

Details for the file macros.py-0.1.0-py3.10.egg.

File metadata

  • Download URL: macros.py-0.1.0-py3.10.egg
  • Upload date:
  • Size: 6.9 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for macros.py-0.1.0-py3.10.egg
Algorithm Hash digest
SHA256 a490c6b5e5009965a05109f6f84cf9d670cdec5fadba68b1e0413764d225cce7
MD5 23bbc4e7ac8a06d158d5f359ad798ca3
BLAKE2b-256 c09c964bf821c781d36ac44cf2d50b8fdca3b2f5fab7f2a3e1a4ccd7d4d99254

See more details on using hashes here.

Supported by

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