All Projects → hchasestevens → Astpath

hchasestevens / Astpath

A command-line search utility for Python ASTs using XPath syntax.

Programming Languages

python
139335 projects - #7 most used programming language
metaprogramming
66 projects

Projects that are alternatives of or similar to Astpath

Ff
Find files (ff) by name, fast!
Stars: ✭ 257 (+53.89%)
Mutual labels:  command-line-tool, search
Phpgrep
Syntax-aware grep for PHP code.
Stars: ✭ 185 (+10.78%)
Mutual labels:  ast, search
Amber
A code search / replace tool
Stars: ✭ 230 (+37.72%)
Mutual labels:  command-line-tool, search
Ripgrep
ripgrep recursively searches directories for a regex pattern while respecting your gitignore
Stars: ✭ 28,564 (+17004.19%)
Mutual labels:  command-line-tool, search
Defiant.js
http://defiantjs.com
Stars: ✭ 907 (+443.11%)
Mutual labels:  search, xpath
Simpleaudioindexer
Searching for the occurrence seconds of words/phrases or arbitrary regex patterns within audio files
Stars: ✭ 100 (-40.12%)
Mutual labels:  command-line-tool, search
Vscode Tsquery
TSQuery extension for Visual Studio Code
Stars: ✭ 13 (-92.22%)
Mutual labels:  ast, search
Query Translator
Query Translator is a search query translator with AST representation
Stars: ✭ 165 (-1.2%)
Mutual labels:  ast, search
Marathon
[DEPRECATED] Marathon makes it easy to write, run and manage your Swift scripts 🏃
Stars: ✭ 1,889 (+1031.14%)
Mutual labels:  command-line-tool
Receivemidi
Multi-platform command-line tool to monitor and receive MIDI messages
Stars: ✭ 164 (-1.8%)
Mutual labels:  command-line-tool
React Ast
render abstract syntax trees with react
Stars: ✭ 160 (-4.19%)
Mutual labels:  ast
Json To Ast
JSON AST parser
Stars: ✭ 161 (-3.59%)
Mutual labels:  ast
Flask Msearch
Full text search for flask.
Stars: ✭ 164 (-1.8%)
Mutual labels:  search
Keyvi
Keyvi - the key value index. It is an in-memory FST-based data structure highly optimized for size and lookup performance.
Stars: ✭ 161 (-3.59%)
Mutual labels:  search
Algoliasearch Client Swift
⚡️ A fully-featured and blazing-fast Swift API client to interact with Algolia.
Stars: ✭ 166 (-0.6%)
Mutual labels:  search
Microjs.com
Fantastic Micro-Frameworks and Micro-Libraries for Fun and Profit!
Stars: ✭ 1,927 (+1053.89%)
Mutual labels:  search
Graphtage
A semantic diff utility and library for tree-like files such as JSON, JSON5, XML, HTML, YAML, and CSV.
Stars: ✭ 2,062 (+1134.73%)
Mutual labels:  command-line-tool
Angular Search Experience
Algolia + Angular = 🔥🔥🔥
Stars: ✭ 167 (+0%)
Mutual labels:  search
Eclipse Instasearch
Eclipse plug-in for fast code search
Stars: ✭ 165 (-1.2%)
Mutual labels:  search
Algorithm
The repository algorithms implemented on the Go
Stars: ✭ 163 (-2.4%)
Mutual labels:  search

astpath

PyPI version

[e ɛs ti pæθ] n.

Ⅰ. A command-line utility for querying Python ASTs using XPath syntax.

ⅠⅠ. A better way of searching through your codebase.

Example usage

Finding all usages of the eval builtin:

