当前位置: 首页>>代码示例>>Python>>正文


Python yapf_api.FormatCode方法代码示例

本文整理汇总了Python中yapf.yapflib.yapf_api.FormatCode方法的典型用法代码示例。如果您正苦于以下问题:Python yapf_api.FormatCode方法的具体用法?Python yapf_api.FormatCode怎么用?Python yapf_api.FormatCode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在yapf.yapflib.yapf_api的用法示例。


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

示例1: format_string

# 需要导入模块: from yapf.yapflib import yapf_api [as 别名]
# 或者: from yapf.yapflib.yapf_api import FormatCode [as 别名]
def format_string(cls, old_contents):
        """Format file for use with task queue."""
        # cmd_root is assigned to formatter inside format_task... ugly!
        style_config = os.path.join(cls.cmd_root, cls.config_file)
        # It might be tempting to use the "inplace" option to FormatFile, but
        # it doesn't do an atomic replace, which is dangerous, so don't use
        # it unless you submit a fix to yapf.
        (new_contents, changed) = FormatCode(
            old_contents, style_config=style_config)

        if platform.system() == 'Windows':
            # yapf screws up line endings on windows
            new_contents = new_contents.replace("\r\n", "\n")

            if len(old_contents) == 0:
                # Windows yapf seems to force a newline? I dunno
                new_contents = ""
        return old_contents, new_contents, 'utf-8' 
开发者ID:ContinuumIO,项目名称:ciocheck,代码行数:20,代码来源:formatters.py

示例2: get_setup

# 需要导入模块: from yapf.yapflib import yapf_api [as 别名]
# 或者: from yapf.yapflib.yapf_api import FormatCode [as 别名]
def get_setup(self):
        # render template
        with self.setup_path.open(encoding='utf-8') as f:
            document = f.read()
        template = Environment().from_string(document)
        document = template.render(
            package=self.package,
            format_vcs=self._format_vcs,
        )

        # format by yapf
        style = CreateGoogleStyle()
        document, _changed = FormatCode(document, style_config=style)
        # remove empty strings
        while '\n\n' in document:
            document = document.replace('\n\n', '\n')
        # format by autopep8
        document = fix_code(document)
        return document 
开发者ID:orsinium-archive,项目名称:poetry-setup,代码行数:21,代码来源:core.py

示例3: process_specification_directory

# 需要导入模块: from yapf.yapflib import yapf_api [as 别名]
# 或者: from yapf.yapflib.yapf_api import FormatCode [as 别名]
def process_specification_directory(glob_pattern, outfile_name, namespace, write_baseclass=True,):
    with open(os.path.join(options.out_path, outfile_name), 'w+') as out_file:
        paths = [p for p in glob.glob(os.path.join(options.spec_path, glob_pattern))]
        classes = list()

        func = functools.partial(process_file, namespace)
        with Pool() as pool:
            classes.extend(pool.map(func, paths))
        print("Formatting...")
        formatted_code = FormatCode("\n".join(sorted(classes)))[0]
        if write_baseclass:
            header = BASE_CLASS
        else:
            header = "from zenpy.lib.api_objects import BaseObject\nimport dateutil.parser"

        out_file.write("\n\n\n".join((header, formatted_code))) 
开发者ID:facetoe,项目名称:zenpy,代码行数:18,代码来源:gen_classes.py

示例4: format_code

# 需要导入模块: from yapf.yapflib import yapf_api [as 别名]
# 或者: from yapf.yapflib.yapf_api import FormatCode [as 别名]
def format_code(self, code: str, notebook: bool, **options) -> str:
        from yapf.yapflib.yapf_api import FormatCode

        return FormatCode(code, **options)[0] 
开发者ID:ryantam626,项目名称:jupyterlab_code_formatter,代码行数:6,代码来源:formatters.py

示例5: _format_olympe_dsl

# 需要导入模块: from yapf.yapflib import yapf_api [as 别名]
# 或者: from yapf.yapflib.yapf_api import FormatCode [as 别名]
def _format_olympe_dsl(code):
    try:
        return FormatCode(code, style_config=_olympe_dsl_style)[0]
    except Exception:
        # Fallback, return unformatted olympe dsl code
        return code 
开发者ID:Parrot-Developers,项目名称:olympe,代码行数:8,代码来源:events.py

