Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up Python 2 compatibility code. #5161

Merged
merged 2 commits into from Dec 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 0 additions & 2 deletions python-package/xgboost/__init__.py
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion python-package/xgboost/callback.py
@@ -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
Expand Down
27 changes: 6 additions & 21 deletions python-package/xgboost/compat.py
@@ -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')


###############################################################################
Expand Down Expand Up @@ -87,12 +78,6 @@ def os_fspath(path):
# END NUMPY PATHLIB ATTRIBUTION
###############################################################################

# pickle
try:
import cPickle as pickle # noqa
except ImportError:
import pickle # noqa

# pandas
try:
from pandas import DataFrame, Series
Expand Down
29 changes: 8 additions & 21 deletions python-package/xgboost/core.py
Expand Up @@ -18,7 +18,7 @@
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)
Expand Down Expand Up @@ -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

Expand All @@ -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


Expand Down
4 changes: 1 addition & 3 deletions python-package/xgboost/rabit.py
@@ -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():
Expand Down
4 changes: 1 addition & 3 deletions python-package/xgboost/tracker.py
Expand Up @@ -5,8 +5,6 @@

# 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
Expand Down Expand Up @@ -336,4 +334,4 @@ def join(self):
self.thread.join(100)

def alive(self):
return self.thread.isAlive()
return self.thread.is_alive()
2 changes: 0 additions & 2 deletions python-package/xgboost/training.py
Expand Up @@ -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)
Expand Down
2 changes: 0 additions & 2 deletions tests/python/regression_test_utilities.py
@@ -1,5 +1,3 @@
from __future__ import print_function

import glob
import itertools as it
import numpy as np
Expand Down
2 changes: 0 additions & 2 deletions tests/python/test_linear.py
@@ -1,5 +1,3 @@
from __future__ import print_function

import numpy as np
import testing as tm
import unittest
Expand Down