forked from kz26/PyExcelerate
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataTypes.py
More file actions
63 lines (59 loc) · 1.95 KB
/
DataTypes.py
File metadata and controls
63 lines (59 loc) · 1.95 KB
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 datetime import datetime, date, time
import decimal
import six
try:
import numpy as np
HAS_NUMPY = True
except:
HAS_NUMPY = False
class DataTypes(object):
BOOLEAN = 0
DATE = 1
ERROR = 2
INLINE_STRING = 3
NUMBER = 4
SHARED_STRING = 5
STRING = 6
FORMULA = 7
EXCEL_BASE_DATE = datetime(1900, 1, 1, 0, 0, 0)
_numberTypes = six.integer_types + (float, complex, decimal.Decimal)
@staticmethod
def get_type(value):
# Using value.__class__ over isinstance for speed
if value.__class__ in six.string_types:
if len(value) > 0 and value[0] == '=':
return DataTypes.FORMULA
else:
return DataTypes.INLINE_STRING
# not using in (int, float, long, complex) for speed
elif value.__class__ == bool:
return DataTypes.BOOLEAN
elif value.__class__ in DataTypes._numberTypes:
return DataTypes.NUMBER
# fall back to the slower isinstance
elif isinstance(value, six.string_types):
if len(value) > 0 and value[0] == '=':
return DataTypes.FORMULA
else:
return DataTypes.INLINE_STRING
elif isinstance(value, bool):
return DataTypes.BOOLEAN
elif isinstance(value, DataTypes._numberTypes):
return DataTypes.NUMBER
elif HAS_NUMPY and isinstance(value, (np.floating, np.integer, np.complexfloating, np.unsignedinteger)):
return DataTypes.NUMBER
elif isinstance(value, (datetime, date, time)):
return DataTypes.DATE
else:
return DataTypes.ERROR
@staticmethod
def to_excel_date(d):
if isinstance(d, datetime):
delta = d - DataTypes.EXCEL_BASE_DATE
excel_date = delta.days + (float(delta.seconds) + float(delta.microseconds) / 1E6) / (60 * 60 * 24) + 1
return excel_date + (excel_date > 59)
elif isinstance(d, date):
# this is why python sucks >.<
return DataTypes.to_excel_date(datetime(*(d.timetuple()[:6])))
elif isinstance(d, time):
return DataTypes.to_excel_date(datetime(*(DataTypes.EXCEL_BASE_DATE.timetuple()[:3]), hour=d.hour, minute=d.minute, second=d.second, microsecond=d.microsecond)) - 1