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


class Abstract(object):

    def __init__(self, base_table, *args, **kwargs):
        self._table = base_table

    @property
    def name(self):
        return self._table.name

    @property
    def columns(self):
        return self._table.columns

    def add_value(self, row, column, value):
        return self._table.add_value(row, column, value)

    def __iter__(self):
        return iter(self._table)


class TableForwarder(Abstract):

    def __init__(self, base_table, proxy):
        self._proxy = proxy
        Abstract.__init__(self, base_table)

    def add_value(self, row, column, value):
        self._proxy.send('pyqp_cell_value', (self.name, row, column, value))


class TableFilterable(Abstract):
    """Table that filters columns and/or rows from output (it holds all data!)

    >>> from time import sleep
    >>> from operator import attrgetter
    >>> from pyqp.tables import Table
    >>> t = Table('foo', ['a', 'b'])
    >>> tf = TableFilterable(t, col_filter=lambda c: c == 'a')
    >>> tf.add_value(1, 'a', 11) #doctest: +ELLIPSIS<commit_msg>Refactored pyqp.tables.decorators.TableForwarder to be able to decide whether to forward data or not runtime<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-


class Abstract(object):

    def __init__(self, base_table, *args, **kwargs):
        self._table = base_table

    @property
    def name(self):
        return self._table.name

    @property
    def columns(self):
        return self._table.columns

    def add_value(self, row, column, value):
        return self._table.add_value(row, column, value)

    def __iter__(self):
        return iter(self._table)


class TableForwarder(Abstract):

    def __init__(self, base_table, allow_forwarding, proxy):
        self._proxy = proxy
        self._do_forward = allow_forwarding
        Abstract.__init__(self, base_table)

    def add_value(self, row, column, value):
        if self._do_forward(self.name):
            self._proxy.send('pyqp_cell_value', (self.name, row, column, value))
        else:
            Abstract.add_value(self, row, column, value)
        return self


class TableFilterable(Abstract):
    """Table that filters columns and/or rows from output (it holds all data!)

    >>> from time import sleep
    >>> from operator import attrgetter
    >>> from pyqp.tables import Table
    >>> t = Table('foo', ['a', 'b'])
    >>> tf = TableFilterable(t, col_filter=lambda c: c == 'a')
    >>> tf.add_value(1, 'a', 11) #doctest: +ELLIPSIS
{'fraction_non_alphanumeric': 0.12870514820592824, 'fraction_numerical': 0.0031201248049922, 'mean_word_length': 3.045741324921136, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 39, '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': '5370121', 'n_tokens_mistral': 800, 'n_tokens_neox': 773, 'n_words': 218}