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


Python black.FileMode方法代码示例

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


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

示例1: format_signature

# 需要导入模块: import black [as 别名]
# 或者: from black import FileMode [as 别名]
def format_signature(
    signature_start: str, signature_end: str, max_line_length: int = 110
):
    """pretty formatting to avoid long signatures on one single line"""

    # first, we make it look like a real function declaration.
    fake_signature_start = 'x' * len(signature_start)
    fake_signature = fake_signature_start + signature_end
    fake_python_code = f'def {fake_signature}:\n    pass\n'

    # we format with black
    mode = black.FileMode(line_length=max_line_length)
    formatted_fake_python_code = black.format_str(fake_python_code, mode=mode)

    # we make the final, multiline signature
    new_signature_end = extract_signature_end(formatted_fake_python_code)
    return signature_start + new_signature_end 
开发者ID:keras-team,项目名称:keras-autodoc,代码行数:19,代码来源:get_signatures.py

示例2: apply_black

# 需要导入模块: import black [as 别名]
# 或者: from black import FileMode [as 别名]
def apply_black(code: str, python_version: PythonVersion) -> str:

    root = black.find_project_root((Path().resolve(),))
    path = root / "pyproject.toml"
    if path.is_file():
        value = str(path)
        pyproject_toml = toml.load(value)
        config = pyproject_toml.get("tool", {}).get("black", {})
    else:
        config = {}

    return black.format_str(
        code,
        mode=black.FileMode(
            target_versions={BLACK_PYTHON_VERSION[python_version]},
            line_length=config.get("line-length", black.DEFAULT_LINE_LENGTH),
            string_normalization=not config.get("skip-string-normalization", True),
        ),
    ) 
开发者ID:koxudaxi,项目名称:datamodel-code-generator,代码行数:21,代码来源:format.py

示例3: test_format_file_contents

# 需要导入模块: import black [as 别名]
# 或者: from black import FileMode [as 别名]
def test_format_file_contents(self) -> None:
        empty = ""
        mode = black.FileMode()
        with self.assertRaises(black.NothingChanged):
            black.format_file_contents(empty, mode=mode, fast=False)
        just_nl = "\n"
        with self.assertRaises(black.NothingChanged):
            black.format_file_contents(just_nl, mode=mode, fast=False)
        same = "j = [1, 2, 3]\n"
        with self.assertRaises(black.NothingChanged):
            black.format_file_contents(same, mode=mode, fast=False)
        different = "j = [1,2,3]"
        expected = same
        actual = black.format_file_contents(different, mode=mode, fast=False)
        self.assertEqual(expected, actual)
        invalid = "return if you can"
        with self.assertRaises(black.InvalidInput) as e:
            black.format_file_contents(invalid, mode=mode, fast=False)
        self.assertEqual(str(e.exception), "Cannot parse: 1:7: return if you can") 
开发者ID:psf,项目名称:black,代码行数:21,代码来源:test_black.py

示例4: test_single_file_force_pyi

# 需要导入模块: import black [as 别名]
# 或者: from black import FileMode [as 别名]
def test_single_file_force_pyi(self) -> None:
        reg_mode = black.FileMode()
        pyi_mode = black.FileMode(is_pyi=True)
        contents, expected = read_data("force_pyi")
        with cache_dir() as workspace:
            path = (workspace / "file.py").resolve()
            with open(path, "w") as fh:
                fh.write(contents)
            self.invokeBlack([str(path), "--pyi"])
            with open(path, "r") as fh:
                actual = fh.read()
            # verify cache with --pyi is separate
            pyi_cache = black.read_cache(pyi_mode)
            self.assertIn(path, pyi_cache)
            normal_cache = black.read_cache(reg_mode)
            self.assertNotIn(path, normal_cache)
        self.assertEqual(actual, expected) 
开发者ID:psf,项目名称:black,代码行数:19,代码来源:test_black.py

示例5: test_multi_file_force_pyi

# 需要导入模块: import black [as 别名]
# 或者: from black import FileMode [as 别名]
def test_multi_file_force_pyi(self) -> None:
        reg_mode = black.FileMode()
        pyi_mode = black.FileMode(is_pyi=True)
        contents, expected = read_data("force_pyi")
        with cache_dir() as workspace:
            paths = [
                (workspace / "file1.py").resolve(),
                (workspace / "file2.py").resolve(),
            ]
            for path in paths:
                with open(path, "w") as fh:
                    fh.write(contents)
            self.invokeBlack([str(p) for p in paths] + ["--pyi"])
            for path in paths:
                with open(path, "r") as fh:
                    actual = fh.read()
                self.assertEqual(actual, expected)
            # verify cache with --pyi is separate
            pyi_cache = black.read_cache(pyi_mode)
            normal_cache = black.read_cache(reg_mode)
            for path in paths:
                self.assertIn(path, pyi_cache)
                self.assertNotIn(path, normal_cache) 
开发者ID:psf,项目名称:black,代码行数:25,代码来源:test_black.py

示例6: test_multi_file_force_py36

# 需要导入模块: import black [as 别名]
# 或者: from black import FileMode [as 别名]
def test_multi_file_force_py36(self) -> None:
        reg_mode = black.FileMode()
        py36_mode = black.FileMode(target_versions=black.PY36_VERSIONS)
        source, expected = read_data("force_py36")
        with cache_dir() as workspace:
            paths = [
                (workspace / "file1.py").resolve(),
                (workspace / "file2.py").resolve(),
            ]
            for path in paths:
                with open(path, "w") as fh:
                    fh.write(source)
            self.invokeBlack([str(p) for p in paths] + PY36_ARGS)
            for path in paths:
                with open(path, "r") as fh:
                    actual = fh.read()
                self.assertEqual(actual, expected)
            # verify cache with --target-version is separate
            pyi_cache = black.read_cache(py36_mode)
            normal_cache = black.read_cache(reg_mode)
            for path in paths:
                self.assertIn(path, pyi_cache)
                self.assertNotIn(path, normal_cache) 
