Macros for Python: Quasiquotes, Case Classes, LINQ and more!
Project description
MacroPy is an implementation of Macros in the Python Programming Language. MacroPy provides a mechanism for user-defined functions (macros) to perform transformations on the abstract syntax tree(AST) of Python code at module import time. This is an easy way to modify the semantics of a python program
Python like you’ve never seen before
MacroPy allows you to create constructs which are impossible to have in normal python code, such as:
Tracing
with trace:
sum([x + 5 for x in range(3)])
# sum([x + 5 for x in range(3)])
# range(3) -> [0, 1, 2]
# x + 5 -> 5
# x + 5 -> 6
# x + 5 -> 7
# [x + 5 for x in range(3)] -> [5, 6, 7]
# sum([x + 5 for x in range(3)]) -> 18
Quick Lambdas
print map(f[_[0]], ['omg', 'wtf', 'bbq'])
# ['o', 'w', 'b']
print reduce(f[_ + _], ['omg', 'wtf', 'bbq'])
# 'omgwtfbbq
Case Classes
@case
class Point(x, y): pass
p = Point(1, 2)
print str(p) #Point(1, 2)
print p.x #1
print p.y #2
print Point(1, 2) == Point(1, 2) # True
And more! All this runs perfectly on vanilla Python 3.5+ (an maybe on pypy, untested). For more details, see the GitHub page. or ask in the Gitter channel or the Google group .
Changelog
1.0.4.dev2 (2017-09-08)
Add MANIFEST.in;
1.0.4.dev1 (2017-09-08)
Updated the import machinery and macro detection to be compatible with Python 3.5+.
Removed support for Python 2, supporting it would require a way to manage differences at the ast level, but i don’t use Python2 anymore.
Added support for Python 3.5+ in the form of new call arguments form and new AsyncFunctionDef.
Basic scope analysis now available in the form of the @Scoped decorator, to be used in conjunction with @Walker.
1.0.3
Error messages are now raised at run-time rather than at import time, with other improvements (double stack traces, catchability).
@enum macro now has much better error messages
Improved error messages for mis-use of stub functions outside their related macro (e.g. the u, name, ast stubs for the q/hq macros)
Improved error messages for invalid case class signatures
Hygienic Quasiquotes now allow lexical capture of module objects
1.0.2
Removed unit test from PyPI distribution
1.0.1
Fixed a bug in ast_ctx_fixer
gen_sym() is now gen_sym(name="sym"), allowing you to override the base name
Implemented macropy.case_classes.enum macro
Implemented macropy.quick_lambda.lazy and macropy.quick_lambda.interned macros
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.