A few weeks ago I released pyastgrep, a tool for grepping Python code at the syntax level (using AST - Abstract Syntax Trees), and today I released some more improvements.
It builds on an earlier tool, astpath which now appears to be abandoned,
and also had quite a few bugs. I’ve fixed lots of things and re-written quite a
bit internally in backwards incompatible ways, so a fork was the easiest way
forward. I’ve also been able to make lots of improvements to default behaviour,
inspired by tools like ripgrep. For example, it automatically exclude paths that
match discovered .gitignore
files, which can make a massive performance
improvement in some cases (looking at you, node_modules
).
This tool can be really useful for certain kind of linting or one off
maintenance tasks e.g. “find me all loop variables called i
or j
”:
pyastgrep './/For/target//Name[@id="i" or @id="j"]'
With a bit more work, and more understanding of XPath expressions, you can do some fairly advanced things. See the docs for more examples.
Hope you find it useful!