示例6: format

# 需要导入模块: from yapf.yapflib import yapf_api [as 别名]
# 或者: from yapf.yapflib.yapf_api import FormatCode [as 别名]
def format(self, style_config: str):
        self._code_data = FormatCode(self._code_data, style_config=style_config)[0] 
开发者ID:nutti,项目名称:fake-bpy-module,代码行数:4,代码来源:generator.py

示例7: exported_function_info

# 需要导入模块: from yapf.yapflib import yapf_api [as 别名]
# 或者: from yapf.yapflib.yapf_api import FormatCode [as 别名]
def exported_function_info(mod, mod_name, func_name) -> Dict[str, Any]:
    func = getattr(mod, func_name)
    sig = inspect.signature(func)

    activity_type = ""
    mod_lastname = mod_name.rsplit(".", 1)[1]
    if mod_lastname == "actions":
        activity_type = "action"
    elif mod_lastname == "probes":
        activity_type = "probe"
    elif mod_lastname == "tolerances":
        activity_type = "tolerance"

    args = build_signature_info(sig)
    return_type = build_return_type_info(sig)
    as_json = called_without_args_info(
        args, mod_name, func_name, activity_type)

    s = ''
    try:
        s = FormatCode("def {}{}:pass".format(func_name, str(sig)))[0]
    except Exception:
        print('Failed to format {} in {}'.format(func_name, mod_name))

    d = inspect.getdoc(func) or ""
    d = d.replace("     ", "")
    d = d.replace("Parameters", "").replace("--------", "")
    d = d.replace("Examples", "").replace("----------", "")

    return {
        "type": activity_type,
        "module": mod_name,
        "name": func_name,
        "doc": d,
        "return": return_type,
        "signature": s,
        "arguments": args,
        "as_json": json.dumps(as_json, indent=2),
        "as_yaml": yaml.dump(as_json, default_flow_style=False)
    } 
开发者ID:chaostoolkit,项目名称:chaostoolkit-documentation,代码行数:42,代码来源:ext2md.py

示例8: testLinesOnRangeBoundary

# 需要导入模块: from yapf.yapflib import yapf_api [as 别名]
# 或者: from yapf.yapflib.yapf_api import FormatCode [as 别名]
def testLinesOnRangeBoundary(self):
    unformatted_code = textwrap.dedent(u"""\
        def A():
          pass

        def B():  # 4
          pass  # 5

        def C():
          pass
        def D():  # 9
          pass  # 10
        def E():
          pass
        """)
    expected_formatted_code = textwrap.dedent(u"""\
        def A():
          pass


        def B():  # 4
          pass  # 5


        def C():
          pass


        def D():  # 9
          pass  # 10


        def E():
          pass
        """)
    code, changed = yapf_api.FormatCode(
        unformatted_code, lines=[(4, 5), (9, 10)])
    self.assertCodeEqual(expected_formatted_code, code)
    self.assertTrue(changed) 
开发者ID:google,项目名称:yapf,代码行数:41,代码来源:blank_line_calculator_test.py

示例9: testLinesRangeBoundaryNotOutside

# 需要导入模块: from yapf.yapflib import yapf_api [as 别名]
# 或者: from yapf.yapflib.yapf_api import FormatCode [as 别名]
def testLinesRangeBoundaryNotOutside(self):
    unformatted_code = textwrap.dedent(u"""\
        def A():
          pass



        def B():  # 6
          pass  # 7



        def C():
          pass
        """)
    expected_formatted_code = textwrap.dedent(u"""\
        def A():
          pass



        def B():  # 6
          pass  # 7



        def C():
          pass
        """)
    code, changed = yapf_api.FormatCode(unformatted_code, lines=[(6, 7)])
    self.assertCodeEqual(expected_formatted_code, code)
    self.assertFalse(changed) 
开发者ID:google,项目名称:yapf,代码行数:34,代码来源:blank_line_calculator_test.py

示例10: testLinesRangeRemove

