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
|
from agate.exceptions import CastError
#: Default values which will be automatically cast to :code:`None`
DEFAULT_NULL_VALUES = ('', 'na', 'n/a', 'none', 'null', '.')
class DataType: # pragma: no cover
"""
Specifies how values should be parsed when creating a :class:`.Table`.
:param null_values: A sequence of values which should be cast to
:code:`None` when encountered by this data type.
"""
def __init__(self, null_values=DEFAULT_NULL_VALUES):
self.null_values = [v.lower() for v in null_values]
def test(self, d):
"""
Test, for purposes of type inference, if a value could possibly be
coerced to this data type.
This is really just a thin wrapper around :meth:`DataType.cast`.
"""
try:
self.cast(d)
except CastError:
return False
return True
def cast(self, d):
"""
Coerce a given string value into this column's data type.
"""
raise NotImplementedError
def csvify(self, d):
"""
Format a given native value for CSV serialization.
"""
if d is None:
return None
return str(d)
def jsonify(self, d):
"""
Format a given native value for JSON serialization.
"""
if d is None:
return None
return str(d)
|