delays importing numpy and pandas to reduce the startup time
Signed-off-by: Shivam Dhar <[email protected]>
This commit is contained in:
+52
-37
@@ -40,37 +40,6 @@ INTEGER_TYPES = ()
|
|||||||
FLOAT_TYPES = (Decimal,)
|
FLOAT_TYPES = (Decimal,)
|
||||||
TIME_TYPES = (date, datetime)
|
TIME_TYPES = (date, datetime)
|
||||||
|
|
||||||
try:
|
|
||||||
import numpy as np
|
|
||||||
|
|
||||||
INTEGER_TYPES += (
|
|
||||||
np.int_,
|
|
||||||
np.intc,
|
|
||||||
np.int8,
|
|
||||||
np.int16,
|
|
||||||
np.int32,
|
|
||||||
np.int64,
|
|
||||||
np.uint8,
|
|
||||||
np.uint16,
|
|
||||||
np.uint32,
|
|
||||||
np.uint64,
|
|
||||||
)
|
|
||||||
FLOAT_TYPES += (
|
|
||||||
np.float_,
|
|
||||||
np.float16,
|
|
||||||
np.float32,
|
|
||||||
np.float64,
|
|
||||||
)
|
|
||||||
except ImportError:
|
|
||||||
np = None
|
|
||||||
|
|
||||||
try:
|
|
||||||
import pandas as pd
|
|
||||||
|
|
||||||
TIME_TYPES += (pd.Timestamp,)
|
|
||||||
except ImportError:
|
|
||||||
pd = None
|
|
||||||
|
|
||||||
|
|
||||||
class Serializer(object):
|
class Serializer(object):
|
||||||
mimetype = ""
|
mimetype = ""
|
||||||
@@ -99,9 +68,15 @@ class JSONSerializer(Serializer):
|
|||||||
mimetype = "application/json"
|
mimetype = "application/json"
|
||||||
|
|
||||||
def default(self, data):
|
def default(self, data):
|
||||||
if isinstance(data, TIME_TYPES) and getattr(pd, "NaT", None) is not data:
|
if isinstance(data, TIME_TYPES):
|
||||||
return data.isoformat()
|
# Little hack to avoid importing pandas but to not
|
||||||
elif isinstance(data, uuid.UUID):
|
# return 'NaT' string for pd.NaT as that's not a valid
|
||||||
|
# date.
|
||||||
|
formatted_data = data.isoformat()
|
||||||
|
if formatted_data != "NaT":
|
||||||
|
return formatted_data
|
||||||
|
|
||||||
|
if isinstance(data, uuid.UUID):
|
||||||
return str(data)
|
return str(data)
|
||||||
elif isinstance(data, FLOAT_TYPES):
|
elif isinstance(data, FLOAT_TYPES):
|
||||||
return float(data)
|
return float(data)
|
||||||
@@ -109,18 +84,58 @@ class JSONSerializer(Serializer):
|
|||||||
return int(data)
|
return int(data)
|
||||||
|
|
||||||
# Special cases for numpy and pandas types
|
# Special cases for numpy and pandas types
|
||||||
elif np:
|
# These are expensive to import so we try them last.
|
||||||
if isinstance(data, np.bool_):
|
try:
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
if isinstance(
|
||||||
|
data,
|
||||||
|
(
|
||||||
|
np.int_,
|
||||||
|
np.intc,
|
||||||
|
np.int8,
|
||||||
|
np.int16,
|
||||||
|
np.int32,
|
||||||
|
np.int64,
|
||||||
|
np.uint8,
|
||||||
|
np.uint16,
|
||||||
|
np.uint32,
|
||||||
|
np.uint64,
|
||||||
|
),
|
||||||
|
):
|
||||||
|
return int(data)
|
||||||
|
elif isinstance(
|
||||||
|
data,
|
||||||
|
(
|
||||||
|
np.float_,
|
||||||
|
np.float16,
|
||||||
|
np.float32,
|
||||||
|
np.float64,
|
||||||
|
),
|
||||||
|
):
|
||||||
|
return float(data)
|
||||||
|
elif isinstance(data, np.bool_):
|
||||||
return bool(data)
|
return bool(data)
|
||||||
elif isinstance(data, np.datetime64):
|
elif isinstance(data, np.datetime64):
|
||||||
return data.item().isoformat()
|
return data.item().isoformat()
|
||||||
elif isinstance(data, np.ndarray):
|
elif isinstance(data, np.ndarray):
|
||||||
return data.tolist()
|
return data.tolist()
|
||||||
if pd:
|
except ImportError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
try:
|
||||||
|
import pandas as pd
|
||||||
|
|
||||||
if isinstance(data, (pd.Series, pd.Categorical)):
|
if isinstance(data, (pd.Series, pd.Categorical)):
|
||||||
return data.tolist()
|
return data.tolist()
|
||||||
|
elif isinstance(data, pd.Timestamp) and data is not getattr(
|
||||||
|
pd, "NaT", None
|
||||||
|
):
|
||||||
|
return data.isoformat()
|
||||||
elif data is getattr(pd, "NA", None):
|
elif data is getattr(pd, "NA", None):
|
||||||
return None
|
return None
|
||||||
|
except ImportError:
|
||||||
|
pass
|
||||||
|
|
||||||
raise TypeError("Unable to serialize %r (type: %s)" % (data, type(data)))
|
raise TypeError("Unable to serialize %r (type: %s)" % (data, type(data)))
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user