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


Python isort.SortImports方法代码示例

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


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

示例1: test_imports

# 需要导入模块: import isort [as 别名]
# 或者: from isort import SortImports [as 别名]
def test_imports(self):
        test_dir = os.path.dirname(__file__)
        test_files = (
            f for f in os.listdir(test_dir, absolute=True) if f.endswith('.py')
        )

        root_dir = os.path.dirname(test_dir)
        root_files = (
            f for f in os.listdir(root_dir, absolute=True)
            if f.endswith('.py')
        )

        source_dir = os.path.join(root_dir, 'pottery')
        source_files = (
            f for f in os.listdir(source_dir, absolute=True)
            if f.endswith('.py')
        )

        for f in itertools.chain(test_files, root_files, source_files):
            if not f.endswith(self._EXCLUDES):
                with self.subTest(f=f):
                    assert SortImports(f, check=True).correctly_sorted 
开发者ID:brainix,项目名称:pottery,代码行数:24,代码来源:test_source.py

示例2: format_string

# 需要导入模块: import isort [as 别名]
# 或者: from isort import SortImports [as 别名]
def format_string(cls, old_contents):
        """Format content of a file."""
        new_contents = isort.SortImports(file_contents=old_contents).output
        return old_contents, new_contents, 'utf-8' 
开发者ID:ContinuumIO,项目名称:ciocheck,代码行数:6,代码来源:formatters.py

示例3: format_code

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

        return SortImports(file_contents=code, **options).output 
开发者ID:ryantam626,项目名称:jupyterlab_code_formatter,代码行数:6,代码来源:formatters.py

示例4: sort_imports

# 需要导入模块: import isort [as 别名]
# 或者: from isort import SortImports [as 别名]
def sort_imports(file_name, **arguments):
    try:
        result = SortImports(file_name, **arguments)
        return SortAttempt(result.incorrectly_sorted, result.skipped)
    except IOError as e:
        print("WARNING: Unable to parse file {0} due to {1}".format(file_name, e))
        return None 
开发者ID:AtomLinter,项目名称:linter-pylama,代码行数:9,代码来源:main.py

示例5: run

# 需要导入模块: import isort [as 别名]
# 或者: from isort import SortImports [as 别名]
def run(self):
        arguments = self.arguments
        wrong_sorted_files = False
        arguments['check'] = True
        for path in self.distribution_files():
            for python_file in glob.iglob(os.path.join(path, '*.py')):
                try:
                    incorrectly_sorted = SortImports(python_file, **arguments).incorrectly_sorted
                    if incorrectly_sorted:
                        wrong_sorted_files = True
                except IOError as e:
                    print("WARNING: Unable to parse file {0} due to {1}".format(python_file, e))
        if wrong_sorted_files:
            exit(1) 
开发者ID:AtomLinter,项目名称:linter-pylama,代码行数:16,代码来源:main.py

示例6: __init__

# 需要导入模块: import isort [as 别名]
# 或者: from isort import SortImports [as 别名]
def __init__(self, linter: Optional[PyLinter] = None) -> None:
        BaseChecker.__init__(self, linter)
        self.isort_obj = isort.SortImports(
            file_contents='',
        ) 
开发者ID:PennyDreadfulMTG,项目名称:Penny-Dreadful-Tools,代码行数:7,代码来源:monolith_checker.py

示例7: run

# 需要导入模块: import isort [as 别名]
# 或者: from isort import SortImports [as 别名]
def run(self):
        arguments = self.arguments
        wrong_sorted_files = False
        arguments['check'] = True
        for path in self.distribution_files():
            for python_file in glob.iglob(os.path.join(path, '*.py')):
                try:
                    incorrectly_sorted = SortImports(python_file, **arguments).incorrectly_sorted
                    if incorrectly_sorted:
                        wrong_sorted_files = True
                except IOError as e:
                    print("WARNING: Unable to parse file {0} due to {1}".format(python_file, e))
        if wrong_sorted_files:
            sys.exit(1) 
开发者ID:sofia-netsurv,项目名称:python-netsurv,代码行数:16,代码来源:main.py

示例8: git_hook

