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 40 41 42 43
|
from decimal import Decimal
from agate.aggregations.base import Aggregation
from agate.data_types import Number, Text
from agate.exceptions import DataTypeError
class MaxLength(Aggregation):
"""
Find the length of the longest string in a column.
Note: On Python 2.7 this function may miscalcuate the length of unicode
strings that contain "wide characters". For details see this StackOverflow
answer: https://stackoverflow.com/a/35462951
:param column_name:
The name of a column containing :class:`.Text` data.
"""
def __init__(self, column_name):
self._column_name = column_name
def get_aggregate_data_type(self, table):
return Number()
def validate(self, table):
column = table.columns[self._column_name]
if not isinstance(column.data_type, Text):
raise DataTypeError('MaxLength can only be applied to columns containing Text data.')
def run(self, table):
"""
:returns:
:class:`int`.
"""
column = table.columns[self._column_name]
lens = [len(d) for d in column.values_without_nulls()]
if not lens:
return Decimal('0')
return Decimal(max(lens))
|