<commit_before>import yaml
from copy import copy
from typing import Dict, List, Set, Iterator, Any
from bugzoo.testing import TestCase, TestSuite, TestOutcome


class FileLine(object):
    """
    Used to represent an one-indexed line within a specific file.
    """
    @staticmethod
    def compactify(d: Dict['FileLine', Any]) -> Dict[str, Dict[int, Any]]:
        """
        Converts a dictionary that is indexed by FileLine objects into a
        nested dictionary structures that maps from file names to a dictionary
        of line numbers.
        """
        assert isinstance(d, dict)
        assert all(isinstance(x, FileLine) for x in d)

        out: Dict[str, Dict[int, Any]] = {}
        for (line, val) in d.items():
            if not line.filename in out:
                out[line.filename] = {}
            out[line.filename][line.num] = val
        return out

    @staticmethod
    def decompactify(d: Dict[str, Dict[int, Any]]) -> 'Dict[FileLine, Any]':
        lines = {}
        for fn in d:
            for num in d[fn]:
                lines[FileLine(fn, num)] = d[fn][num]
        return lines

    def __init__(self, fn: str, num: int) -> None:
        self.__fn = fn
        self.__num = num

    def __hash__(self) -> int:
        return hash((self.__fn, self.__num))

    def __eq__(self, other) -> bool:
        return  isinstance(other, FileLine) and \
                self.filename == other.filename and \
                self.num == other.num

    @property
    def filename(self) -> str:
        """
        The name of the file to which this line belongs.
        """
        return self.__fn

    fn = filename

    @property
    def num(self) -> int:
        """
        The one-indexed number of this line.
        """
        return self.__num

    def __str__(self) -> str:
        return "{}:{}".format(self.filename, self.num)

    __repr__ = __str__


class FileLineCoverage(object):
    """
    Provides line-level coverage information for a given file.
    """
    T = Dict[int, int]

    def __init__(self, filename: str, lines: T) -> None:
        self.__filename = filename
        self.__lines = \
            {num: hits for (num, hits) in lines.items() if hits > 0}

#    @property
#    def lines(self) -> List[int]:
#        """
#        A list of the one-indexed numbers of the lines that are included in
#        this report.
#        """
#        return list(self.__lines.keys())

    def was_hit(self, num: int) -> bool:
        """
        Determines whether a line with a given number was executed at least
        once during the execution(s).
        """
        return self.hits(num) > 0

    def hits(self, num: int) -> int:
        """
        Returns the number of times that a line with a given number was
        executed.
        """
        assert num > 0
        return self.__lines[num] if num in self.__lines else 0

    __getitem__ = hits

    @property
    def lines(self) -> Iterator[FileLine]:
        """
        Returns an iterator over the set of all lines within this file
        that were covered.
        """
        for num in self.__lines:
            yield FileLine(self.__filename, num)

    def to_dict(self) -> dict:
        return copy(self.__lines)


class ProjectLineCoverage(object):
    """
    Provides complete line coverage information for all files and across all
    tests within a given project.
    """
    T = Dict[str, FileLineCoverage.T]

    @staticmethod
    def from_dict(test: TestCase,
                  d: dict,
                  ) -> 'ProjectLineCoverage':
        cov: T = {}
        outcome = TestOutcome.from_dict(d['outcome'])
        for (fn, fcov) in d['coverage'].items():
            cov[fn] = FileLineCoverage(fn, fcov)
        return ProjectLineCoverage(test, outcome, cov)

    def __init__(self,
                 test: TestCase,
                 outcome: TestOutcome,
                 files: Dict[str, FileLineCoverage]) -> None:
        self.__test = test
        self.__outcome = outcome
        self.__files = files

    def covers(self, line: FileLine) -> bool:
        """
        Returns `True` if given line was covered, or `False` if not.
        """
        if not line.filename in self.__files:
            return False

        f = self[line.filename]
        return f.was_hit(line.num)

    @property
    def test(self) -> TestCase:
        """
        The test case used to generate this coverage.
        """<commit_msg>coverage: removed Python 3.6 code<commit_after>import yaml
