本文整理汇总了Python中redbaron.RedBaron.find_all方法的典型用法代码示例。如果您正苦于以下问题:Python RedBaron.find_all方法的具体用法?Python RedBaron.find_all怎么用?Python RedBaron.find_all使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类redbaron.RedBaron
的用法示例。
在下文中一共展示了RedBaron.find_all方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: functionalize
# 需要导入模块: from redbaron import RedBaron [as 别名]
# 或者: from redbaron.RedBaron import find_all [as 别名]
def functionalize(src):
red = RedBaron(src)
red.insert(0, 'import pync')
for func in red.find_all('def'):
func.decorators.append('@pync.curry')
for l in red.find_all('list') + red.find_all('list_comprehension'):
l.replace("pync.list(%s)" % l)
return red.dumps()
示例2: compare
# 需要导入模块: from redbaron import RedBaron [as 别名]
# 或者: from redbaron.RedBaron import find_all [as 别名]
def compare(s1, s2, decay_factor = DEFAULT_DECAY_FACTOR):
red1 = RedBaron(s1)
red2 = RedBaron(s2)
result = []
for ast_f2 in red2.find_all('def'):
ast_f1 = red1.find('def', name = ast_f2.name)
if ast_f1 is not None:
additions, deletions = preprocess_files(ast_f1.dumps(),
ast_f2.dumps())
comments, exceptions = preprocess_comments(ast_f2, additions)
for a in additions:
for c in comments:
line, _ = c.left_bounds
distance = math.fabs(line - a)
score = int(c.score() - float(decay_factor) / (distance * distance))
c.setScore(score if score > 0 else 0)
for d in deletions:
for c in comments:
line, _ = c.left_bounds
line = line + 1 if line >= d else line
distance = math.fabs(line - d)
score = int(c.score() - float(decay_factor) / (distance * distance))
c.setScore(score if score > 0 else 0)
result.extend(comments)
result.extend(exceptions)
else:
result.extend(preprocess_comments(ast_f2, []))
return result
示例3: _cleanupPyLintComments
# 需要导入模块: from redbaron import RedBaron [as 别名]
# 或者: from redbaron.RedBaron import find_all [as 别名]
def _cleanupPyLintComments(filename, abort):
from baron.parser import ( # pylint: disable=I0021,import-error,no-name-in-module
ParsingError, # @UnresolvedImport
)
from redbaron import ( # pylint: disable=I0021,import-error,no-name-in-module
RedBaron, # @UnresolvedImport
)
old_code = getFileContents(filename)
try:
red = RedBaron(old_code)
# red = RedBaron(old_code.rstrip()+'\n')
except ParsingError:
if abort:
raise
my_print("PARSING ERROR.")
return 2
for node in red.find_all("CommentNode"):
try:
_updateCommentNode(node)
except Exception:
my_print("Problem with", node)
node.help(deep=True, with_formatting=True)
raise
new_code = red.dumps()
if new_code != old_code:
with open(filename, "w") as source_code:
source_code.write(red.dumps())
示例4: main
# 需要导入模块: from redbaron import RedBaron [as 别名]
# 或者: from redbaron.RedBaron import find_all [as 别名]
def main():
"""Rewrite Thrift-generated Python clients to handle recursive structs. For
more details see: https://issues.apache.org/jira/browse/THRIFT-2642.
Requires package `RedBaron`, available via pip:
$ pip install redbaron
To use:
$ thrift -gen py mapd.thrift
$ mv gen-py/mapd/ttypes.py gen-py/mapd/ttypes-backup.py
$ python fix_recursive_structs.py gen-py/mapd/ttypes-backup.py gen-py/mapd/ttypes.py
"""
in_file = open(sys.argv[1], 'r')
out_file = open(sys.argv[2], 'w')
red_ast = RedBaron(in_file.read())
thrift_specs = [ts.parent for ts in red_ast.find_all(
'name', 'thrift_spec') if ts.parent.type == 'assignment' and ts.parent.parent.name in ['TDatumVal', 'TColumnData']]
nodes = []
for ts in thrift_specs:
node = ts.copy()
node.target = ts.parent.name + '.' + str(node.target)
nodes.append(node)
ts.value = 'None'
red_ast.extend(nodes)
out_file.write(red_ast.dumps())
示例5: test_node_previous_recursive
# 需要导入模块: from redbaron import RedBaron [as 别名]
# 或者: from redbaron.RedBaron import find_all [as 别名]
def test_node_previous_recursive():
red = RedBaron("def a():\n b = 1\ndef c():\n d = 1")
assert red.previous is None
assert red.previous_recursive is None
first, second = red.find_all('def')
assert second.previous is first
inner = second.value.node_list
assert inner[2].previous == inner[1]
assert inner[2].previous_recursive == inner[1]
assert inner[1].previous == inner[0]
assert inner[1].previous_recursive == inner[0]
assert inner[0].previous == None
assert inner[0].previous_recursive == first
示例6: test_node_next_recursive
# 需要导入模块: from redbaron import RedBaron [as 别名]
# 或者: from redbaron.RedBaron import find_all [as 别名]
def test_node_next_recursive():
red = RedBaron("def a():\n b = 1\ndef c():\n d = 1")
assert red.next is None
assert red.next_recursive is None
first, second = red.find_all('def')
assert first.next is second
inner = first.value.node_list
assert inner[0].next == inner[1]
assert inner[0].next_recursive == inner[1]
assert inner[1].next == inner[2]
assert inner[1].next_recursive == inner[2]
assert inner[2].next == None
assert inner[2].next_recursive == second
示例7: compare
# 需要导入模块: from redbaron import RedBaron [as 别名]
# 或者: from redbaron.RedBaron import find_all [as 别名]
def compare(s1, s2, decay_factor = PYTHON_DECAY_FACTOR):
try:
red1 = RedBaron(s1)
red2 = RedBaron(s2)
result = []
defs = red2.find_all('def')
length = len(defs)
for ast_f2 in defs:
ast_f1 = red1.find('def', name = ast_f2.name)
if ast_f1 is not None:
additions, deletions = preprocess_files(ast_f1.dumps(),
ast_f2.dumps(),
ast_f2.absolute_bounding_box.top_left.line)
comments, exceptions = preprocess_comments(ast_f2, additions)
for a in additions:
for c in comments:
line, _ = c.left_bounds
distance = line - a
score = compute_addition(c, distance, decay_factor)
c.setScore(score if score > 0 else 0)
for d in deletions:
for c in comments:
line, _ = c.left_bounds
line = line + 1 if line >= d else line
distance = line - d
score = compute_deletion(c, distance, decay_factor)
c.setScore(score if score > 0 else 0)
result.extend(comments)
result.extend(exceptions)
else:
comments, _ = preprocess_comments(ast_f2, [])
result.extend(comments)
print_to_log('Result: ' + str(result))
return result
except Exception as e:
err = 'CommentHealth compare error: ' + str(e)
return []
示例8: updateDefNode
# 需要导入模块: from redbaron import RedBaron [as 别名]
# 或者: from redbaron.RedBaron import find_all [as 别名]
def updateDefNode(def_node):
# This is between "def" and function name.
def_node.first_formatting = " "
# This is after the opening/closing brace, we don't want it there.
def_node.third_formatting = ""
def_node.fourth_formatting = ""
# This is to insert/remove spaces or new lines, depending on line length
# so far, but is not functional at all.
for argument_node in def_node.arguments:
argument_node.first_formatting = " "
argument_node.second_formatting = " "
for node in red.find_all("CallNode"):
try:
updateCall(node)
except Exception:
print("Problem with", node)
node.help(deep = True, with_formatting = True)
raise
for node in red.find_all("TupleNode"):
try:
updateTuple(node)
except Exception:
print("Problem with", node)
node.help(deep = True, with_formatting = True)
raise
示例9: open
# 需要导入模块: from redbaron import RedBaron [as 别名]
# 或者: from redbaron.RedBaron import find_all [as 别名]
import copy
import sys
from redbaron import RedBaron
in_file = out_file = sys.argv[1]
code = open(in_file).read()
red_baron = RedBaron(code)
super_nodes = red_baron.find_all('AtomtrailersNode')
for super_node in super_nodes:
node = copy.copy(super_node)
class_name = node.find_all('name')[1].name.dumps()
while node.parent:
node = node.parent
if node.name == class_name:
super_node.value[1] = '()'
with open(out_file, "w") as fh:
fh.write(red_baron.dumps())
示例10: open
# 需要导入模块: from redbaron import RedBaron [as 别名]
# 或者: from redbaron.RedBaron import find_all [as 别名]
import optparse
import json
parser = optparse.OptionParser()
parser.add_option('-s', '--source', dest="source_file")
options, remainder = parser.parse_args()
if options.source_file:
source_code = open(options.source_file, "r")
else:
source_code = sys.stdin
imported_modules = []
red = RedBaron(source_code.read())
import_nodes = red.find_all(lambda identifier: identifier == "import" or identifier == "from_import")
for import_node in import_nodes:
lineno = import_node.absolute_bounding_box.top_left.line
code = import_node.dumps()
modules = None
def is_valid_module(module_path):
return not (module_path.endswith(".(") or module_path.endswith(".)"))
if import_node.type == "from_import":
modules = filter(is_valid_module, import_node.full_path_modules())
elif import_node.type == "import":
modules = import_node.modules()
imported_modules.append({
示例11: open
# 需要导入模块: from redbaron import RedBaron [as 别名]
# 或者: from redbaron.RedBaron import find_all [as 别名]
import copy
import sys
from redbaron import RedBaron
in_file = out_file = sys.argv[1]
code = open(in_file).read()
red_baron = RedBaron(code)
try_nodes = red_baron.find_all('TryNode')
for node in try_nodes:
if node.find('PassNode'):
t_node = copy.copy(super_node)
class_name = node.find_all('name')[1].name.dumps()
while node.parent:
node = node.parent
if node.name == class_name:
super_node.value[1] = '()'
with open(out_file, "w") as fh:
fh.write(red_baron.dumps())