<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = 'maxwu'

try:
    import xml.etree.cElementTree as ET
except ImportError:
    import xml.etree.ElementTree as ET
import json
from echarts import Echart, Legend, Bar, Axis
from me.maxwu.cistat.logger import Logger

logger = Logger(name=__name__).get_logger()


class Xunitrpt(object):
    """ The XUnit Test Report Class.
    Which represents an XML file as a test report in XUnit format.
    The XSD refers to below document:
    https://github.com/apache/maven-surefire/blob/master/maven-surefire-plugin/src/site/resources/xsd/surefire-test-report.xsd
    __add__() is overridden to combine two reports.
    __getitem__() and __setitem__() are overridden to operate case dict counters.
    However, __delitem__, __set/get/delslice__ are not implemented. So it is not a full overriding for [] operator.
    """
    DEFAULT_DICT = {'pass': 0, 'fail': 0, 'skip': 0, 'sum': 0, 'rate': 0, 'time': 0.0}

    def __init__(self, xunit=None):
        self.case_dict = {}
        if xunit:
            self.accumulate_xunit_str(xunit)

    def get_cases(self):
        return self.case_dict.copy()

    def get_case(self, tcname=None):
        return self.case_dict.get(tcname, dict())

    def __getitem__(self, item):
        return self.get_case(item)

    def __setitem__(self, key, value):
        return self.case_dict.__setitem__(key, value)

    def get_cases_in_rate(self, reverse=False):
        return sorted(self.case_dict.items(), lambda x, y: cmp(x[1]['rate'], y[1]['rate']), reverse=reverse)

    def __str__(self, indent=2, *args, **kwargs):
        return json.dumps(self.case_dict, indent=indent, *args, **kwargs) if self.case_dict else ''

    def dump(self):
        return self.__str__(indent=2)

    def __add__(self, other):
        z = Xunitrpt()
        z.case_dict = self.case_dict.copy()

        for k, v in other.get_cases().items():
            if k not in self.case_dict:
                z.case_dict[k] = Xunitrpt.DEFAULT_DICT.copy()
                continue
            for sk in ['sum', 'pass', 'fail', 'skip', 'time']:
                z.case_dict[k][sk] += v.get(sk, 0)
            z.cal_rate(k)

        return z

    def __eq__(self, other):
        if not self and not other:
            return True
        return self.get_cases() == other.get_cases()
<commit_msg>Refactor Xunit type more pythonic<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = 'maxwu'

try:
    import xml.etree.cElementTree as ET
except ImportError:
    import xml.etree.ElementTree as ET
import json
from echarts import Echart, Legend, Bar, Axis
from me.maxwu.cistat.logger import Logger

logger = Logger(name=__name__).get_logger()


class Xunitrpt(object):
    """ The XUnit Test Report Class.
    Which represents an XML file as a test report in XUnit format.
    The XSD refers to below document:
    https://github.com/apache/maven-surefire/blob/master/maven-surefire-plugin/src/site/resources/xsd/surefire-test-report.xsd
    __add__() is overridden to combine two reports.
    __getitem__() and __setitem__() are overridden to operate case dict counters.
    However, __delitem__, __set/get/delslice__ are not implemented. So it is not a full overriding for [] operator.
    """
    DEFAULT_DICT = {'pass': 0, 'fail': 0, 'skip': 0, 'sum': 0, 'rate': 0, 'time': 0.0}

    def __init__(self, xunit=None):
        self.case_dict = dict()
        if xunit:
            self.accumulate_xunit_str(xunit)

    def get_cases(self):
        return self.case_dict.copy()

    def get_case(self, tcname=None):
        return self.case_dict.get(tcname, dict())

    def __getitem__(self, item):
        return self.get_case(item)

    def __setitem__(self, key, value):
        return self.case_dict.__setitem__(key, value)

    def get_cases_in_rate(self, reverse=False):
        return sorted(self.case_dict.items(), lambda x, y: cmp(x[1]['rate'], y[1]['rate']), reverse=reverse)

    def __str__(self, indent=2, *args, **kwargs):
        return json.dumps(self.case_dict, indent=indent, *args, **kwargs) if self.case_dict else ''

    def dump(self):
        return self.__str__(indent=2)

    def __add__(self, other):
        z = Xunitrpt()
        z.case_dict = self.case_dict.copy()
        z.extend(other)
        return z

    def __iadd__(self, other):
        return self.extend(other)

    def extend(self, other):
        for k, v in other:
            if k not in self.keys():
                self[k] = Xunitrpt.DEFAULT_DICT.copy()
                continue
            for sk in ['sum', 'pass', 'fail', 'skip', 'time']:
                self[k][sk] += v.get(sk, 0)
            self.cal_rate(k)
        return self

    def __iter__(self):
        return self.case_dict.iteritems()

    def keys(self):
        return self.case_dict.keys()

    def __eq__(self, other):
        if not self and not other:
            return True
        return self.get_cases() == other.get_cases()

{'fraction_non_alphanumeric': 0.124724283136154, 'fraction_numerical': 0.005213555243633447, 'mean_word_length': 2.7335329341317367, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, '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': '1356840', 'n_tokens_mistral': 1678, 'n_tokens_neox': 1604, 'n_words': 440}