from copy import copy
from typing import Dict, List, Set, Iterator, Any
from bugzoo.testing import TestCase, TestSuite, TestOutcome


class FileLine(object):
    """
    Used to represent an one-indexed line within a specific file.
    """
    @staticmethod
    def compactify(d: Dict['FileLine', Any]) -> Dict[str, Dict[int, Any]]:
        """
        Converts a dictionary that is indexed by FileLine objects into a
        nested dictionary structures that maps from file names to a dictionary
        of line numbers.
        """
        assert isinstance(d, dict)
        assert all(isinstance(x, FileLine) for x in d)

        out = {}
        for (line, val) in d.items():
            if not line.filename in out:
                out[line.filename] = {}
            out[line.filename][line.num] = val
        return out

    @staticmethod
    def decompactify(d: Dict[str, Dict[int, Any]]) -> 'Dict[FileLine, Any]':
        lines = {}
        for fn in d:
            for num in d[fn]:
                lines[FileLine(fn, num)] = d[fn][num]
        return lines

    def __init__(self, fn: str, num: int) -> None:
        self.__fn = fn
        self.__num = num

    def __hash__(self) -> int:
        return hash((self.__fn, self.__num))

    def __eq__(self, other) -> bool:
        return  isinstance(other, FileLine) and \
                self.filename == other.filename and \
                self.num == other.num

    @property
    def filename(self) -> str:
        """
        The name of the file to which this line belongs.
        """
        return self.__fn

    fn = filename

    @property
    def num(self) -> int:
        """
        The one-indexed number of this line.
        """
        return self.__num

    def __str__(self) -> str:
        return "{}:{}".format(self.filename, self.num)

    __repr__ = __str__


class FileLineCoverage(object):
    """
    Provides line-level coverage information for a given file.
    """
    T = Dict[int, int]

    def __init__(self, filename: str, lines: T) -> None:
        self.__filename = filename
        self.__lines = \
            {num: hits for (num, hits) in lines.items() if hits > 0}

#    @property
#    def lines(self) -> List[int]:
#        """
#        A list of the one-indexed numbers of the lines that are included in
#        this report.
#        """
#        return list(self.__lines.keys())

    def was_hit(self, num: int) -> bool:
        """
        Determines whether a line with a given number was executed at least
        once during the execution(s).
        """
        return self.hits(num) > 0

    def hits(self, num: int) -> int:
        """
        Returns the number of times that a line with a given number was
        executed.
        """
        assert num > 0
        return self.__lines[num] if num in self.__lines else 0

    __getitem__ = hits

    @property
    def lines(self) -> Iterator[FileLine]:
        """
        Returns an iterator over the set of all lines within this file
        that were covered.
        """
        for num in self.__lines:
            yield FileLine(self.__filename, num)

    def to_dict(self) -> dict:
        return copy(self.__lines)


class ProjectLineCoverage(object):
    """
    Provides complete line coverage information for all files and across all
    tests within a given project.
    """
    T = Dict[str, FileLineCoverage.T]

    @staticmethod
    def from_dict(test: TestCase,
                  d: dict,
                  ) -> 'ProjectLineCoverage':
        cov = {}
        outcome = TestOutcome.from_dict(d['outcome'])
        for (fn, fcov) in d['coverage'].items():
            cov[fn] = FileLineCoverage(fn, fcov)
        return ProjectLineCoverage(test, outcome, cov)

    def __init__(self,
                 test: TestCase,
                 outcome: TestOutcome,
                 files: Dict[str, FileLineCoverage]) -> None:
        self.__test = test
        self.__outcome = outcome
        self.__files = files

    def covers(self, line: FileLine) -> bool:
        """
        Returns `True` if given line was covered, or `False` if not.
        """
        if not line.filename in self.__files:
            return False

        f = self[line.filename]
        return f.was_hit(line.num)

    @property
    def test(self) -> TestCase:
        """
        The test case used to generate this coverage.
        """
{'fraction_non_alphanumeric': 0.10577139007410734, 'fraction_numerical': 0.0011228385358185494, 'mean_word_length': 1.9031942633637549, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 45, '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': '319604', 'n_tokens_mistral': 2599, 'n_tokens_neox': 2517, 'n_words': 848}