本文整理汇总了Python中redbaron.RedBaron.dumps方法的典型用法代码示例。如果您正苦于以下问题:Python RedBaron.dumps方法的具体用法?Python RedBaron.dumps怎么用?Python RedBaron.dumps使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类redbaron.RedBaron
的用法示例。
在下文中一共展示了RedBaron.dumps方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _cleanupPyLintComments
# 需要导入模块: from redbaron import RedBaron [as 别名]
# 或者: from redbaron.RedBaron import dumps [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())
示例2: main
# 需要导入模块: from redbaron import RedBaron [as 别名]
# 或者: from redbaron.RedBaron import dumps [as 别名]
def main(meetup, tc=(255, 255, 255), bg=None, *tags):
target_url = meetup
soup = BeautifulSoup(requests.get(target_url).content, "html.parser")
description = soup.find("div", id="groupDesc")
description = (" " * 4).join(map(lambda x: str(x), description.contents)) + (" " * 4)
description = "\n".join(map(lambda x: x.rstrip(), description.split("\n")))
target_meetup_name = target_url.split("/")[-2]
target = target_url.split("/")[-2].lower().replace("-", "_")
if re.match("^\d", target):
target = "_" + target
logo_url = soup.find("img", "photo")["src"] if soup.find("img", "photo") else None
if bg == None:
if logo_url:
palette = extract_colors(Image.open(BytesIO(requests.get(logo_url).content)))
colors = palette.colors
background_color = colors[0].value
text_color = tc
else:
h = (random.randint(1, 100) * 0.618033988749895) % 1
background_color = hsv_to_rgb(h, .5, .95)
text_color = "#000000"
h, s, v = rgb_to_hsv(background_color)
else:
background_color = bg
text_color = tc
# background_color = map(lambda x: (x + 255)/2, background_color)
red = RedBaron(open("agendas/be.py", "r").read())
for i in red("def", recursive=False):
if target < i.name:
break
i.insert_before(template % {
"background_color": rgb_to_hex(background_color) if not (isinstance(background_color, basestring) and background_color.startswith("#")) else background_color,
"text_color": rgb_to_hex(text_color) if not (isinstance(text_color, basestring) and text_color.startswith("#")) else text_color,
"url": target_url,
"tags": ", ".join(map(repr, tags)),
"function_name": target,
"description": description,
"meetup_name": target_meetup_name,
})
red.dumps()
open("agendas/be.py", "w").write(red.dumps())
os.system("python manage.py fetch_events %s" % target)
示例3: main
# 需要导入模块: from redbaron import RedBaron [as 别名]
# 或者: from redbaron.RedBaron import dumps [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())
示例4: insert_output_start_stop_indicators
# 需要导入模块: from redbaron import RedBaron [as 别名]
# 或者: from redbaron.RedBaron import dumps [as 别名]
def insert_output_start_stop_indicators(src):
"""
Insert identifier strings so that output can be segregated from input.
Parameters
----------
src : str
String containing input and output lines.
Returns
-------
str
String with output demarked.
"""
rb = RedBaron(src)
# find lines with trailing comments so we can preserve them properly
lines_with_comments = {}
comments = rb.findAll('comment')
for c in comments:
if c.previous and c.previous.type != 'endl':
lines_with_comments[c.previous] = c
input_block_number = 0
# find all nodes that might produce output
nodes = rb.findAll(lambda identifier: identifier in ['print', 'atomtrailers'])
for r in nodes:
# assume that whatever is in the try block will fail and produce no output
# this way we can properly handle display of error messages in the except
if hasattr(r.parent, 'type') and r.parent.type == 'try':
continue
# Output within if/else statements is not a good idea for docs, because
# we don't know which branch execution will follow and thus where to put
# the output block. Regardless of which branch is taken, though, the
# output blocks must start with the same block number.
if hasattr(r.parent, 'type') and r.parent.type == 'if':
if_block_number = input_block_number
if hasattr(r.parent, 'type') and r.parent.type in ['elif', 'else']:
input_block_number = if_block_number
if is_output_node(r):
# if there was a trailing comment on this line, output goes after it
if r in lines_with_comments:
r = lines_with_comments[r] # r is now the comment
# find the correct node to 'insert_after'
while hasattr(r, 'parent') and not hasattr(r.parent, 'insert'):
r = r.parent
r.insert_after('print(">>>>>%d")\n' % input_block_number)
input_block_number += 1
# curse you, redbaron! stop inserting endl before trailing comments!
for l, c in lines_with_comments.items():
if c.previous and c.previous.type == 'endl':
c.previous.value = ''
return rb.dumps()
示例5: test_comma_proxy_list_indented_set_item
# 需要导入模块: from redbaron import RedBaron [as 别名]
# 或者: from redbaron.RedBaron import dumps [as 别名]
def test_comma_proxy_list_indented_set_item():
red = RedBaron("[\n 1,\n]")
comma_proxy_list = red[0].value
comma_proxy_list[0] = "42"
assert comma_proxy_list[0].type == "int"
assert comma_proxy_list[0].value == "42"
comma_proxy_list[0] = "plop"
assert comma_proxy_list[0].type == "name"
assert comma_proxy_list[0].value == "plop"
assert red.dumps() == "[\n plop,\n]"
示例6: functionalize
# 需要导入模块: from redbaron import RedBaron [as 别名]
# 或者: from redbaron.RedBaron import dumps [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()
示例7: test_comma_proxy_list_set_item
# 需要导入模块: from redbaron import RedBaron [as 别名]
# 或者: from redbaron.RedBaron import dumps [as 别名]
def test_comma_proxy_list_set_item():
red = RedBaron("[1]")
comma_proxy_list = red[0].value
comma_proxy_list[0] = "42"
assert comma_proxy_list[0].type == "int"
assert comma_proxy_list[0].value == 42
comma_proxy_list[0] = "plop"
assert comma_proxy_list[0].type == "name"
assert comma_proxy_list[0].value == "plop"
assert red.dumps() == "[plop]"
示例8: remove_raise_skip_tests
# 需要导入模块: from redbaron import RedBaron [as 别名]
# 或者: from redbaron.RedBaron import dumps [as 别名]
def remove_raise_skip_tests(src):
"""
Remove from the code any raise unittest.SkipTest lines since we don't want those in
what the user sees.
"""
rb = RedBaron(src)
raise_nodes = rb.findAll("RaiseNode")
for rn in raise_nodes:
# only the raise for SkipTest
if rn.value[:2].dumps() == 'unittestSkipTest':
rn.parent.value.remove(rn)
return rb.dumps()
示例9: handle
# 需要导入模块: from redbaron import RedBaron [as 别名]
# 或者: from redbaron.RedBaron import dumps [as 别名]
def handle(self, *args, **options):
call_command('migrate', interactive = False)
try:
company = Entity.objects.get(owner=True)
self.stdout.write('Already configured')
except Entity.DoesNotExist:
entity = Entity()
entity.name = 'Your company'
entity.description = 'Your company description'
entity.endpoint = 'http://localhost:8000/api/1.0'
entity.provider = True
entity.owner = True
entity.save()
# Add dummy liaison
liaison = Liaison()
liaison.name = 'Your Company Support Center'
liaison.email = '[email protected]'
liaison.phone = '123456789'
liaison.address = 'Testing Road'
liaison.zip = '123456'
liaison.city = 'Testicity'
liaison.provider = entity
liaison.save()
base = getattr(settings, 'BASE_DIR')
settings_path = os.path.join(base, 'Exchange', 'settings.py')
with open(settings_path, 'r+') as f:
read_data = f.read()
f.seek(0)
red = RedBaron(read_data)
red.find("assignment", target=lambda x: x.dumps() == "SENDER").value.replace("'" + str(entity.id) + "'")
secret_key = ''.join([choice('[email protected]#$%^&*(-_=+)') for i in range(50)])
red.find("assignment", target=lambda x: x.dumps() == "SECRET_KEY").value.replace("'" + secret_key + "'")
f.truncate()
code = red.dumps()
f.write(code)
f.closed
print('Database content created')
print('Provider configured')
print('New secret key generated')
self.stdout.write('Setup complete')
示例10: reform_input
# 需要导入模块: from redbaron import RedBaron [as 别名]
# 或者: from redbaron.RedBaron import dumps [as 别名]
def reform_input(args, method="foo"):
"""Re-give the def repr.
- args: a list of dicts, representing redbaron's arguments
- return: something like "def foo(args):" (without 'pass')
"""
# https://redbaron.readthedocs.io/en/latest/nodes_reference.html#funcdefnode
args = interpose_commas(args)
newdef = "def {}(): pass".format(method)
red = RedBaron(newdef)
red[0].arguments = args
res = red.dumps().strip()
res = res.strip(" pass")
return res
示例11: replace_asserts_with_prints
# 需要导入模块: from redbaron import RedBaron [as 别名]
# 或者: from redbaron.RedBaron import dumps [as 别名]
def replace_asserts_with_prints(source_code):
"""
Replace asserts with print statements.
Using RedBaron, replace some assert calls with print statements that print the actual
value given in the asserts.
Depending on the calls, the actual value can be the first or second
argument.
"""
rb = RedBaron(source_code) # convert to RedBaron internal structure
for assert_type in ['assertAlmostEqual', 'assertLess', 'assertGreater', 'assertEqual',
'assert_equal_arrays', 'assertTrue', 'assertFalse']:
assert_nodes = rb.findAll("NameNode", value=assert_type)
for assert_node in assert_nodes:
assert_node = assert_node.parent
remove_redbaron_node(assert_node, 0) # remove 'self' from the call
assert_node.value[0].replace('print')
if assert_type not in ['assertTrue', 'assertFalse']:
remove_redbaron_node(assert_node.value[1], 1) # remove the expected value argument
assert_nodes = rb.findAll("NameNode", value='assert_rel_error')
for assert_node in assert_nodes:
assert_node = assert_node.parent
# If relative error tolerance is specified, there are 4 arguments
if len(assert_node.value[1]) == 4:
remove_redbaron_node(assert_node.value[1], -1) # remove the relative error tolerance
remove_redbaron_node(assert_node.value[1], -1) # remove the expected value
remove_redbaron_node(assert_node.value[1], 0) # remove the first argument which is
# the TestCase
assert_node.value[0].replace("print")
assert_nodes = rb.findAll("NameNode", value='assert_almost_equal')
for assert_node in assert_nodes:
assert_node = assert_node.parent
# If relative error tolerance is specified, there are 3 arguments
if len(assert_node.value[1]) == 3:
remove_redbaron_node(assert_node.value[1], -1) # remove the relative error tolerance
remove_redbaron_node(assert_node.value[1], -1) # remove the expected value
assert_node.value[0].replace("print")
source_code_with_prints = rb.dumps() # get back the string representation of the code
return source_code_with_prints
示例12: test_comma_proxy_list_indented_set_slice
# 需要导入模块: from redbaron import RedBaron [as 别名]
# 或者: from redbaron.RedBaron import dumps [as 别名]
def test_comma_proxy_list_indented_set_slice():
red = RedBaron("[\n 1,\n 2,\n 3,\n]")
comma_proxy_list = red[0].value
comma_proxy_list[1:2] = ["42", "31", "23"]
assert red.dumps() == "[\n 1,\n 42,\n 31,\n 23,\n 3,\n]"
示例13: test_comma_proxy_list_indented_delslice
# 需要导入模块: from redbaron import RedBaron [as 别名]
# 或者: from redbaron.RedBaron import dumps [as 别名]
def test_comma_proxy_list_indented_delslice():
red = RedBaron("[\n 1,\n 2,\n 3,\n 4,\n 5,\n 6,\n]")
comma_proxy_list = red[0].value
del comma_proxy_list[1:4]
assert red.dumps() == "[\n 1,\n 5,\n 6,\n]"
示例14: test_comma_proxy_list_indented_remove_2_at_top
# 需要导入模块: from redbaron import RedBaron [as 别名]
# 或者: from redbaron.RedBaron import dumps [as 别名]
def test_comma_proxy_list_indented_remove_2_at_top():
red = RedBaron("[\n 2,\n 1,\n]")
comma_proxy_list = red[0].value
comma_proxy_list.remove(comma_proxy_list[0])
assert red.dumps() == "[\n 1,\n]"
示例15: test_line_proxy_with_blank_line_list_remove_2
# 需要导入模块: from redbaron import RedBaron [as 别名]
# 或者: from redbaron.RedBaron import dumps [as 别名]
def test_line_proxy_with_blank_line_list_remove_2():
red = RedBaron("while a:\n pass\n\n plop\n c\n pass\n")
red[0].value.remove(red[0].value[1])
assert red.dumps() == "while a:\n pass\n plop\n c\n pass\n"