<commit_before>import logging
from sqlagg.base import TableNotFoundException, ColumnNotFoundException
from corehq.apps.reports.sqlreport import SqlData

logger = logging.getLogger(__name__)


class IndicatorSetException(Exception):
    pass


class SqlIndicatorSet(SqlData):
    no_value = 0
    name = ''
    table_name = None

    def __init__(self, domain, user):
        self.domain = domain
        self.user = user

    @property
    def data(self):
        try:
            data = super(SqlIndicatorSet, self).data
        except (TableNotFoundException, ColumnNotFoundException) as e:
            logger.exception(e)
            return {}

        if self.keys and self.group_by:
            for key_group in self.keys:
                row_key = self._row_key(key_group)
                row = data.get(row_key, None)
                if not row:
                    row = dict(zip(self.group_by, key_group))

                data[row_key] = dict([(c.view.name, self._or_no_value(c.get_value(row))) for c in self.columns])
        elif self.group_by:
            for k, v in data.items():
                data[k] = dict([(c.view.name, self._or_no_value(c.get_value(v))) for c in self.columns])
        else:
            data = dict([(c.view.name, self._or_no_value(c.get_value(data))) for c in self.columns])

        return data

    def _row_key(self, key_group):
        if len(self.group_by) == 1:
            return key_group[0]
        elif len(self.group_by) > 1:
            return tuple(key_group)

    def _or_no_value(self, value):
        return value if value is not None else self.no_value<commit_msg>if keys supplied only return matching keys<commit_after>import logging
from sqlagg.base import TableNotFoundException, ColumnNotFoundException
from corehq.apps.reports.sqlreport import SqlData

logger = logging.getLogger(__name__)


class IndicatorSetException(Exception):
    pass


class SqlIndicatorSet(SqlData):
    no_value = 0
    name = ''
    table_name = None

    def __init__(self, domain, user):
        self.domain = domain
        self.user = user

    @property
    def data(self):
        try:
            data = super(SqlIndicatorSet, self).data
        except (TableNotFoundException, ColumnNotFoundException) as e:
            logger.exception(e)
            return {}

        ret = dict()
        if self.keys and self.group_by:
            for key_group in self.keys:
                row_key = self._row_key(key_group)
                row = data.get(row_key, None)
                if not row:
                    row = dict(zip(self.group_by, key_group))

                ret[row_key] = dict([(c.view.name, self._or_no_value(c.get_value(row))) for c in self.columns])
        elif self.group_by:
            for k, v in data.items():
                ret[k] = dict([(c.view.name, self._or_no_value(c.get_value(v))) for c in self.columns])
        else:
            ret = dict([(c.view.name, self._or_no_value(c.get_value(data))) for c in self.columns])

        return ret

    def _row_key(self, key_group):
        if len(self.group_by) == 1:
            return key_group[0]
        elif len(self.group_by) > 1:
            return tuple(key_group)

    def _or_no_value(self, value):
        return value if value is not None else self.no_value
{'fraction_non_alphanumeric': 0.0982360097323601, 'fraction_numerical': 0.0024330900243309003, 'mean_word_length': 2.2119140625, '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': '4032487', 'n_tokens_mistral': 1019, 'n_tokens_neox': 984, 'n_words': 247}