File: text.py

package info (click to toggle)
python-agate 1.13.0-1~exp1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 2,008 kB
  • sloc: python: 8,578; makefile: 126
file content (32 lines) | stat: -rw-r--r-- 852 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
from agate.data_types.base import DataType


class Text(DataType):
    """
    Data representing text.

    :param cast_nulls:
        If :code:`True`, values in :data:`.DEFAULT_NULL_VALUES` will be
        converted to `None`. Disable to retain them as strings.
    """
    def __init__(self, cast_nulls=True, **kwargs):
        super().__init__(**kwargs)

        self.cast_nulls = cast_nulls

    def cast(self, d):
        """
        Cast a single value to :func:`unicode` (:func:`str` in Python 3).

        :param d:
            A value to cast.
        :returns:
            :func:`unicode` (:func:`str` in Python 3) or :code:`None`
        """
        if d is None:
            return d
        if isinstance(d, str):
            if self.cast_nulls and d.strip().lower() in self.null_values:
                return None

        return str(d)