开发者ID:psf,项目名称:black,代码行数:25,代码来源:test_black.py

示例7: format_file

# 需要导入模块: import black [as 别名]
# 或者: from black import FileMode [as 别名]
def format_file(
        filename: str, black_mode: black.FileMode, skip_errors: bool,
) -> int:
    with open(filename, encoding='UTF-8') as f:
        contents = f.read()
    new_contents, errors = format_str(contents, black_mode)
    for error in errors:
        lineno = contents[:error.offset].count('\n') + 1
        print(f'{filename}:{lineno}: code block parse error {error.exc}')
    if errors and not skip_errors:
        return 1
    if contents != new_contents:
        print(f'{filename}: Rewriting...')
        with open(filename, 'w', encoding='UTF-8') as f:
            f.write(new_contents)
        return 1
    else:
        return 0 
开发者ID:asottile,项目名称:blacken-docs,代码行数:20,代码来源:blacken_docs.py

示例8: handle_options

# 需要导入模块: import black [as 别名]
# 或者: from black import FileMode [as 别名]
def handle_options(**options):
        import black

        file_mode_change_version = version.parse("19.3b0")
        current_black_version = version.parse(black.__version__)
        if current_black_version >= file_mode_change_version:
            return {"mode": black.FileMode(**options)}
        else:
            return options 
开发者ID:ryantam626,项目名称:jupyterlab_code_formatter,代码行数:11,代码来源:formatters.py

示例9: format_blueprint_command

# 需要导入模块: import black [as 别名]
# 或者: from black import FileMode [as 别名]
def format_blueprint_command(bp_file):
    path = pathlib.Path(bp_file)
    LOG.debug("Formatting blueprint {} using black".format(path))
    if format_file_in_place(
        path, fast=False, mode=FileMode(), write_back=WriteBack.DIFF
    ):
        LOG.info("Patching above diff to blueprint - {}".format(path))
        format_file_in_place(
            path, fast=False, mode=FileMode(), write_back=WriteBack.YES
        )
        LOG.info("All done!")
    else:
        LOG.info("Blueprint {} left unchanged.".format(path)) 
开发者ID:nutanix,项目名称:calm-dsl,代码行数:15,代码来源:bps.py

示例10: _format_script

# 需要导入模块: import black [as 别名]
# 或者: from black import FileMode [as 别名]
def _format_script(script):
    try:
        import black
        formatter = partial(
            black.format_str, mode=black.FileMode(line_length=80)
        )
    except ImportError:
        # use autopep8 for python3.5
        import autopep8
        formatter = partial(
            autopep8.fix_code, options={"aggressive": 1}
        )

    return formatter(script) 
开发者ID:pandera-dev,项目名称:pandera,代码行数:16,代码来源:io.py

示例11: format_text

# 需要导入模块: import black [as 别名]
# 或者: from black import FileMode [as 别名]
def format_text(*, text, config):
    mode = black.FileMode(
        target_versions=config["target_version"],
        line_length=config["line_length"],
        is_pyi=config["pyi"],
        string_normalization=not config["skip_string_normalization"],
    )

    return black.format_file_contents(text, fast=config["fast"], mode=mode) 
开发者ID:rupert,项目名称:pyls-black,代码行数:11,代码来源:plugin.py

示例12: _format_code

# 需要导入模块: import black [as 别名]
# 或者: from black import FileMode [as 别名]
def _format_code(code):
        return format_str(src_contents=code, mode=FileMode()) 
开发者ID:dnanhkhoa,项目名称:nb_black,代码行数:4,代码来源:lab_black.py

示例13: black

# 需要导入模块: import black [as 别名]
# 或者: from black import FileMode [as 别名]
def black(self, line, cell):
        """Magic command to format the IPython cell."""
        args = magic_arguments.parse_argstring(self.black, line)
        line_length = args.line_length
        if cell:
            try:
                from black import FileMode
                mode = FileMode(line_length=line_length)
                formatted = format_str(src_contents=cell, mode=mode)
            except TypeError:
                formatted = format_str(src_contents=cell, line_length=line_length)
            if formatted and formatted[-1] == "\n":
                    formatted = formatted[:-1]
            self.shell.set_next_input(formatted, replace=True) 
开发者ID:csurfer,项目名称:blackcellmagic,代码行数:16,代码来源:blackcellmagic.py

示例14: checkSourceFile

# 需要导入模块: import black [as 别名]
# 或者: from black import FileMode [as 别名]
def checkSourceFile(self, name: str) -> None:
        path = THIS_DIR.parent / name
        source, expected = read_data(str(path), data=False)
        actual = fs(source)
        self.assertFormatEqual(expected, actual)
        black.assert_equivalent(source, actual)
        black.assert_stable(source, actual, black.FileMode())
        self.assertFalse(ff(path)) 
开发者ID:psf,项目名称:black,代码行数:10,代码来源:test_black.py

示例15: test_empty

# 需要导入模块: import black [as 别名]
# 或者: from black import FileMode [as 别名]
def test_empty(self) -> None:
        source = expected = ""
        actual = fs(source)
        self.assertFormatEqual(expected, actual)
        black.assert_equivalent(source, actual)
        black.assert_stable(source, actual, black.FileMode()) 
开发者ID:psf,项目名称:black,代码行数:8,代码来源:test_black.py


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