Clean up Python 2 compatibility code. (#5161)

This commit is contained in:
Jiaming Yuan 2019-12-27 18:34:53 +08:00 committed by GitHub
parent 61286c6e8f
commit 6848d0426f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 16 additions and 57 deletions

View File

@ -4,8 +4,6 @@
Contributors: https://github.com/dmlc/xgboost/blob/master/CONTRIBUTORS.md
"""
from __future__ import absolute_import
import os
from .core import DMatrix, Booster

View File

@ -1,7 +1,6 @@
# coding: utf-8
# pylint: disable=invalid-name, too-many-statements
"""Training Library containing training routines."""
from __future__ import absolute_import
from . import rabit
from .core import EarlyStopException

View File

@ -1,30 +1,21 @@
# coding: utf-8
# pylint: disable= invalid-name, unused-import
"""For compatibility and optional dependencies."""
from __future__ import absolute_import
import abc
import os
import sys
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
STRING_TYPES = (str,)
# pylint: disable=invalid-name, redefined-builtin
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):
"""convert c string back to python string"""
return x
def py_str(x):
"""convert c string back to python string"""
return x.decode('utf-8')
###############################################################################
@ -87,12 +78,6 @@ else:
# END NUMPY PATHLIB ATTRIBUTION
###############################################################################
# pickle
try:
import cPickle as pickle # noqa
except ImportError:
import pickle # noqa
# pandas
try:
from pandas import DataFrame, Series

View File

@ -18,7 +18,7 @@ import numpy as np
import scipy.sparse
from .compat import (
STRING_TYPES, PY3, DataFrame, MultiIndex, py_str,
STRING_TYPES, DataFrame, MultiIndex, py_str,
PANDAS_INSTALLED, DataTable,
CUDF_INSTALLED, CUDF_DataFrame, CUDF_Series, CUDF_MultiIndex,
os_fspath, os_PathLike)
@ -70,11 +70,7 @@ def from_pystr_to_cstr(data):
if not isinstance(data, list):
raise NotImplementedError
pointers = (ctypes.c_char_p * len(data))()
if PY3:
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]
data = [bytes(d, 'utf-8') for d in data]
pointers[:] = data
return pointers
@ -89,21 +85,12 @@ def from_cstr_to_pystr(data, length):
length : ctypes pointer
pointer to length of data
"""
if PY3:
res = []
for i in range(length.value):
try:
res.append(str(data[i].decode('ascii')))
except UnicodeDecodeError:
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')))
res = []
for i in range(length.value):
try:
res.append(str(data[i].decode('ascii')))
except UnicodeDecodeError:
res.append(str(data[i].decode('utf-8')))
return res

View File

@ -1,14 +1,12 @@
# coding: utf-8
# pylint: disable= invalid-name
"""Distributed XGBoost Rabit related API."""
from __future__ import absolute_import
import sys
import ctypes
import pickle
import numpy as np
from .core import _LIB, c_str, STRING_TYPES
from .compat import pickle
def _init_rabit():

View File

@ -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=too-many-branches, too-many-statements, too-many-instance-attributes
from __future__ import absolute_import
import socket
import struct
import time
@ -336,4 +334,4 @@ class RabitTracker(object):
self.thread.join(100)
def alive(self):
return self.thread.isAlive()
return self.thread.is_alive()

View File

@ -2,8 +2,6 @@
# pylint: disable=too-many-locals, too-many-arguments, invalid-name
# pylint: disable=too-many-branches, too-many-statements
"""Training Library containing training routines."""
from __future__ import absolute_import
import numpy as np
from .core import Booster, STRING_TYPES, XGBoostError, CallbackEnv, EarlyStopException
from .compat import (SKLEARN_INSTALLED, XGBStratifiedKFold)

View File

@ -1,5 +1,3 @@
from __future__ import print_function
import glob
import itertools as it
import numpy as np

View File

@ -1,5 +1,3 @@
from __future__ import print_function
import numpy as np
import testing as tm
import unittest