本文整理汇总了Python中sphinx.pycode.parser.Parser.parse方法的典型用法代码示例。如果您正苦于以下问题:Python Parser.parse方法的具体用法?Python Parser.parse怎么用?Python Parser.parse使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sphinx.pycode.parser.Parser
的用法示例。
在下文中一共展示了Parser.parse方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_class
# 需要导入模块: from sphinx.pycode.parser import Parser [as 别名]
# 或者: from sphinx.pycode.parser.Parser import parse [as 别名]
def test_class():
source = ('class Foo(object):\n'
' attr1 = None #: comment1\n'
' attr2 = None #: comment2\n'
'\n'
' def __init__(self):\n'
' self.a = 1 + 1 #: comment3\n'
' self.attr2 = 1 + 1 #: overrided\n'
' b = 1 + 1 #: comment5\n'
'\n'
' def some_method(self):\n'
' c = 1 + 1 #: comment6\n')
parser = Parser(source)
parser.parse()
assert parser.comments == {('Foo', 'attr1'): 'comment1',
('Foo', 'a'): 'comment3',
('Foo', 'attr2'): 'overrided'}
assert parser.definitions == {'Foo': ('class', 1, 11),
'Foo.__init__': ('def', 5, 8),
'Foo.some_method': ('def', 10, 11)}
assert parser.deforders == {'Foo': 0,
'Foo.attr1': 1,
'Foo.__init__': 3,
'Foo.a': 4,
'Foo.attr2': 5,
'Foo.some_method': 6}
示例2: test_formfeed_char
# 需要导入模块: from sphinx.pycode.parser import Parser [as 别名]
# 或者: from sphinx.pycode.parser.Parser import parse [as 别名]
def test_formfeed_char():
source = ('class Foo:\n'
'\f\n'
' attr = 1234 #: comment\n')
parser = Parser(source)
parser.parse()
assert parser.comments == {('Foo', 'attr'): 'comment'}
示例3: test_obj_assignment
# 需要导入模块: from sphinx.pycode.parser import Parser [as 别名]
# 或者: from sphinx.pycode.parser.Parser import parse [as 别名]
def test_obj_assignment():
source = ('obj = SomeObject() #: some object\n'
'obj.attr = 1 #: attr1\n'
'obj.attr.attr = 1 #: attr2\n')
parser = Parser(source)
parser.parse()
assert parser.comments == {('', 'obj'): 'some object'}
assert parser.definitions == {}
示例4: test_annotated_assignment_py36
# 需要导入模块: from sphinx.pycode.parser import Parser [as 别名]
# 或者: from sphinx.pycode.parser.Parser import parse [as 别名]
def test_annotated_assignment_py36():
source = ('a: str = "Sphinx" #: comment\n'
'b: int = 1\n'
'"""string on next line"""')
parser = Parser(source)
parser.parse()
assert parser.comments == {('', 'a'): 'comment',
('', 'b'): 'string on next line'}
assert parser.definitions == {}
示例5: test_class_comment
# 需要导入模块: from sphinx.pycode.parser import Parser [as 别名]
# 或者: from sphinx.pycode.parser.Parser import parse [as 别名]
def test_class_comment():
source = ('import logging\n'
'logger = logging.getLogger(__name__)\n'
'\n'
'class Foo(object):\n'
' """Bar"""\n')
parser = Parser(source)
parser.parse()
assert parser.comments == {}
assert parser.definitions == {'Foo': ('class', 4, 5)}
示例6: test_nested_function
# 需要导入模块: from sphinx.pycode.parser import Parser [as 别名]
# 或者: from sphinx.pycode.parser.Parser import parse [as 别名]
def test_nested_function():
source = ('def some_function():\n'
' a = 1 + 1 #: comment1\n'
'\n'
' def inner_function():\n'
' b = 1 + 1 #: comment2\n')
parser = Parser(source)
parser.parse()
assert parser.comments == {}
assert parser.definitions == {'some_function': ('def', 1, 5)}
assert parser.deforders == {'some_function': 0}
示例7: test_function
# 需要导入模块: from sphinx.pycode.parser import Parser [as 别名]
# 或者: from sphinx.pycode.parser.Parser import parse [as 别名]
def test_function():
source = ('def some_function():\n'
' """docstring"""\n'
' a = 1 + 1 #: comment1\n'
'\n'
' b = a #: comment2\n')
parser = Parser(source)
parser.parse()
assert parser.comments == {}
assert parser.definitions == {'some_function': ('def', 1, 5)}
assert parser.deforders == {'some_function': 0}
示例8: test_class_uses_non_self
# 需要导入模块: from sphinx.pycode.parser import Parser [as 别名]
# 或者: from sphinx.pycode.parser.Parser import parse [as 别名]
def test_class_uses_non_self():
source = ('class Foo(object):\n'
' def __init__(this):\n'
' this.a = 1 + 1 #: comment\n')
parser = Parser(source)
parser.parse()
assert parser.comments == {('Foo', 'a'): 'comment'}
assert parser.definitions == {'Foo': ('class', 1, 3),
'Foo.__init__': ('def', 2, 3)}
assert parser.deforders == {'Foo': 0,
'Foo.__init__': 1,
'Foo.a': 2}
示例9: test_container_assignment
# 需要导入模块: from sphinx.pycode.parser import Parser [as 别名]
# 或者: from sphinx.pycode.parser.Parser import parse [as 别名]
def test_container_assignment():
source = ('l = [] #: list\n'
'l[1] = True #: list assignment\n'
'l[0:0] = [] #: list assignment\n'
'l[_from:_to] = [] #: list assignment\n'
'd = {} #: dict\n'
'd["doc"] = 1 #: dict assignment\n')
parser = Parser(source)
parser.parse()
assert parser.comments == {('', 'l'): 'list',
('', 'd'): 'dict'}
assert parser.definitions == {}
示例10: test_complex_assignment_py3
# 需要导入模块: from sphinx.pycode.parser import Parser [as 别名]
# 或者: from sphinx.pycode.parser.Parser import parse [as 别名]
def test_complex_assignment_py3():
source = ('a, *b, c = (1, 2, 3, 4) #: unpack assignment\n'
'd, *self.attr = (5, 6, 7) #: unpack assignment2\n'
'e, *f[0] = (8, 9, 0) #: unpack assignment3\n'
)
parser = Parser(source)
parser.parse()
assert parser.comments == {('', 'a'): 'unpack assignment',
('', 'b'): 'unpack assignment',
('', 'c'): 'unpack assignment',
('', 'd'): 'unpack assignment2',
('', 'e'): 'unpack assignment3',
}
assert parser.definitions == {}
示例11: test_complex_assignment
# 需要导入模块: from sphinx.pycode.parser import Parser [as 别名]
# 或者: from sphinx.pycode.parser.Parser import parse [as 别名]
def test_complex_assignment():
source = ('a = 1 + 1; b = a #: compound statement\n'
'c, d = (1, 1) #: unpack assignment\n'
'e = True #: first assignment\n'
'e = False #: second assignment\n'
'f = g = None #: multiple assignment at once\n')
parser = Parser(source)
parser.parse()
assert parser.comments == {('', 'b'): 'compound statement',
('', 'c'): 'unpack assignment',
('', 'd'): 'unpack assignment',
('', 'e'): 'second assignment',
('', 'f'): 'multiple assignment at once',
('', 'g'): 'multiple assignment at once'}
assert parser.definitions == {}
示例12: test_comment_picker_multiline_string
# 需要导入模块: from sphinx.pycode.parser import Parser [as 别名]
# 或者: from sphinx.pycode.parser.Parser import parse [as 别名]
def test_comment_picker_multiline_string():
source = ('class Foo(object):\n'
' a = None\n'
' """multiline\n'
' docstring\n'
' """\n'
' b = None\n'
' """\n'
' docstring\n'
' starts with::\n'
'\n'
' empty line"""\n')
parser = Parser(source)
parser.parse()
assert parser.comments == {('Foo', 'a'): 'multiline\ndocstring',
('Foo', 'b'): 'docstring\nstarts with::\n\n empty line'}
示例13: test_nested_class
# 需要导入模块: from sphinx.pycode.parser import Parser [as 别名]
# 或者: from sphinx.pycode.parser.Parser import parse [as 别名]
def test_nested_class():
source = ('class Foo(object):\n'
' attr1 = None #: comment1\n'
'\n'
' class Bar(object):\n'
' attr2 = None #: comment2\n')
parser = Parser(source)
parser.parse()
assert parser.comments == {('Foo', 'attr1'): 'comment1',
('Foo.Bar', 'attr2'): 'comment2'}
assert parser.definitions == {'Foo': ('class', 1, 5),
'Foo.Bar': ('class', 4, 5)}
assert parser.deforders == {'Foo': 0,
'Foo.attr1': 1,
'Foo.Bar': 2,
'Foo.Bar.attr2': 3}
示例14: parse
# 需要导入模块: from sphinx.pycode.parser import Parser [as 别名]
# 或者: from sphinx.pycode.parser.Parser import parse [as 别名]
def parse(self):
# type: () -> None
"""Parse the source code."""
try:
parser = Parser(self.code, self.encoding)
parser.parse()
self.attr_docs = {}
for (scope, comment) in iteritems(parser.comments):
if comment:
self.attr_docs[scope] = comment.splitlines() + ['']
else:
self.attr_docs[scope] = ['']
self.tags = parser.definitions
self.tagorder = parser.deforders
except Exception as exc:
raise PycodeError('parsing %r failed: %r' % (self.srcname, exc))
示例15: test_comment_picker_basic
# 需要导入模块: from sphinx.pycode.parser import Parser [as 别名]
# 或者: from sphinx.pycode.parser.Parser import parse [as 别名]
def test_comment_picker_basic():
source = ('a = 1 + 1 #: assignment\n'
'b = 1 +\\\n 1 #: assignment including a CR\n'
'c = (1 +\n 1) #: tuple \n'
'd = {1, \n 1} #: set\n'
'e = [1, \n 1] #: list #: additional comment\n'
'f = "abc"\n'
'#: string; comment on next line (ignored)\n'
'g = 1.0\n'
'"""float; string on next line"""\n')
parser = Parser(source)
parser.parse()
assert parser.comments == {('', 'a'): 'assignment',
('', 'b'): 'assignment including a CR',
('', 'c'): 'tuple ',
('', 'd'): ' set',
('', 'e'): 'list #: additional comment',
('', 'g'): 'float; string on next line'}