當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。