| 1 |
from __future__ import absolute_import, print_function, division |
| 2 |
|
| 3 |
|
| 4 |
import datetime |
| 5 |
from petl.compat import long |
| 6 |
|
| 7 |
|
| 8 |
def datetimeparser(fmt, strict=True): |
| 9 |
"""Return a function to parse strings as :class:`datetime.datetime` objects |
| 10 |
using a given format. E.g.:: |
| 11 |
|
| 12 |
>>> from petl import datetimeparser |
| 13 |
>>> isodatetime = datetimeparser('%Y-%m-%dT%H:%M:%S') |
| 14 |
>>> isodatetime('2002-12-25T00:00:00') |
| 15 |
datetime.datetime(2002, 12, 25, 0, 0) |
| 16 |
>>> try: |
| 17 |
... isodatetime('2002-12-25T00:00:99') |
| 18 |
... except ValueError as e: |
| 19 |
... print(e) |
| 20 |
... |
| 21 |
unconverted data remains: 9 |
| 22 |
|
| 23 |
If ``strict=False`` then if an error occurs when parsing, the original |
| 24 |
value will be returned as-is, and no error will be raised. |
| 25 |
|
| 26 |
""" |
| 27 |
|
| 28 |
def parser(value): |
| 29 |
try: |
| 30 |
return datetime.datetime.strptime(value.strip(), fmt) |
| 31 |
except Exception as e: |
| 32 |
if strict: |
| 33 |
raise e |
| 34 |
else: |
| 35 |
return value |
| 36 |
return parser |
| 37 |
|
| 38 |
|
| 39 |
def dateparser(fmt, strict=True): |
| 40 |
"""Return a function to parse strings as :class:`datetime.date` objects |
| 41 |
using a given format. E.g.:: |
| 42 |
|
| 43 |
>>> from petl import dateparser |
| 44 |
>>> isodate = dateparser('%Y-%m-%d') |
| 45 |
>>> isodate('2002-12-25') |
| 46 |
datetime.date(2002, 12, 25) |
| 47 |
>>> try: |
| 48 |
... isodate('2002-02-30') |
| 49 |
... except ValueError as e: |
| 50 |
... print('day is out of range for month') |
| 51 |
... |
| 52 |
day is out of range for month |
| 53 |
|
| 54 |
If ``strict=False`` then if an error occurs when parsing, the original |
| 55 |
value will be returned as-is, and no error will be raised. |
| 56 |
|
| 57 |
""" |
| 58 |
|
| 59 |
def parser(value): |
| 60 |
try: |
| 61 |
return datetime.datetime.strptime(value.strip(), fmt).date() |
| 62 |
except Exception as e: |
| 63 |
if strict: |
| 64 |
raise e |
| 65 |
else: |
| 66 |
return value |
| 67 |
return parser |
| 68 |
|
| 69 |
|
| 70 |
def timeparser(fmt, strict=True): |
| 71 |
"""Return a function to parse strings as :class:`datetime.time` objects |
| 72 |
using a given format. E.g.:: |
| 73 |
|
| 74 |
>>> from petl import timeparser |
| 75 |
>>> isotime = timeparser('%H:%M:%S') |
| 76 |
>>> isotime('00:00:00') |
| 77 |
datetime.time(0, 0) |
| 78 |
>>> isotime('13:00:00') |
| 79 |
datetime.time(13, 0) |
| 80 |
>>> try: |
| 81 |
... isotime('12:00:99') |
| 82 |
... except ValueError as e: |
| 83 |
... print(e) |
| 84 |
... |
| 85 |
unconverted data remains: 9 |
| 86 |
>>> try: |
| 87 |
... isotime('25:00:00') |
| 88 |
... except ValueError as e: |
| 89 |
... print(e) |
| 90 |
... |
| 91 |
time data '25:00:00' does not match format '%H:%M:%S' |
| 92 |
|
| 93 |
If ``strict=False`` then if an error occurs when parsing, the original |
| 94 |
value will be returned as-is, and no error will be raised. |
| 95 |
|
| 96 |
""" |
| 97 |
|
| 98 |
def parser(value): |
| 99 |
try: |
| 100 |
return datetime.datetime.strptime(value.strip(), fmt).time() |
| 101 |
except Exception as e: |
| 102 |
if strict: |
| 103 |
raise e |
| 104 |
else: |
| 105 |
return value |
| 106 |
return parser |
| 107 |
|
| 108 |
|
| 109 |
def boolparser(true_strings=('true', 't', 'yes', 'y', '1'), |
| 110 |
false_strings=('false', 'f', 'no', 'n', '0'), |
| 111 |
case_sensitive=False, |
| 112 |
strict=True): |
| 113 |
"""Return a function to parse strings as :class:`bool` objects using a |
| 114 |
given set of string representations for `True` and `False`. E.g.:: |
| 115 |
|
| 116 |
>>> from petl import boolparser |
| 117 |
>>> mybool = boolparser(true_strings=['yes', 'y'], false_strings=['no', 'n']) |
| 118 |
>>> mybool('y') |
| 119 |
True |
| 120 |
>>> mybool('yes') |
| 121 |
True |
| 122 |
>>> mybool('Y') |
| 123 |
True |
| 124 |
>>> mybool('No') |
| 125 |
False |
| 126 |
>>> try: |
| 127 |
... mybool('foo') |
| 128 |
... except ValueError as e: |
| 129 |
... print(e) |
| 130 |
... |
| 131 |
value is not one of recognised boolean strings: 'foo' |
| 132 |
>>> try: |
| 133 |
... mybool('True') |
| 134 |
... except ValueError as e: |
| 135 |
... print(e) |
| 136 |
... |
| 137 |
value is not one of recognised boolean strings: 'true' |
| 138 |
|
| 139 |
If ``strict=False`` then if an error occurs when parsing, the original |
| 140 |
value will be returned as-is, and no error will be raised. |
| 141 |
|
| 142 |
""" |
| 143 |
|
| 144 |
if not case_sensitive: |
| 145 |
true_strings = [s.lower() for s in true_strings] |
| 146 |
false_strings = [s.lower() for s in false_strings] |
| 147 |
|
| 148 |
def parser(value): |
| 149 |
value = value.strip() |
| 150 |
if not case_sensitive: |
| 151 |
value = value.lower() |
| 152 |
if value in true_strings: |
| 153 |
return True |
| 154 |
elif value in false_strings: |
| 155 |
return False |
| 156 |
elif strict: |
| 157 |
raise ValueError('value is not one of recognised boolean strings: ' |
| 158 |
'%r' % value) |
| 159 |
else: |
| 160 |
return value |
| 161 |
|
| 162 |
return parser |
| 163 |
|
| 164 |
|
| 165 |
def numparser(strict=False): |
| 166 |
"""Return a function that will attempt to parse the value as a number, |
| 167 |
trying :func:`int`, :func:`long`, :func:`float` and :func:`complex` in |
| 168 |
that order. If all fail, return the value as-is, unless ``strict=True``, |
| 169 |
in which case raise the underlying exception. |
| 170 |
|
| 171 |
""" |
| 172 |
|
| 173 |
def f(v): |
| 174 |
try: |
| 175 |
return int(v) |
| 176 |
except (ValueError, TypeError): |
| 177 |
pass |
| 178 |
try: |
| 179 |
return long(v) |
| 180 |
except (ValueError, TypeError): |
| 181 |
pass |
| 182 |
try: |
| 183 |
return float(v) |
| 184 |
except (ValueError, TypeError): |
| 185 |
pass |
| 186 |
try: |
| 187 |
return complex(v) |
| 188 |
except (ValueError, TypeError) as e: |
| 189 |
if strict: |
| 190 |
raise e |
| 191 |
return v |
| 192 |
|
| 193 |
return f |