From 302a6a54e9fffff5a215ee72dc1f360204c32499 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Apitzsch?= Date: Thu, 14 Mar 2024 17:41:01 +0100 Subject: [PATCH] Remove Python 2 compatibility code --- cylp/cpp/IClpSimplex.cpp | 15 +++------------ cylp/cpp/ICoinIndexedVector.cpp | 16 ++++------------ cylp/cy/CyCbcModel.pyx | 4 ---- cylp/cy/CyClpPrimalColumnSteepest.pyx | 3 --- cylp/cy/CyClpSimplex.pyx | 8 +------- cylp/cy/CyCoinIndexedVector.pyx | 2 -- cylp/cy/CyPEPivot.pyx | 2 -- cylp/cy/CySolve.py | 1 - cylp/cy/CyTest.pyx | 1 - cylp/input/fileDownloader.py | 1 - cylp/input/netlib/fileDownloader.py | 1 - cylp/py/PySolve.py | 1 - cylp/py/QP/GQP.py | 1 - cylp/py/QP/QP.py | 1 - cylp/py/QP/QPGen.py | 1 - cylp/py/mip/GomoryCutGenerator.py | 1 - cylp/py/modeling/CyLPModel.py | 12 ++---------- cylp/py/pivots/DualDantzigPivot.py | 1 - cylp/py/pivots/PositiveEdgePivot.py | 1 - cylp/py/pivots/WolfePivot.py | 7 +------ cylp/py/pivots/WolfePivotPE.py | 4 +--- cylp/py/utils/readSetcovering.py | 1 - cylp/py/utils/sparseUtil.py | 17 ++++------------- cylp/py/utils/util.py | 10 +--------- cylp/tests/test_MIP.py | 1 - cylp/tests/test_QP.py | 1 - fixBinaries.py | 1 - 27 files changed, 17 insertions(+), 98 deletions(-) diff --git a/cylp/cpp/IClpSimplex.cpp b/cylp/cpp/IClpSimplex.cpp index 3fd20390..512ff300 100644 --- a/cylp/cpp/IClpSimplex.cpp +++ b/cylp/cpp/IClpSimplex.cpp @@ -8,15 +8,6 @@ #include "OsiClpSolverInterface.hpp" #include -// define PyInt_* macros for Python 3.x -#ifndef PyInt_Check -#define PyInt_Check PyLong_Check -#define PyInt_FromLong PyLong_FromLong -#define PyInt_AsLong PyLong_AsLong -#define PyInt_Type PyLong_Type -#endif - - int IClpSimplex::argWeightedMax(PyObject* arr, PyObject* arr_ind, PyObject* w, PyObject* w_ind){ //_import_array(); @@ -28,9 +19,9 @@ int IClpSimplex::argWeightedMax(PyObject* arr, PyObject* arr_ind, PyObject* w, P int wholeArray = false; double w_num_val; - if (PyInt_Check(w)){ + if (PyLong_Check(w)){ wIsNum = true; - w_num_val = (double)PyInt_AsLong(w); + w_num_val = PyLong_AsDouble(w); }else if (PyFloat_Check(w)){ wIsNum = true; w_num_val = PyFloat_AsDouble(w); @@ -41,7 +32,7 @@ int IClpSimplex::argWeightedMax(PyObject* arr, PyObject* arr_ind, PyObject* w, P } - if (PyInt_Check(arr_ind) || PyFloat_Check(arr_ind)){ + if (PyLong_Check(arr_ind) || PyFloat_Check(arr_ind)){ wholeArray = true; }else if (!PyArray_Check(arr_ind)){ PyErr_SetString(PyExc_ValueError, diff --git a/cylp/cpp/ICoinIndexedVector.cpp b/cylp/cpp/ICoinIndexedVector.cpp index db51863e..2b2c35f7 100644 --- a/cylp/cpp/ICoinIndexedVector.cpp +++ b/cylp/cpp/ICoinIndexedVector.cpp @@ -1,13 +1,5 @@ #include "ICoinIndexedVector.hpp" -// define PyInt_* macros for Python 3.x -#ifndef PyInt_Check -#define PyInt_Check PyLong_Check -#define PyInt_FromLong PyLong_FromLong -#define PyInt_AsLong PyLong_AsLong -#define PyInt_Type PyLong_Type -#endif - ICoinIndexedVector::ICoinIndexedVector(){ _import_array(); } @@ -68,8 +60,8 @@ void ICoinIndexedVector::assign(PyObject* ind, PyObject* other){ //_import_array(); - if( PyInt_Check(other) ) { - val = (double)PyInt_AsLong(other); + if( PyLong_Check(other) ) { + val = PyLong_AsDouble(other); other_is_num = 1; } else if( PyFloat_Check(other) ) { val = PyFloat_AsDouble(other); @@ -84,8 +76,8 @@ void ICoinIndexedVector::assign(PyObject* ind, PyObject* other){ } - if( PyInt_Check(ind) ) { - idx = (double)PyInt_AsLong(ind); + if( PyLong_Check(ind) ) { + idx = PyLong_AsDouble(ind); ind_is_num = 1; } else if( PyFloat_Check(ind) ) { idx = PyFloat_AsDouble(ind); diff --git a/cylp/cy/CyCbcModel.pyx b/cylp/cy/CyCbcModel.pyx index 4491855f..4771e236 100644 --- a/cylp/cy/CyCbcModel.pyx +++ b/cylp/cy/CyCbcModel.pyx @@ -1,10 +1,6 @@ # cython: embedsignature=True from itertools import product -try: - from itertools import izip -except ImportError: # Python 3 does not have izip, use zip - izip = zip from cylp.py.mip import NodeCompareBase from cylp.py.modeling.CyLPModel import CyLPSolution from cylp.cy.CyCutGeneratorPythonBase cimport CyCutGeneratorPythonBase diff --git a/cylp/cy/CyClpPrimalColumnSteepest.pyx b/cylp/cy/CyClpPrimalColumnSteepest.pyx index 4106c309..cb2bc833 100644 --- a/cylp/cy/CyClpPrimalColumnSteepest.pyx +++ b/cylp/cy/CyClpPrimalColumnSteepest.pyx @@ -1,8 +1,5 @@ # cython: embedsignature=True -from __future__ import print_function - - #cimport cClpPrimalColumnSteepest from cClpPrimalColumnSteepest cimport c_ClpPrimalColumnSteepest, new_ClpPrimalColumnSteepest #cimport cClpPrimalColumnPivot diff --git a/cylp/cy/CyClpSimplex.pyx b/cylp/cy/CyClpSimplex.pyx index f6a8bfbc..f049870b 100644 --- a/cylp/cy/CyClpSimplex.pyx +++ b/cylp/cy/CyClpSimplex.pyx @@ -2,15 +2,9 @@ # cython: profile=True # cython: embedsignature=True -from __future__ import print_function - import inspect import os.path from itertools import product -try: - from itertools import izip -except ImportError: # Python 3 does not have izip, use zip - izip = zip import numpy as np cimport numpy as np from scipy import sparse @@ -160,7 +154,7 @@ cdef class CyClpSimplex: # self.setObjectiveCoefficient(i, o[0,i]) #if not isinstance(o, sparse.coo_matrix): # o = o.tocoo() - #for i, j, v in izip(o.row, o.col, o.data): + #for i, j, v in zip(o.row, o.col, o.data): # self.setObjectiveCoefficient(j, v) #self.setObjectiveArray( # self.cyLPModel.objective.astype(np.double)) diff --git a/cylp/cy/CyCoinIndexedVector.pyx b/cylp/cy/CyCoinIndexedVector.pyx index 1e61372e..1ccb5c88 100644 --- a/cylp/cy/CyCoinIndexedVector.pyx +++ b/cylp/cy/CyCoinIndexedVector.pyx @@ -1,7 +1,5 @@ # cython: embedsignature=True -from __future__ import print_function - cdef class CyCoinIndexedVector: ''' ``CyCoinIndexedVector`` interfaces ``CoinIndexedVector``. diff --git a/cylp/cy/CyPEPivot.pyx b/cylp/cy/CyPEPivot.pyx index 397c2cda..0c591ce6 100644 --- a/cylp/cy/CyPEPivot.pyx +++ b/cylp/cy/CyPEPivot.pyx @@ -1,7 +1,5 @@ # cython: embedsignature=True -from __future__ import print_function - import numpy as np cimport numpy as np from cylp.cy cimport CyPEPivot diff --git a/cylp/cy/CySolve.py b/cylp/cy/CySolve.py index 983cf041..6b4d1460 100644 --- a/cylp/cy/CySolve.py +++ b/cylp/cy/CySolve.py @@ -1,4 +1,3 @@ -from __future__ import print_function import sys from cylp.cy.CyTest import CySolve diff --git a/cylp/cy/CyTest.pyx b/cylp/cy/CyTest.pyx index 0133c1ba..67bdc2fd 100644 --- a/cylp/cy/CyTest.pyx +++ b/cylp/cy/CyTest.pyx @@ -1,4 +1,3 @@ -from __future__ import print_function import sys from time import perf_counter from cylp.cy.CyClpSimplex cimport CyClpSimplex diff --git a/cylp/input/fileDownloader.py b/cylp/input/fileDownloader.py index 30e63d3f..11262f7d 100644 --- a/cylp/input/fileDownloader.py +++ b/cylp/input/fileDownloader.py @@ -1,4 +1,3 @@ -from __future__ import print_function import urllib f = urllib.urlopen("http://www.netlib.org/lp/data/") lines = f.read().splitlines() diff --git a/cylp/input/netlib/fileDownloader.py b/cylp/input/netlib/fileDownloader.py index e3e3b077..cf76a2de 100644 --- a/cylp/input/netlib/fileDownloader.py +++ b/cylp/input/netlib/fileDownloader.py @@ -1,4 +1,3 @@ -from __future__ import print_function import urllib import os diff --git a/cylp/py/PySolve.py b/cylp/py/PySolve.py index f7b901ac..4e69d7b0 100644 --- a/cylp/py/PySolve.py +++ b/cylp/py/PySolve.py @@ -1,4 +1,3 @@ -from __future__ import print_function import sys from time import perf_counter import cProfile diff --git a/cylp/py/QP/GQP.py b/cylp/py/QP/GQP.py index c9bbe7fb..aebc5e74 100644 --- a/cylp/py/QP/GQP.py +++ b/cylp/py/QP/GQP.py @@ -1,4 +1,3 @@ -from __future__ import print_function import QP import numpy as np import Constants diff --git a/cylp/py/QP/QP.py b/cylp/py/QP/QP.py index 50f05245..68341bfa 100644 --- a/cylp/py/QP/QP.py +++ b/cylp/py/QP/QP.py @@ -1,6 +1,5 @@ #UNDER DEVELOPMENT AND TEST -from __future__ import print_function import sys import cProfile import inspect diff --git a/cylp/py/QP/QPGen.py b/cylp/py/QP/QPGen.py index b1a509fc..a0dac0a8 100644 --- a/cylp/py/QP/QPGen.py +++ b/cylp/py/QP/QPGen.py @@ -1,4 +1,3 @@ -from __future__ import print_function #import random import numpy as np from numpy import random diff --git a/cylp/py/mip/GomoryCutGenerator.py b/cylp/py/mip/GomoryCutGenerator.py index 2048b777..545e246e 100644 --- a/cylp/py/mip/GomoryCutGenerator.py +++ b/cylp/py/mip/GomoryCutGenerator.py @@ -1,4 +1,3 @@ -from __future__ import print_function import os import math import numpy as np diff --git a/cylp/py/modeling/CyLPModel.py b/cylp/py/modeling/CyLPModel.py index 830a8430..f5f69a06 100644 --- a/cylp/py/modeling/CyLPModel.py +++ b/cylp/py/modeling/CyLPModel.py @@ -119,15 +119,7 @@ ''' -from __future__ import print_function from functools import reduce - -# Python 3 does not have long, only int -try: - long -except NameError: - long = int - from itertools import product from copy import deepcopy from operator import mul @@ -136,7 +128,7 @@ from scipy.sparse import identity, lil_matrix from cylp.py.utils.util import Ind, getMultiDimMatrixIndex, getTupleIndex -NUMBERS = (int, float, long, np.int64, np.int32, np.double) +NUMBERS = (int, float, np.int64, np.int32, np.double) def isNumber(n): return (isinstance(n, NUMBERS) or (isinstance(n, CyLPArray) and n.shape == ())) @@ -524,7 +516,7 @@ def perform(self, opr, left=None, right=None): #dim = left.parentDim else: # # FIXME: Think of the correct way to check bound dimensions -# if (not isinstance(right, (float, long, int)) and +# if (not isinstance(right, (float, int)) and # right.shape[0] != self.nRows): # #raise Exception('Bound dim: %d, expected dim: %d ' # # % (right.shape[0], self.nRows)) diff --git a/cylp/py/pivots/DualDantzigPivot.py b/cylp/py/pivots/DualDantzigPivot.py index f748ac06..4f79e0cf 100644 --- a/cylp/py/pivots/DualDantzigPivot.py +++ b/cylp/py/pivots/DualDantzigPivot.py @@ -4,7 +4,6 @@ for testing purposes we implement one in Python. ''' -from __future__ import print_function import sys import numpy as np from operator import itemgetter diff --git a/cylp/py/pivots/PositiveEdgePivot.py b/cylp/py/pivots/PositiveEdgePivot.py index 5d58f376..ecc94c51 100644 --- a/cylp/py/pivots/PositiveEdgePivot.py +++ b/cylp/py/pivots/PositiveEdgePivot.py @@ -3,7 +3,6 @@ pivot selection rule. ''' -from __future__ import print_function import random import numpy as np from cylp.cy import CyCoinIndexedVector diff --git a/cylp/py/pivots/WolfePivot.py b/cylp/py/pivots/WolfePivot.py index 7ec88c31..36e4cbe2 100644 --- a/cylp/py/pivots/WolfePivot.py +++ b/cylp/py/pivots/WolfePivot.py @@ -1,8 +1,3 @@ -from __future__ import print_function -try: - from itertools import izip -except ImportError: # Python 3 does not have izip use zip - izip = zip import numpy as np from .PivotPythonBase import PivotPythonBase @@ -96,6 +91,6 @@ def setComplement(self, model, v1, v2): v2n = v2.name listv1 = np.array(model.inds.varIndex[v1n])[v1.indices] listv2 = np.array(model.inds.varIndex[v2n])[v2.indices] - for i, j in izip(listv1, listv2): + for i, j in zip(listv1, listv2): self.complementarityList[i], self.complementarityList[j] = j, i diff --git a/cylp/py/pivots/WolfePivotPE.py b/cylp/py/pivots/WolfePivotPE.py index 28d4a88f..0ef7d30e 100644 --- a/cylp/py/pivots/WolfePivotPE.py +++ b/cylp/py/pivots/WolfePivotPE.py @@ -1,5 +1,3 @@ -from __future__ import print_function -from itertools import izip import random import numpy as np from cylp.cy import CyCoinIndexedVector @@ -251,7 +249,7 @@ def setComplement(self, model, v1, v2): v2n = v2.name listv1 = np.array(model.inds.varIndex[v1n])[v1.indices] listv2 = np.array(model.inds.varIndex[v2n])[v2.indices] - for i, j in izip(listv1, listv2): + for i, j in zip(listv1, listv2): (self.complementarityList[i], self.complementarityList[j]) = \ (self.complementarityList[j], self.complementarityList[i]) diff --git a/cylp/py/utils/readSetcovering.py b/cylp/py/utils/readSetcovering.py index e2910997..46697185 100644 --- a/cylp/py/utils/readSetcovering.py +++ b/cylp/py/utils/readSetcovering.py @@ -1,4 +1,3 @@ -from __future__ import print_function import numpy as np from scipy import sparse from cylp.cy import CyClpSimplex diff --git a/cylp/py/utils/sparseUtil.py b/cylp/py/utils/sparseUtil.py index 16f9b45f..833efc40 100644 --- a/cylp/py/utils/sparseUtil.py +++ b/cylp/py/utils/sparseUtil.py @@ -10,18 +10,9 @@ regardless of their dimension alignments. Fills with zeros where necessary. ''' - -# Python 3 does not have long, only int -try: - long -except NameError: - long = int - from scipy import sparse import numpy as np - - class csc_matrixPlus(sparse.csc_matrix): def __init__(self, arg1, shape=None, dtype=None, copy=False, fromMatrix=None): @@ -53,7 +44,7 @@ def __setitem__(self, location, val): ''' iRow, iCol = location - if not isinstance(val, (int, long, float)): + if not isinstance(val, (int, float)): return sparse.csc_matrix.__setitem__(self, (iRow, iCol), val) nCols = self.shape[1] @@ -117,7 +108,7 @@ def addColumns(self, nCol): def __getitem__(self, key): ret = sparse.csc_matrix.__getitem__(self, key) - if isinstance(ret, (int, long, float)): + if isinstance(ret, (int, float)): return ret # This seems to cause some potential problems when the result is 1x1 # It should really be returned as an int/float in that case, but @@ -268,7 +259,7 @@ def __setitem__(self, location, val): ''' iRow, iCol = location - if not isinstance(val, (int, long, float)): + if not isinstance(val, (int, float)): return sparse.csr_matrix.__setitem__(self, (iRow, iCol), val) nRows = self.shape[0] @@ -356,7 +347,7 @@ def addRows(self, nRow): def __getitem__(self, key): ret = sparse.csr_matrix.__getitem__(self, key) - if isinstance(ret, (int, long, float)): + if isinstance(ret, (int, float)): return ret # This seems to cause some potential problems when the result is 1x1 # It should really be returned as an int/float in that case, but diff --git a/cylp/py/utils/util.py b/cylp/py/utils/util.py index b226dc40..16054e15 100644 --- a/cylp/py/utils/util.py +++ b/cylp/py/utils/util.py @@ -1,11 +1,3 @@ -from __future__ import print_function - -# Python 3 does not have long, only int -try: - long -except NameError: - long = int - import numpy as np from math import atan2 from cylp.py import Constants @@ -138,7 +130,7 @@ def __init__(self, key, dim): start = sl.start self.indices = range(start, stop) self.dim = dim - elif isinstance(key, (int, long)): + elif isinstance(key, int): if key >= dim: raise Exception('Index (%d) out of range (%d)' % (key, dim)) self.indices = [key] diff --git a/cylp/tests/test_MIP.py b/cylp/tests/test_MIP.py index d4e7d80f..e00af9e0 100644 --- a/cylp/tests/test_MIP.py +++ b/cylp/tests/test_MIP.py @@ -1,4 +1,3 @@ -from __future__ import print_function import unittest import inspect import os diff --git a/cylp/tests/test_QP.py b/cylp/tests/test_QP.py index 1ef71080..c6c3a885 100644 --- a/cylp/tests/test_QP.py +++ b/cylp/tests/test_QP.py @@ -1,4 +1,3 @@ -from __future__ import print_function import unittest import inspect import os diff --git a/fixBinaries.py b/fixBinaries.py index 7e520131..1c07db78 100644 --- a/fixBinaries.py +++ b/fixBinaries.py @@ -2,7 +2,6 @@ This module contains code that copies CBC libraries into CyLP's source, and make CyLP binaries' refernces to them relative. ''' -from __future__ import print_function import distutils.util import sys import os