<commit_before># -*- coding: utf-8 -*-

import logging
import re
from enum import Enum

from coalib.bears.LocalBear import LocalBear
from coalib.results.Diff import Diff
from coalib.results.TextRange import TextRange


class ImportType(Enum):
    DEFAULT = "default"
    MULTI_LINE_SLASH = "multi_line_slash"
    MULTI_LINE_PARENTHESIS = "multi_line_parenthesis"


class PySortImportsBear(LocalBear):
    LANGUAGES = {"Python"}
    _LINE_IMPORTS_SPLIT = re.compile(r"\s*,\s+")

    def run(self, filename, file):
        """
        Sorts imports in source code.

        :type filename: str
        :type file: tuple
        :rtype: types.GeneratorType
        """
        logging.debug("PySortImportsBear checking imports order in file %s.", filename)

        import_groups = self._get_imports_from_file(file)
        group_diff = self._check_imports_group_order(import_groups, file)
        line_diff = self._check_imports_line_order([i for l in import_groups for i in l], file)

        for diff in [group_diff, line_diff]:
            additions, deletions = diff.stats()
            if additions or deletions:
                yield self.new_result(
                    message="Import sort error.",
                    diffs={filename: diff},
                    file=filename
                )

    @staticmethod
    def _get_imports_from_file(file):
        """
        Returns a list of lists of tuples containing imports from file.
        Inner lists are import groups separated by lines not starting with from/import keyword.
        Each tuple contains import itself, start line number of import and end line number

        :type file: tuple
        :rtype: list[list[tuple[str, int, int]]]
        """
        imports = []
        import_groups = [imports]
        import_type = ImportType.DEFAULT
        multi_line_import_start = 0
        multi_line_import_parts = []

        for line_number, line in enumerate(file, start=1):
            line = line.strip()
            if import_type == ImportType.DEFAULT:
                if line.startswith(("from ", "import ")):<commit_msg>Fix PEP violation<commit_after># -*- coding: utf-8 -*-

import logging
import re
from enum import Enum

from coalib.bears.LocalBear import LocalBear
from coalib.results.Diff import Diff
from coalib.results.TextRange import TextRange


class ImportType(Enum):
    DEFAULT = "default"
    MULTI_LINE_SLASH = "multi_line_slash"
    MULTI_LINE_PARENTHESIS = "multi_line_parenthesis"


class PySortImportsBear(LocalBear):
    LANGUAGES = {"Python"}
    _LINE_IMPORTS_SPLIT = re.compile(r"\s*,\s+")

    def run(self, filename, file):
        """
        Sorts imports in source code.

        :type filename: str
        :type file: tuple
        :rtype: types.GeneratorType
        """
        logging.debug("PySortImportsBear checking imports order in file %s.", filename)

        import_groups = self._get_imports_from_file(file)
        group_diff = self._check_imports_group_order(import_groups, file)
        line_diff = self._check_imports_line_order([i for imports in import_groups for i in imports], file)

        for diff in [group_diff, line_diff]:
            additions, deletions = diff.stats()
            if additions or deletions:
                yield self.new_result(
                    message="Import sort error.",
                    diffs={filename: diff},
                    file=filename
                )

    @staticmethod
    def _get_imports_from_file(file):
        """
        Returns a list of lists of tuples containing imports from file.
        Inner lists are import groups separated by lines not starting with from/import keyword.
        Each tuple contains import itself, start line number of import and end line number

        :type file: tuple
        :rtype: list[list[tuple[str, int, int]]]
        """
        imports = []
        import_groups = [imports]
        import_type = ImportType.DEFAULT
        multi_line_import_start = 0
        multi_line_import_parts = []

        for line_number, line in enumerate(file, start=1):
            line = line.strip()
            if import_type == ImportType.DEFAULT:
                if line.startswith(("from ", "import ")):
{'fraction_non_alphanumeric': 0.0850356294536817, 'fraction_numerical': 0.0014251781472684087, 'mean_word_length': 2.3987086359967718, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2235668', 'n_tokens_mistral': 1216, 'n_tokens_neox': 1165, 'n_words': 338}