astpath
1.0.0
[e ɛs ti pæθ] n .
Ⅰ. XPath 구문을 사용하여 Python AST를 쿼리하기 위한 명령줄 유틸리티입니다.
ⅠⅠ. 코드베이스를 검색하는 더 나은 방법입니다.
eval
내장 의 모든 사용법 찾기 :
$ 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,
모든 숫자 찾기:
$ 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
... 변수에 할당되지 않은 경우:
$ 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,
... 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):
42자를 초과하는 이름 찾기:
$ 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):
catch하는 것과 다른 예외 클래스를 발생시키는 except
절 찾기 :
$ 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:
도달할 수 없는 코드 블록의 시작 찾기:
$ 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):
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
정규식과 일치하는 클래스 찾기:
$ 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
프로그래밍 방식으로 가져오고 사용할 수도 있습니다.
>> > from astpath import search
>> > len ( search ( '.' , '//Print' , print_matches = False )) # number of print statements in the codebase
751
XPath 쿼리 언어를 완전히 사용할 수 있도록 선택적인 lxml
종속성과 함께 astpath
설치하는 것이 좋습니다. 그렇게 하려면,
pip install astpath[xpath]
또는 Python의 내장 XPath 하위 집합을 사용하는 비종속성 버전을 다음을 통해 설치할 수 있습니다.
pip install astpath
astpath
Python 2.7과 3.x를 모두 지원합니다.
ast
모듈 문서입니다.astpath
기반으로 구축된 모든 기능을 갖춘 린팅 엔진인 bellybutton
.