From 03b7514510f3ed1abf02d2329a8d758859ca8fe9 Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Sun, 3 Feb 2019 14:41:20 +0300 Subject: [PATCH] Drop dependency on six and implement exec() function for py2 We use "imports from future" to get print() function. --- README.rst | 4 ---- mpmath/calculus/optimization.py | 34 +++++++++++++++++---------------- mpmath/functions/zeta.py | 12 +++++++----- mpmath/libmp/backend.py | 17 +++++++++++++++-- setup.cfg | 1 - 5 files changed, 40 insertions(+), 28 deletions(-) diff --git a/README.rst b/README.rst index ba971051..725ed018 100644 --- a/README.rst +++ b/README.rst @@ -51,10 +51,6 @@ Numerous other people have contributed by reporting bugs, requesting new features, or suggesting improvements to the documentation. -Mpmath includes a copy of Benjamin Peterson's six.py for -Python 2/3 compatibility. six.py is provided under the MIT license -(see the source file for details). - For a detailed changelog, including individual contributions, see the CHANGES file. diff --git a/mpmath/calculus/optimization.py b/mpmath/calculus/optimization.py index d1f94c90..d2324c75 100644 --- a/mpmath/calculus/optimization.py +++ b/mpmath/calculus/optimization.py @@ -1,6 +1,8 @@ +from __future__ import print_function + from copy import copy -from ..libmp.backend import xrange, print_ +from ..libmp.backend import xrange class OptimizationMethods(object): def __init__(ctx): @@ -268,8 +270,8 @@ class Muller: fx2x1x0 = (fx1x0 - fx2x1) / (x0 - x2) if w == 0 and fx2x1x0 == 0: if self.verbose: - print_('canceled with') - print_('x0 =', x0, ', x1 =', x1, 'and x2 =', x2) + print('canceled with') + print('x0 =', x0, ', x1 =', x1, 'and x2 =', x2) break x0 = x1 fx0 = fx1 @@ -395,7 +397,7 @@ class Illinois: self.method = kwargs.get('method', 'illinois') self.getm = _getm(self.method) if self.verbose: - print_('using %s method' % self.method) + print('using %s method' % self.method) def __iter__(self): method = self.method @@ -415,7 +417,7 @@ class Illinois: if abs(fz) < self.tol: # TODO: better condition (when f is very flat) if self.verbose: - print_('canceled with z =', z) + print('canceled with z =', z) yield z, l break if fz * fb < 0: # root in [z, b] @@ -429,7 +431,7 @@ class Illinois: fb = fz fa = m*fa # scale down to ensure convergence if self.verbose and m and not method == 'illinois': - print_('m:', m) + print('m:', m) yield (a + b)/2, abs(l) def Pegasus(*args, **kwargs): @@ -500,7 +502,7 @@ class Ridder: if abs(fx4) < self.tol: # TODO: better condition (when f is very flat) if self.verbose: - print_('canceled with f(x4) =', fx4) + print('canceled with f(x4) =', fx4) yield x4, abs(x1 - x2) break if fx4 * fx2 < 0: # root in [x4, x2] @@ -551,21 +553,21 @@ class ANewton: x0 = phi(x0) except ZeroDivisionError: if self.verbose: - print_('ZeroDivisionError: canceled with x =', x0) + print('ZeroDivisionError: canceled with x =', x0) break preverror = error error = abs(prevx - x0) # TODO: decide not to use convergence acceleration if error and abs(error - preverror) / error < 1: if self.verbose: - print_('converging slowly') + print('converging slowly') counter += 1 if counter >= 3: # accelerate convergence phi = steffensen(phi) counter = 0 if self.verbose: - print_('accelerating convergence') + print('accelerating convergence') yield x0, error # TODO: add Brent @@ -657,16 +659,16 @@ class MDNewton: Jx = J(*x0) s = self.ctx.lu_solve(Jx, fxn) if self.verbose: - print_('Jx:') - print_(Jx) - print_('s:', s) + print('Jx:') + print(Jx) + print('s:', s) # damping step size TODO: better strategy (hard task) l = self.ctx.one x1 = x0 + s while True: if x1 == x0: if self.verbose: - print_("canceled, won't get more excact") + print("canceled, won't get more excact") cancel = True break fx = self.ctx.matrix(f(*x1)) @@ -959,8 +961,8 @@ def findroot(ctx, f, x0, solver='secant', tol=None, verbose=False, verify=True, i = 0 for x, error in iterations: if verbose: - print_('x: ', x) - print_('error:', error) + print('x: ', x) + print('error:', error) i += 1 if error < tol * max(1, norm(x)) or i >= maxsteps: break diff --git a/mpmath/functions/zeta.py b/mpmath/functions/zeta.py index d067d714..a21dc99a 100644 --- a/mpmath/functions/zeta.py +++ b/mpmath/functions/zeta.py @@ -1,4 +1,6 @@ -from ..libmp.backend import xrange, print_ +from __future__ import print_function + +from ..libmp.backend import xrange from .functions import defun, defun_wrapped, defun_static @defun @@ -602,9 +604,9 @@ def _hurwitz(ctx, s, a=1, d=0, **kwargs): T1, T2 = _hurwitz_em(ctx, s, a, d, prec+10, verbose) cancellation = ctx.mag(T1) - ctx.mag(T1+T2) if verbose: - print_("Term 1:", T1) - print_("Term 2:", T2) - print_("Cancellation:", cancellation, "bits") + print("Term 1:", T1) + print("Term 2:", T2) + print("Cancellation:", cancellation, "bits") if cancellation < extraprec: return T1 + T2 else: @@ -713,7 +715,7 @@ def _hurwitz_em(ctx, s, a, d, prec, verbose): return lsum, (-1)**d * tailsum fact *= (j2+1)*(j2+2) if verbose: - print_("Sum range:", M1, M2, "term magnitude", ctx.mag(t), "tolerance", tol) + print("Sum range:", M1, M2, "term magnitude", ctx.mag(t), "tolerance", tol) M1, M2 = M2, M2*2 if ctx.re(s) < 0: N += N//2 diff --git a/mpmath/libmp/backend.py b/mpmath/libmp/backend.py index cf8643fd..1d82d941 100644 --- a/mpmath/libmp/backend.py +++ b/mpmath/libmp/backend.py @@ -29,17 +29,30 @@ else: BACKEND = 'python' -from six import exec_, print_ - if not python3: MPZ = long xrange = xrange basestring = basestring + + def exec_(_code_, _globs_=None, _locs_=None): + """Execute code in a namespace.""" + if _globs_ is None: + frame = sys._getframe(1) + _globs_ = frame.f_globals + if _locs_ is None: + _locs_ = frame.f_locals + del frame + elif _locs_ is None: + _locs_ = _globs_ + exec("""exec _code_ in _globs_, _locs_""") else: MPZ = int xrange = range basestring = str + import builtins + exec_ = getattr(builtins, "exec") + # Define constants for calculating hash on Python 3.2. if sys.version >= "3.2": HASH_MODULUS = sys.hash_info.modulus diff --git a/setup.cfg b/setup.cfg index 54e6a967..f41681fc 100644 --- a/setup.cfg +++ b/setup.cfg @@ -26,7 +26,6 @@ project_urls = Source = https://github.com/fredrik-johansson/mpmath packages = find: setup_requires = setuptools>=36.7.0 setuptools_scm>=1.7.0 -install_requires = six tests_require = mpmath[tests] [options.extras_require] tests = pytest