• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Python redbaron.RedBaron类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Python中redbaron.RedBaron的典型用法代码示例。如果您正苦于以下问题:Python RedBaron类的具体用法?Python RedBaron怎么用?Python RedBaron使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



在下文中一共展示了RedBaron类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。

示例1: main

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())
开发者ID:kanak,项目名称:mapd-core,代码行数:31,代码来源:fix_recursive_structs.py


示例2: test_node_elif_ifelseblock_next

def test_node_elif_ifelseblock_next():
    red = RedBaron("if a:\n    pass\nelif a:\n    pass")
    assert red.elif_.next is None
    red = RedBaron("if a:\n    pass\nelif a:\n    pass\nelse:\n    pass")
    assert red.elif_.next is red.else_
    red = RedBaron("if a:\n    pass\nelif a:\n    pass\nchocolat")
    assert red.elif_.next is red.find("name", "chocolat")
开发者ID:bosr,项目名称:redbaron,代码行数:7,代码来源:test_initial_parsing.py


示例3: _cleanupPyLintComments

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())
开发者ID:kayhayen,项目名称:Nuitka,代码行数:33,代码来源:Autoformat.py


示例4: insert_output_start_stop_indicators

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()
开发者ID:samtx,项目名称:OpenMDAO,代码行数:60,代码来源:docutil.py


示例5: compare

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
开发者ID:cbonoz,项目名称:codehealth,代码行数:31,代码来源:codehealth_on_save.py


示例6: test_find_empty

def test_find_empty():
    red = RedBaron("")
    assert red.find("stuff") is None
    assert red.find("something_else") is None
    assert red.find("something_else", useless="pouet") is None
    with pytest.raises(AttributeError):
        red.will_raises
开发者ID:SylvainDe,项目名称:redbaron,代码行数:7,代码来源:test_initial_parsing.py


示例7: readCode

 def readCode(self,modulepath=None):
     result=Bag()
     modulepath=modulepath or sys.modules[self.__module__].__file__
     with open(modulepath, "r") as source_code:
         red = RedBaron(source_code.read())
     result.fromJson(red.fst())
     return result
开发者ID:genropy,项目名称:genropy,代码行数:7,代码来源:baron.py


示例8: test_node_if_ifelseblock_previous_intuitive

def test_node_if_ifelseblock_previous_intuitive():
    red = RedBaron("if a:\n    pass")
    assert red.if_.previous_intuitive is None
    red = RedBaron("chocolat\nif a:\n    pass")
    assert red.if_.previous_intuitive is red.find("endl")
    red = RedBaron("pouet\nif a:\n    pass\nelif a:\n    pass\nelse:\n    pass")
    assert red.else_.previous_intuitive is red.elif_
    assert red.if_.previous is None
开发者ID:SylvainDe,项目名称:redbaron,代码行数:8,代码来源:test_initial_parsing.py


示例9: test_node_else_elseelseblock_next_generator

def test_node_else_elseelseblock_next_generator():
    red = RedBaron("if a:\n    pass\nelse:\n    pass")
    assert len(list(red.else_.next_generator())) == 0
    red = RedBaron("if a:\n    pass\nelse:\n    pass\nchocolat")
    assert list(red.else_.next_generator())[0] is red.find("name", "chocolat")

    red = RedBaron("if a:\n    pass\nelse:\n    pass\nchocolat")
    assert list(red.else_.next_generator()) == [red.find("name", "chocolat")]
开发者ID:bosr,项目名称:redbaron,代码行数:8,代码来源:test_initial_parsing.py


示例10: main

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)
开发者ID:Psycojoker,项目名称:hackeragenda,代码行数:58,代码来源:add_meetup.py


示例11: test_comma_proxy_list_indented_set_item

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]"
开发者ID:SylvainDe,项目名称:redbaron,代码行数:10,代码来源:test_proxy_list.py


示例12: test_comma_proxy_list_set_item

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]"
开发者ID:shs96c,项目名称:redbaron,代码行数:10,代码来源:test_proxy_list.py


示例13: replace_argument

def replace_argument(txt, pos, new):
    """
    """
    red = RedBaron(txt)
    fst = red.fst()[0]
    args  = fst['arguments']
    args = filter(arg_type_no_comma, args)
    args.pop(pos)
    args.insert(pos, new)
    res = reform_input(args, method=fst['name'])
    return res
开发者ID:vindarel,项目名称:redbaron4emacs,代码行数:11,代码来源:red4emacs.py


示例14: remove_raise_skip_tests

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()
开发者ID:samtx,项目名称:OpenMDAO,代码行数:12,代码来源:docutil.py


示例15: get_method_body

def get_method_body(method_code):
    '''Using the RedBaron module, get the body of a method.

    Do not want the definition signature line
    '''

    method_code = '\n' + method_code  # For some reason RedBaron has problems with this if
    #                                                     if it does not start with an empty line
    rb = RedBaron(method_code)
    def_node = rb.findAll("DefNode")[0]  # Look for the 'def' node. Should only be one!
    def_node.value.decrease_indentation(8)  # so that the code is all the way to the left
    return def_node.value.dumps()
开发者ID:samtx,项目名称:OpenMDAO,代码行数:12,代码来源:docutil.py


示例16: test_node_next_recursive

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
开发者ID:bosr,项目名称:redbaron,代码行数:13,代码来源:test_initial_parsing.py


示例17: test_node_previous_recursive

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
开发者ID:bosr,项目名称:redbaron,代码行数:13,代码来源:test_initial_parsing.py


示例18: reform_input

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
开发者ID:vindarel,项目名称:redbaron4emacs,代码行数:15,代码来源:red4emacs.py


示例19: replace_asserts_with_prints

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
开发者ID:samtx,项目名称:OpenMDAO,代码行数:45,代码来源:docutil.py


示例20: test_rendering_iter

def test_rendering_iter():
    red = RedBaron("a + 2")
    assert list(red._generate_nodes_in_rendering_order()) == [
        red[0],
        red.name,
        red[0].first_formatting[0],
        red[0],
        red[0].second_formatting[0],
        red.int,
    ]
    assert list(red[0]._generate_nodes_in_rendering_order()) == [
        red[0],
        red.name,
        red[0].first_formatting[0],
        red[0],
        red[0].second_formatting[0],
        red.int,
    ]
开发者ID:gporcari,项目名称:redbaron,代码行数:18,代码来源:test_render.py



注:本文中的redbaron.RedBaron类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python redcap.Project类代码示例发布时间:2022-05-26
下一篇:
Python app.test_client函数代码示例发布时间:2022-05-26
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap