File: boolean.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 (63 lines) | stat: -rw-r--r-- 1,983 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from decimal import Decimal

from agate.data_types.base import DEFAULT_NULL_VALUES, DataType
from agate.exceptions import CastError

#: Default values which will be automatically cast to :code:`True`.
DEFAULT_TRUE_VALUES = ('yes', 'y', 'true', 't', '1')

#: Default values which will be automatically cast to :code:`False`.
DEFAULT_FALSE_VALUES = ('no', 'n', 'false', 'f', '0')


class Boolean(DataType):
    """
    Data representing true and false.

    Note that by default numerical `1` and `0` are considered valid boolean
    values, but other numbers are not.

    :param true_values: A sequence of values which should be cast to
        :code:`True` when encountered with this type.
    :param false_values: A sequence of values which should be cast to
        :code:`False` when encountered with this type.
    """
    def __init__(self, true_values=DEFAULT_TRUE_VALUES, false_values=DEFAULT_FALSE_VALUES,
                 null_values=DEFAULT_NULL_VALUES):
        super().__init__(null_values=null_values)

        self.true_values = true_values
        self.false_values = false_values

    def cast(self, d):
        """
        Cast a single value to :class:`bool`.

        :param d: A value to cast.
        :returns: :class:`bool` or :code:`None`.
        """
        if d is None:
            return d
        if type(d) is bool and type(d) is not int:
            return d
        if type(d) is int or isinstance(d, Decimal):
            if d == 1:
                return True
            if d == 0:
                return False
        if isinstance(d, str):
            d = d.replace(',', '').strip()

            d_lower = d.lower()

            if d_lower in self.null_values:
                return None
            if d_lower in self.true_values:
                return True
            if d_lower in self.false_values:
                return False

        raise CastError('Can not convert value %s to bool.' % d)

    def jsonify(self, d):
        return d