一个使用像jquery选择器的命令行工具来搜索python代码

shudaoge 8年前

来自: https://github.com/caioariede/pyq

pyq

A command-line tool to search for Python code using jQuery-like selectors

Installation

pip install pyqtool

Usage

pyq2 [OPTIONS] SELECTOR [PATH]  # Python 2.x  pyq3 [OPTIONS] SELECTOR [PATH]  # Python 3.x

Available selectors

Type

class , def and import

Name

#classname or #methodname

Attributes

[name=value] , [name!=value] , [name*=value] , [name^=value] and [name$=value]

Pseudo-selectors

:extends(classname) and :not(selector)

Combinators

parent > child , parent descendant

Examples

Search for classes that extends the IntegerField class:

❯ pyq3 'class:extends(IntegerField)' django/forms  django/forms/fields.py:278 class FloatField(IntegerField):  django/forms/fields.py:315 class DecimalField(IntegerField):

Search for classes with the name FloatField :

❯ pyq3 'class[name=FloatField]' django/forms  django/forms/fields.py:278 class FloatField(IntegerField):

Search for methods under the FloatField class:

❯ pyq3 'class[name=FloatField] > def' django/forms  django/forms/fields.py:283     def to_python(self, value):  django/forms/fields.py:299     def validate(self, value):  django/forms/fields.py:308     def widget_attrs(self, widget):

Search for methods whose name starts with to under the FloatField class:

❯ pyq3 'class[name=FloatField] > def[name^=to]' django/forms  django/forms/fields.py:283     def to_python(self, value):

Search for import statements importing Counter :

❯ pyq3 'import[from=collections][name=Counter]' django/  django/apps/registry.py:5 from collections import Counter, OrderedDict, defaultdict  django/template/utils.py:3 from collections import Counter, OrderedDict  django/test/testcases.py:14 from collections import Counter