# 需要导入模块: from yapf.yapflib import yapf_api [as 别名]
# 或者: from yapf.yapflib.yapf_api import FormatCode [as 别名]
def testLinesRangeRemove(self):
    unformatted_code = textwrap.dedent(u"""\
        def A():
          pass



        def B():  # 6
          pass  # 7




        def C():
          pass
        """)
    expected_formatted_code = textwrap.dedent(u"""\
        def A():
          pass


        def B():  # 6
          pass  # 7


        def C():
          pass
        """)
    code, changed = yapf_api.FormatCode(unformatted_code, lines=[(5, 9)])
    self.assertCodeEqual(expected_formatted_code, code)
    self.assertTrue(changed) 
开发者ID:google,项目名称:yapf,代码行数:33,代码来源:blank_line_calculator_test.py

示例11: testLinesRangeRemoveSome

# 需要导入模块: from yapf.yapflib import yapf_api [as 别名]
# 或者: from yapf.yapflib.yapf_api import FormatCode [as 别名]
def testLinesRangeRemoveSome(self):
    unformatted_code = textwrap.dedent(u"""\
        def A():
          pass




        def B():  # 7
          pass  # 8




        def C():
          pass
        """)
    expected_formatted_code = textwrap.dedent(u"""\
        def A():
          pass



        def B():  # 7
          pass  # 8



        def C():
          pass
        """)
    code, changed = yapf_api.FormatCode(unformatted_code, lines=[(6, 9)])
    self.assertCodeEqual(expected_formatted_code, code)
    self.assertTrue(changed) 
开发者ID:google,项目名称:yapf,代码行数:36,代码来源:blank_line_calculator_test.py

示例12: _Check

# 需要导入模块: from yapf.yapflib import yapf_api [as 别名]
# 或者: from yapf.yapflib.yapf_api import FormatCode [as 别名]
def _Check(self, unformatted_code, expected_formatted_code):
    formatted_code, _ = yapf_api.FormatCode(
        unformatted_code, style_config='yapf')
    self.assertCodeEqual(expected_formatted_code, formatted_code) 
开发者ID:google,项目名称:yapf,代码行数:6,代码来源:yapf_test.py

示例13: testBadCode

# 需要导入模块: from yapf.yapflib import yapf_api [as 别名]
# 或者: from yapf.yapflib.yapf_api import FormatCode [as 别名]
def testBadCode(self):
    code = 'x = """hello\n'
    self.assertRaises(tokenize.TokenError, yapf_api.FormatCode, code) 
开发者ID:google,项目名称:yapf,代码行数:5,代码来源:yapf_test.py

示例14: _format_code

# 需要导入模块: from yapf.yapflib import yapf_api [as 别名]
# 或者: from yapf.yapflib.yapf_api import FormatCode [as 别名]
def _format_code(code):
        return FormatCode(code, style_config="facebook")[0] 
开发者ID:dnanhkhoa,项目名称:nb_black,代码行数:4,代码来源:lab_black.py

示例15: fix_code

# 需要导入模块: from yapf.yapflib import yapf_api [as 别名]
# 或者: from yapf.yapflib.yapf_api import FormatCode [as 别名]
def fix_code(source_code, line_ranges, options=None, verbose=0):
    '''Apply autopep8 over the line_ranges, returns the corrected code.

    Note: though this is not checked for line_ranges should not overlap.

    Example
    -------
    >>> code = "def f( x ):\\n  if  True:\\n    return 2*x"
    >>> print(fix_code(code, [(1, 1), (3, 3)]))
    def f(x):
      if  True:
          return 2 * x

    '''
    if options is None:
        from pep8radius.main import parse_args
        options = parse_args()

    if getattr(options, "yapf", False):
        from yapf.yapflib.yapf_api import FormatCode
        result = FormatCode(source_code, style_config=options.style, lines=line_ranges)
        # yapf<0.3 returns diff as str, >=0.3 returns a tuple of (diff, changed)
        return result[0] if isinstance(result, tuple) else result

    line_ranges = reversed(line_ranges)
    # Apply line fixes "up" the file (i.e. in reverse) so that
    # fixes do not affect changes we're yet to make.
    partial = source_code
    for start, end in line_ranges:
        partial = fix_line_range(partial, start, end, options)
        _maybe_print('.', end='', max_=1, verbose=verbose)
    _maybe_print('', max_=1, verbose=verbose)
    fixed = partial
    return fixed 
开发者ID:hayd,项目名称:pep8radius,代码行数:36,代码来源:radius.py


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