$ astpath ".//Call/func/Name[@id='eval']" | head -5
./rlcompleter.py:136    >            thisobject = eval(expr, self.namespace)
./warnings.py:176       >            cat = eval(category)
./rexec.py:328  >        return eval(code, m.__dict__)
./pdb.py:387    >                    func = eval(arg,
./pdb.py:760    >            return eval(arg, self.curframe.f_globals,

Finding all numbers:

$ astpath ".//Num" | head -5
./DocXMLRPCServer.py:31 >        here = 0
./DocXMLRPCServer.py:41 >        while 1:
./DocXMLRPCServer.py:57 >            elif text[end:end+1] == '(':
./DocXMLRPCServer.py:82 >                    args[1:],
./DocXMLRPCServer.py:96 >            argspec = object[0] or argspec

... that are never assigned to a variable:

$ astpath ".//Num[not(ancestor::Assign)]" | head -5
./DocXMLRPCServer.py:41 >        while 1:
./DocXMLRPCServer.py:57 >            elif text[end:end+1] == '(':
./DocXMLRPCServer.py:201        >                assert 0, "Could not find method in self.functions and no "\
./DocXMLRPCServer.py:237        >        self.send_response(200)
./DocXMLRPCServer.py:252        >                 logRequests=1, allow_none=False, encoding=None,

... and are greater than 1000:

$ astpath ".//Num[not(ancestor::Assign) and number(@n) > 1000]" | head -5
./decimal.py:959      >                    return 314159
./fractions.py:206    >    def limit_denominator(self, max_denominator=1000000):
./pty.py:138  >    return os.read(fd, 1024)
./whichdb.py:94       >    if magic in (0x13579ace, 0x13579acd, 0x13579acf):
./whichdb.py:94       >    if magic in (0x13579ace, 0x13579acd, 0x13579acf):

Finding names longer than 42 characters:

$ astpath "//Name[string-length(@id) > 42]"
./site-packages/setuptools/dist.py:59   >_patch_distribution_metadata_write_pkg_info()
./site-packages/setuptools/command/easy_install.py:1759 >        updater=clear_and_remove_cached_zip_archive_directory_data)
./test/test_reprlib.py:268      >        module = areallylongpackageandmodulenametotestreprtruncation
./test/test_argparse.py:2744    >    MEPBase, TestMutuallyExclusiveOptionalsAndPositionalsMixed):

Finding except clauses that raise a different exception class than they catch:

$ astpath "//ExceptHandler[body//Raise/exc//Name and not(contains(body//Raise/exc//Name/@id, type/Name/@id))]" | head -5
./hashlib.py:144        >except ImportError:
./plistlib.py:89        >        except KeyError:
./plistlib.py:103       >        except KeyError:
./nntplib.py:868        >        except ValueError:
./argparse.py:1116      >        except KeyError:

Finding beginnings of unreachable code blocks:

$ astpath "//body/*[preceding-sibling::Return or preceding-sibling::Raise][1]"
./unittest/test/testmock/testhelpers.py:381     >        class Foo(object):
./test/test_deque.py:16 >    yield 1
./test/test_posix.py:728        >            def _create_and_do_getcwd(dirname, current_path_length = 0):

Finding candidates for replacement with sum:

$ astpath -A 1 "//For/body[AugAssign/op/Add and count(child::*)=1]" | head -6
./functools.py:374      >        for item in sorted_items:
./functools.py:375                   key += item
./statistics.py:177     >    for d, n in sorted(partials.items()):
./statistics.py:178              total += Fraction(n, d)
./pstats.py:512 >    for calls in callers.values():
./pstats.py:513          nc += calls

Finding classes matching a regular expression:

$ astpath "//ClassDef[re:match('.*Var', @name)]" | head -5
./typing.py:452  >      class TypeVar(_TypingBase, _root=True):
./typing.py:1366 >      class _ClassVar(_FinalTypingBase, _root=True):
./tkinter/__init__.py:287  >    class Variable:
./tkinter/__init__.py:463  >    class StringVar(Variable):
./tkinter/__init__.py:485  >    class IntVar(Variable):

astpath can also be imported and used programmatically:

>>> from astpath import search
>>> len(search('.', '//Print', print_matches=False))  # number of print statements in the codebase
751

Installation

It is recommended that astpath be installed with the optional lxml dependency, to allow full use of the XPath query language. To do so,

pip install astpath[xpath]

Alternatively, a no-dependency version using Python's builtin XPath subset can be installed via

pip install astpath

astpath supports both Python 2.7 and 3.x.

Links

Contacts

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].