Clean up Python 2 compatibility code. (#5161)
This commit is contained in:
parent
61286c6e8f
commit
6848d0426f
@ -4,8 +4,6 @@
|
|||||||
Contributors: https://github.com/dmlc/xgboost/blob/master/CONTRIBUTORS.md
|
Contributors: https://github.com/dmlc/xgboost/blob/master/CONTRIBUTORS.md
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from __future__ import absolute_import
|
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
|
||||||
from .core import DMatrix, Booster
|
from .core import DMatrix, Booster
|
||||||
|
|||||||
@ -1,7 +1,6 @@
|
|||||||
# coding: utf-8
|
# coding: utf-8
|
||||||
# pylint: disable=invalid-name, too-many-statements
|
# pylint: disable=invalid-name, too-many-statements
|
||||||
"""Training Library containing training routines."""
|
"""Training Library containing training routines."""
|
||||||
from __future__ import absolute_import
|
|
||||||
|
|
||||||
from . import rabit
|
from . import rabit
|
||||||
from .core import EarlyStopException
|
from .core import EarlyStopException
|
||||||
|
|||||||
@ -1,30 +1,21 @@
|
|||||||
# coding: utf-8
|
# coding: utf-8
|
||||||
# pylint: disable= invalid-name, unused-import
|
# pylint: disable= invalid-name, unused-import
|
||||||
"""For compatibility and optional dependencies."""
|
"""For compatibility and optional dependencies."""
|
||||||
|
|
||||||
from __future__ import absolute_import
|
|
||||||
|
|
||||||
import abc
|
import abc
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
from pathlib import PurePath
|
from pathlib import PurePath
|
||||||
|
|
||||||
PY3 = (sys.version_info[0] == 3)
|
assert (sys.version_info[0] == 3), 'Python 2 is no longer supported.'
|
||||||
|
|
||||||
if PY3:
|
# pylint: disable=invalid-name, redefined-builtin
|
||||||
# pylint: disable=invalid-name, redefined-builtin
|
STRING_TYPES = (str,)
|
||||||
STRING_TYPES = (str,)
|
|
||||||
|
|
||||||
def py_str(x):
|
|
||||||
"""convert c string back to python string"""
|
|
||||||
return x.decode('utf-8')
|
|
||||||
else:
|
|
||||||
STRING_TYPES = (basestring,) # pylint: disable=undefined-variable
|
|
||||||
|
|
||||||
def py_str(x):
|
def py_str(x):
|
||||||
"""convert c string back to python string"""
|
"""convert c string back to python string"""
|
||||||
return x
|
return x.decode('utf-8')
|
||||||
|
|
||||||
|
|
||||||
###############################################################################
|
###############################################################################
|
||||||
@ -87,12 +78,6 @@ else:
|
|||||||
# END NUMPY PATHLIB ATTRIBUTION
|
# END NUMPY PATHLIB ATTRIBUTION
|
||||||
###############################################################################
|
###############################################################################
|
||||||
|
|
||||||
# pickle
|
|
||||||
try:
|
|
||||||
import cPickle as pickle # noqa
|
|
||||||
except ImportError:
|
|
||||||
import pickle # noqa
|
|
||||||
|
|
||||||
# pandas
|
# pandas
|
||||||
try:
|
try:
|
||||||
from pandas import DataFrame, Series
|
from pandas import DataFrame, Series
|
||||||
|
|||||||
@ -18,7 +18,7 @@ import numpy as np
|
|||||||
import scipy.sparse
|
import scipy.sparse
|
||||||
|
|
||||||
from .compat import (
|
from .compat import (
|
||||||
STRING_TYPES, PY3, DataFrame, MultiIndex, py_str,
|
STRING_TYPES, DataFrame, MultiIndex, py_str,
|
||||||
PANDAS_INSTALLED, DataTable,
|
PANDAS_INSTALLED, DataTable,
|
||||||
CUDF_INSTALLED, CUDF_DataFrame, CUDF_Series, CUDF_MultiIndex,
|
CUDF_INSTALLED, CUDF_DataFrame, CUDF_Series, CUDF_MultiIndex,
|
||||||
os_fspath, os_PathLike)
|
os_fspath, os_PathLike)
|
||||||
@ -70,11 +70,7 @@ def from_pystr_to_cstr(data):
|
|||||||
if not isinstance(data, list):
|
if not isinstance(data, list):
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
pointers = (ctypes.c_char_p * len(data))()
|
pointers = (ctypes.c_char_p * len(data))()
|
||||||
if PY3:
|
data = [bytes(d, 'utf-8') for d in data]
|
||||||
data = [bytes(d, 'utf-8') for d in data]
|
|
||||||
else:
|
|
||||||
data = [d.encode('utf-8') if isinstance(d, unicode) else d # pylint: disable=undefined-variable
|
|
||||||
for d in data]
|
|
||||||
pointers[:] = data
|
pointers[:] = data
|
||||||
return pointers
|
return pointers
|
||||||
|
|
||||||
@ -89,21 +85,12 @@ def from_cstr_to_pystr(data, length):
|
|||||||
length : ctypes pointer
|
length : ctypes pointer
|
||||||
pointer to length of data
|
pointer to length of data
|
||||||
"""
|
"""
|
||||||
if PY3:
|
res = []
|
||||||
res = []
|
for i in range(length.value):
|
||||||
for i in range(length.value):
|
try:
|
||||||
try:
|
res.append(str(data[i].decode('ascii')))
|
||||||
res.append(str(data[i].decode('ascii')))
|
except UnicodeDecodeError:
|
||||||
except UnicodeDecodeError:
|
res.append(str(data[i].decode('utf-8')))
|
||||||
res.append(str(data[i].decode('utf-8')))
|
|
||||||
else:
|
|
||||||
res = []
|
|
||||||
for i in range(length.value):
|
|
||||||
try:
|
|
||||||
res.append(str(data[i].decode('ascii')))
|
|
||||||
except UnicodeDecodeError:
|
|
||||||
# pylint: disable=undefined-variable
|
|
||||||
res.append(unicode(data[i].decode('utf-8')))
|
|
||||||
return res
|
return res
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -1,14 +1,12 @@
|
|||||||
# coding: utf-8
|
# coding: utf-8
|
||||||
# pylint: disable= invalid-name
|
# pylint: disable= invalid-name
|
||||||
|
|
||||||
"""Distributed XGBoost Rabit related API."""
|
"""Distributed XGBoost Rabit related API."""
|
||||||
from __future__ import absolute_import
|
|
||||||
import sys
|
import sys
|
||||||
import ctypes
|
import ctypes
|
||||||
|
import pickle
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
|
||||||
from .core import _LIB, c_str, STRING_TYPES
|
from .core import _LIB, c_str, STRING_TYPES
|
||||||
from .compat import pickle
|
|
||||||
|
|
||||||
|
|
||||||
def _init_rabit():
|
def _init_rabit():
|
||||||
|
|||||||
@ -5,8 +5,6 @@ which is a specialized version for xgboost tasks.
|
|||||||
|
|
||||||
# pylint: disable=invalid-name, missing-docstring, too-many-arguments, too-many-locals
|
# pylint: disable=invalid-name, missing-docstring, too-many-arguments, too-many-locals
|
||||||
# pylint: disable=too-many-branches, too-many-statements, too-many-instance-attributes
|
# pylint: disable=too-many-branches, too-many-statements, too-many-instance-attributes
|
||||||
from __future__ import absolute_import
|
|
||||||
|
|
||||||
import socket
|
import socket
|
||||||
import struct
|
import struct
|
||||||
import time
|
import time
|
||||||
@ -336,4 +334,4 @@ class RabitTracker(object):
|
|||||||
self.thread.join(100)
|
self.thread.join(100)
|
||||||
|
|
||||||
def alive(self):
|
def alive(self):
|
||||||
return self.thread.isAlive()
|
return self.thread.is_alive()
|
||||||
|
|||||||
@ -2,8 +2,6 @@
|
|||||||
# pylint: disable=too-many-locals, too-many-arguments, invalid-name
|
# pylint: disable=too-many-locals, too-many-arguments, invalid-name
|
||||||
# pylint: disable=too-many-branches, too-many-statements
|
# pylint: disable=too-many-branches, too-many-statements
|
||||||
"""Training Library containing training routines."""
|
"""Training Library containing training routines."""
|
||||||
from __future__ import absolute_import
|
|
||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
from .core import Booster, STRING_TYPES, XGBoostError, CallbackEnv, EarlyStopException
|
from .core import Booster, STRING_TYPES, XGBoostError, CallbackEnv, EarlyStopException
|
||||||
from .compat import (SKLEARN_INSTALLED, XGBStratifiedKFold)
|
from .compat import (SKLEARN_INSTALLED, XGBStratifiedKFold)
|
||||||
|
|||||||
@ -1,5 +1,3 @@
|
|||||||
from __future__ import print_function
|
|
||||||
|
|
||||||
import glob
|
import glob
|
||||||
import itertools as it
|
import itertools as it
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
|||||||
@ -1,5 +1,3 @@
|
|||||||
from __future__ import print_function
|
|
||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import testing as tm
|
import testing as tm
|
||||||
import unittest
|
import unittest
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user