-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.py
More file actions
32 lines (27 loc) · 905 Bytes
/
helpers.py
File metadata and controls
32 lines (27 loc) · 905 Bytes
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 dateutil.parser import parse
from dateutil.parser import ParserError
import re
def infer_data_type(value):
if isinstance(value, str):
# Check if the string is a number first
if re.match(r'^-?\d+(\.\d+)?$', value):
return 'float' if '.' in value else 'integer'
# Try to parse as date
try:
# Add a check for reasonable year range
date = parse(value, fuzzy=False)
if 1000 <= date.year <= 9999:
return 'date'
except (ParserError, OverflowError, ValueError):
pass
data_type = type(value).__name__
type_mapping = {
'bool': 'boolean',
'int': 'integer',
'float': 'float',
'str': 'string',
'list': 'array',
'dict': 'object',
'NoneType': 'null'
}
return type_mapping.get(data_type, data_type)