<commit_before>from numbers import Number
from time import time
from collections import defaultdict
from .base import Metric


class _Accumulator(object):

    def __init__(self):
        self._value = 0
        self._count = 0

    @property
    def value(self):
        if isinstance(self._value, Number):
            if self._count != 0:
                return self._value / self._count
            else:
                return 0
        else:
            return self._value

    @value.setter
    def value(self, value):
        if isinstance(value, Number) and isinstance(self._value, Number):
            self._value += value
            self._count += 1
        else:
            self._value = value
            count = 1

    @property
    def count(self):
        return self._count


def count_summarise(metrics, window=1):
    accumulator_dict = defaultdict(lambda: defaultdict(_Accumulator))

    for metric in metrics:
        if metric is not None:
            name = metric.name
            metadata = tuple((k, v) for k, v in metric.values.iteritems()
                             if k in metric.metadata)

            for field, value in metric.values.iteritems():
                if field in metric.fields:
                    accumulator_dict[(name, metadata)][field].value = value

        for (name, metadata), accumulators in accumulator_dict.iteritems():
            fields = [
                key for key, accumulator in accumulators.iteritems()
                if accumulator.count >= window
            ]

            if len(fields) == 0:
                continue<commit_msg>USe six to fix py3 issues<commit_after>import six
from numbers import Number
from time import time
from collections import defaultdict
from .base import Metric


class _Accumulator(object):

    def __init__(self):
        self._value = 0
        self._count = 0

    @property
    def value(self):
        if isinstance(self._value, Number):
            if self._count != 0:
                return self._value / self._count
            else:
                return 0
        else:
            return self._value

    @value.setter
    def value(self, value):
        if isinstance(value, Number) and isinstance(self._value, Number):
            self._value += value
            self._count += 1
        else:
            self._value = value
            count = 1

    @property
    def count(self):
        return self._count


def count_summarise(metrics, window=1):
    accumulator_dict = defaultdict(lambda: defaultdict(_Accumulator))

    for metric in metrics:
        if metric is not None:
            name = metric.name
            metadata = tuple((k, v) for k, v in six.iteritems(metric.values)
                             if k in metric.metadata)

            for field, value in six.iteritems(metric.values):
                if field in metric.fields:
                    accumulator_dict[(name, metadata)][field].value = value

        for (name, metadata), accumulators in six.iteritems(accumulator_dict):
            fields = [
                key for key, accumulator in six.iteritems(accumulators)
                if accumulator.count >= window
            ]

            if len(fields) == 0:
                continue
{'fraction_non_alphanumeric': 0.07970342910101946, 'fraction_numerical': 0.005251776336113685, 'mean_word_length': 1.7255892255892256, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '6387382', 'n_tokens_mistral': 861, 'n_tokens_neox': 806, 'n_words': 260}