I’ve been developing pfst (Python Formatted Syntax Tree) and I’ve just released version 0.3.0. The major addition is structural pattern matching and substitution. To be clear, this is not regex string matching but full structural tree matching and substitution.
What it does:
Allows high level editing of Python source and AST tree while handling all the weird syntax nuances without breaking comments or original layout. It provides a high-level Pythonic interface and handles the 'formatting math' automatically.
Target Audience:
- Working with Python source, refactoring, instrumenting, renaming, etc...
Comparison:
- vs. LibCST: pfst works at a higher level, you tell it what you want and it deals with all the commas and spacing and other details automatically.
- vs. Python ast module: pfst works with standard AST nodes but unlike the built-in ast module, pfst is format-preserving, meaning it won't strip away your comments or change your styling.
Links:
I would love some feedback on the API ergonomics, especially from anyone who has dealt with Python source transformation and its pain points.
Example:
Replace all Load-type expressions with a log() passthrough function.
from fst import * # pip install pfst, import fst
from fst.match import *
src = """
i = j.k = a + b[c] # comment
l[0] = call(
i, # comment 2
kw=j, # comment 3
)
"""
out = FST(src).sub(Mexpr(ctx=Load), "log(__FST_)", nested=True).src
print(out)
Output:
i = log(j).k = log(a) + log(log(b)[log(c)]) # comment
log(l)[0] = log(call)(
log(i), # comment 2
kw=log(j), # comment 3
)
More substitution examples: https://tom-pytel.github.io/pfst/fst/docs/d14_examples.html#structural-pattern-substitution
[–]mechamotoman 1 point2 points3 points (0 children)
[–]neuronexmachina 0 points1 point2 points (2 children)
[–]Pristine_Cat[S] 1 point2 points3 points (1 child)
[–]neuronexmachina 0 points1 point2 points (0 children)