"docs/advanced/vscode:/vscode.git/clone" did not exist on "a97e9d8cacadfddc3ff19790242e335499639168"
compat.py 2.19 KB
Newer Older
wxchan's avatar
wxchan committed
1
2
3
4
5
6
7
8
# coding: utf-8
# pylint: disable = C0103
"""Compatibility"""
from __future__ import absolute_import

import inspect
import sys

9
10
import numpy as np

wxchan's avatar
wxchan committed
11
12
13
14
15
16
is_py3 = (sys.version_info[0] == 3)

"""compatibility between python2 and python3"""
if is_py3:
    string_type = str
    numeric_types = (int, float, bool)
wxchan's avatar
wxchan committed
17
    integer_types = (int, )
wxchan's avatar
wxchan committed
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
    range_ = range

    def argc_(func):
        """return number of arguments of a function"""
        return len(inspect.signature(func).parameters)
else:
    string_type = basestring
    numeric_types = (int, long, float, bool)
    integer_types = (int, long)
    range_ = xrange

    def argc_(func):
        """return number of arguments of a function"""
        return len(inspect.getargspec(func).args)

"""json"""
try:
    import simplejson as json
except (ImportError, SyntaxError):
    # simplejson does not support Python 3.2, it throws a SyntaxError
    # because of u'...' Unicode literals.
    import json

41

42
43
44
45
46
47
48
49
50
def json_default_with_numpy(obj):
    if isinstance(obj, (np.integer, np.floating, np.bool_)):
        return obj.item()
    elif isinstance(obj, np.ndarray):
        return obj.tolist()
    else:
        return obj


wxchan's avatar
wxchan committed
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
"""pandas"""
try:
    from pandas import Series, DataFrame
except ImportError:
    class Series(object):
        pass

    class DataFrame(object):
        pass

"""sklearn"""
try:
    from sklearn.base import BaseEstimator
    from sklearn.base import RegressorMixin, ClassifierMixin
    from sklearn.preprocessing import LabelEncoder
    from sklearn.utils import deprecated
    try:
wxchan's avatar
wxchan committed
68
        from sklearn.model_selection import StratifiedKFold, GroupKFold
wxchan's avatar
wxchan committed
69
    except ImportError:
wxchan's avatar
wxchan committed
70
        from sklearn.cross_validation import StratifiedKFold, GroupKFold
wxchan's avatar
wxchan committed
71
72
73
74
75
76
77
    SKLEARN_INSTALLED = True
    LGBMModelBase = BaseEstimator
    LGBMRegressorBase = RegressorMixin
    LGBMClassifierBase = ClassifierMixin
    LGBMLabelEncoder = LabelEncoder
    LGBMDeprecated = deprecated
    LGBMStratifiedKFold = StratifiedKFold
wxchan's avatar
wxchan committed
78
    LGBMGroupKFold = GroupKFold
wxchan's avatar
wxchan committed
79
80
81
82
83
84
85
except ImportError:
    SKLEARN_INSTALLED = False
    LGBMModelBase = object
    LGBMClassifierBase = object
    LGBMRegressorBase = object
    LGBMLabelEncoder = None
    LGBMStratifiedKFold = None
wxchan's avatar
wxchan committed
86
    LGBMGroupKFold = None