# 需要导入模块: import isort [as 别名]
# 或者: from isort import SortImports [as 别名]
def git_hook(strict=False, modify=False):
    """
    Git pre-commit hook to check staged files for isort errors

    :param bool strict - if True, return number of errors on exit,
        causing the hook to fail. If False, return zero so it will
        just act as a warning.
    :param bool modify - if True, fix the sources if they are not
        sorted properly. If False, only report result without
        modifying anything.

    :return number of errors if in strict mode, 0 otherwise.
    """

    # Get list of files modified and staged
    diff_cmd = "git diff-index --cached --name-only --diff-filter=ACMRTUXB HEAD"
    files_modified = get_lines(diff_cmd)

    errors = 0
    for filename in files_modified:
        if filename.endswith('.py'):
            # Get the staged contents of the file
            staged_cmd = "git show :%s" % filename
            staged_contents = get_output(staged_cmd)

            sort = SortImports(
                file_path=filename,
                file_contents=staged_contents.decode(),
                check=True
            )

            if sort.incorrectly_sorted:
                errors += 1
                if modify:
                    SortImports(
                        file_path=filename,
                        file_contents=staged_contents.decode(),
                        check=False,
                    )

    return errors if strict else 0 
开发者ID:sofia-netsurv,项目名称:python-netsurv,代码行数:43,代码来源:hooks.py

示例9: visitPrg

# 需要导入模块: import isort [as 别名]
# 或者: from isort import SortImports [as 别名]
def visitPrg(self, ctx):
        if ctx.classDef():
            self.class_list = [self.visit(classDef.classDefStart())[0] for classDef in ctx.classDef()]
        if ctx.funcDef():
            self.function_list = [self.visit(funcdef.funcDefStart())[0] for funcdef in ctx.funcDef()]

        self.imports = ['from __future__ import division, print_function']
        self.imports.append('from vfp2py import vfpfunc')
        self.imports.append('from vfp2py.vfpfunc import DB, Array, C, F, M, S')
        self.imports.append('from vfp2py.vfpfunc import parameters, lparameters, vfpclass')
        defs = []

        for i, child in enumerate(ctx.children):
            if isinstance(child, ctx.parser.FuncDefContext):
                funcname, decorator, funcbody = self.visit(child)
                if i == 0 and funcname == '_program_main':
                    funcname = CodeStr('MAIN')
                defs += [
                    add_args_to_code('@{}', (decorator,)),
                    add_args_to_code('def {}():', (funcname,)),
                    funcbody
                ]
                if child.lineComment():
                    defs += sum((self.visit(comment) for comment in child.lineComment()), [])
            elif not isinstance(child, antlr4.tree.Tree.TerminalNodeImpl):
                defs += self.visit(child)

        imports = isort.SortImports(file_contents='\n'.join(set(self.imports)), line_length=100000).output.splitlines()
        return  [CodeStr(imp) for imp in imports] + defs 
开发者ID:mwisslead,项目名称:vfp2py,代码行数:31,代码来源:vfp2py_convert_visitor.py

示例10: apply_isort

# 需要导入模块: import isort [as 别名]
# 或者: from isort import SortImports [as 别名]
def apply_isort(code: str) -> str:
    return SortImports(file_contents=code).output 
开发者ID:koxudaxi,项目名称:datamodel-code-generator,代码行数:4,代码来源:format.py

示例11: _get_diff

# 需要导入模块: import isort [as 别名]
# 或者: from isort import SortImports [as 别名]
def _get_diff(self):
        if self.treat_seperated_imports_independently:
            import_stmts = PyImportSortBear._seperate_imports(self.file)
            sorted_imps = []
            for units in import_stmts:
                sort_imports = SortImports(file_contents=''.
                                           join([x[1] for x in units]),
                                           **self.isort_settings)
                sort_imports = sort_imports.output.splitlines(True)
                sorted_imps.append((units, sort_imports))

            diff = Diff(self.file)
            for old, new in sorted_imps:
                start = old[0][0]
                end = start + len(old) - 1
                diff.delete_lines(start, end)
                assert isinstance(new, list)
                diff.add_lines(start, list(new))

            if diff.modified != diff._file:
                return diff
        else:
            sort_imports = SortImports(file_contents=''.join(self.file),
                                       **self.isort_settings)

            new_file = tuple(sort_imports.output.splitlines(True))
            if new_file != tuple(self.file):
                diff = Diff.from_string_arrays(self.file, new_file)
                return diff
        return None 
开发者ID:coala,项目名称:coala-bears,代码行数:32,代码来源:PyImportSortBear.py


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