File: first.py

package info (click to toggle)
python-agate 1.9.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,996 kB
  • sloc: python: 8,512; makefile: 126
file content (39 lines) | stat: -rw-r--r-- 1,222 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
from agate.aggregations.base import Aggregation


class First(Aggregation):
    """
    Returns the first value that passes a test.

    If the test is omitted, the aggregation will return the first value in the column.

    If no values pass the test, the aggregation will raise an exception.

    :param column_name:
        The name of the column to check.
    :param test:
        A function that takes a value and returns `True` or `False`. Test may be
        omitted when checking :class:`.Boolean` data.
    """
    def __init__(self, column_name, test=None):
        self._column_name = column_name
        self._test = test

    def get_aggregate_data_type(self, table):
        return table.columns[self._column_name].data_type

    def validate(self, table):
        column = table.columns[self._column_name]
        data = column.values()

        if self._test is not None and len([d for d in data if self._test(d)]) == 0:
            raise ValueError('No values pass the given test.')

    def run(self, table):
        column = table.columns[self._column_name]
        data = column.values()

        if self._test is None:
            return data[0]

        return next(d for d in data if self._test(d))