diff options
author | Charles Harris <charlesr.harris@gmail.com> | 2015-07-05 09:38:47 -0600 |
---|---|---|
committer | Charles Harris <charlesr.harris@gmail.com> | 2015-07-05 09:59:07 -0600 |
commit | c80c609950fe5aeecf3082d397c7b5149a834fca (patch) | |
tree | 21664c3a8ce6dff8302db62751b7c2017e37c80a /numpy/testing | |
parent | c2ae6aa0103aecdb5e2a71504583451cada1bfbc (diff) | |
download | numpy-c80c609950fe5aeecf3082d397c7b5149a834fca.tar.gz |
STY: PEP8 fixes for numpy/testing numpy/testing/tests.
Diffstat (limited to 'numpy/testing')
-rw-r--r-- | numpy/testing/decorators.py | 11 | ||||
-rw-r--r-- | numpy/testing/noseclasses.py | 22 | ||||
-rw-r--r-- | numpy/testing/nosetester.py | 13 | ||||
-rwxr-xr-x | numpy/testing/print_coercion_tables.py | 6 | ||||
-rwxr-xr-x | numpy/testing/setup.py | 12 | ||||
-rw-r--r-- | numpy/testing/tests/test_decorators.py | 2 | ||||
-rw-r--r-- | numpy/testing/tests/test_utils.py | 29 | ||||
-rw-r--r-- | numpy/testing/utils.py | 73 |
8 files changed, 87 insertions, 81 deletions
diff --git a/numpy/testing/decorators.py b/numpy/testing/decorators.py index 8a4cfb480..22026ec42 100644 --- a/numpy/testing/decorators.py +++ b/numpy/testing/decorators.py @@ -123,9 +123,9 @@ def skipif(skip_condition, msg=None): # Allow for both boolean or callable skip conditions. if isinstance(skip_condition, collections.Callable): - skip_val = lambda : skip_condition() + skip_val = lambda: skip_condition() else: - skip_val = lambda : skip_condition + skip_val = lambda: skip_condition def get_msg(func,msg=None): """Skip message with information about function being skipped.""" @@ -199,15 +199,16 @@ def knownfailureif(fail_condition, msg=None): # Allow for both boolean or callable known failure conditions. if isinstance(fail_condition, collections.Callable): - fail_val = lambda : fail_condition() + fail_val = lambda: fail_condition() else: - fail_val = lambda : fail_condition + fail_val = lambda: fail_condition def knownfail_decorator(f): # Local import to avoid a hard nose dependency and only incur the # import time overhead at actual test-time. import nose from .noseclasses import KnownFailureTest + def knownfailer(*args, **kwargs): if fail_val(): raise KnownFailureTest(msg) @@ -257,7 +258,7 @@ def deprecated(conditional=True): raise AssertionError("No warning raised when calling %s" % f.__name__) if not l[0].category is DeprecationWarning: - raise AssertionError("First warning for %s is not a " \ + raise AssertionError("First warning for %s is not a " "DeprecationWarning( is %s)" % (f.__name__, l[0])) if isinstance(conditional, collections.Callable): diff --git a/numpy/testing/noseclasses.py b/numpy/testing/noseclasses.py index cb757a13f..e6cc10179 100644 --- a/numpy/testing/noseclasses.py +++ b/numpy/testing/noseclasses.py @@ -59,7 +59,7 @@ class NumpyDocTestFinder(doctest.DocTestFinder): return module.__name__ == object.__module__ elif isinstance(object, property): #print '_fm C6' # dbg - return True # [XX] no way not be sure. + return True # [XX] no way not be sure. else: raise ValueError("object must be a class or function") @@ -77,20 +77,20 @@ class NumpyDocTestFinder(doctest.DocTestFinder): # doctests in extension modules. # Local shorthands - from inspect import isroutine, isclass, ismodule, isfunction, \ - ismethod + from inspect import ( + isroutine, isclass, ismodule, isfunction, ismethod + ) # Look for tests in a module's contained objects. if ismodule(obj) and self._recurse: for valname, val in obj.__dict__.items(): valname1 = '%s.%s' % (name, valname) if ( (isroutine(val) or isclass(val)) - and self._from_module(module, val) ): + and self._from_module(module, val)): self._find(tests, val, valname1, module, source_lines, globs, seen) - # Look for tests in a class's contained objects. if isclass(obj) and self._recurse: #print 'RECURSE into class:',obj # dbg @@ -128,12 +128,12 @@ class NumpyOutputChecker(doctest.OutputChecker): # actually some bigendian examples in the doctests). Let's try # making them all little endian got = got.replace("'>", "'<") - want= want.replace("'>", "'<") + want = want.replace("'>", "'<") # try to normalize out 32 and 64 bit default int sizes for sz in [4, 8]: - got = got.replace("'<i%d'"%sz, "int") - want= want.replace("'<i%d'"%sz, "int") + got = got.replace("'<i%d'" % sz, "int") + want = want.replace("'<i%d'" % sz, "int") ret = doctest.OutputChecker.check_output(self, want, got, optionflags) @@ -159,7 +159,7 @@ print_state = numpy.get_printoptions() class NumpyDoctest(npd.Doctest): name = 'numpydoctest' # call nosetests with --with-numpydoctest - score = 1000 # load late, after doctest builtin + score = 1000 # load late, after doctest builtin # always use whitespace and ellipsis options for doctests doctest_optflags = doctest.NORMALIZE_WHITESPACE | doctest.ELLIPSIS @@ -283,8 +283,8 @@ class Unplugger(object): By default it removes the "doctest" plugin. """ name = 'unplugger' - enabled = True # always enabled - score = 4000 # load late in order to be after builtins + enabled = True # always enabled + score = 4000 # load late in order to be after builtins def __init__(self, to_unplug='doctest'): self.to_unplug = to_unplug diff --git a/numpy/testing/nosetester.py b/numpy/testing/nosetester.py index b920399eb..d8c5977c6 100644 --- a/numpy/testing/nosetester.py +++ b/numpy/testing/nosetester.py @@ -67,10 +67,9 @@ def import_nose(): fine_nose = False if not fine_nose: - msg = 'Need nose >= %d.%d.%d for tests - see ' \ - 'http://somethingaboutorange.com/mrl/projects/nose' % \ - minimum_nose_version - + msg = ('Need nose >= %d.%d.%d for tests - see ' + 'http://somethingaboutorange.com/mrl/projects/nose' % + minimum_nose_version) raise ImportError(msg) return nose @@ -293,7 +292,7 @@ class NoseTester(object): argv += ['--exclude', ename] # our way of doing coverage if coverage: - argv+=['--cover-package=%s' % self.package_name, '--with-coverage', + argv += ['--cover-package=%s' % self.package_name, '--with-coverage', '--cover-tests', '--cover-erase'] # construct list of plugins import nose.plugins.builtin @@ -309,8 +308,8 @@ class NoseTester(object): # use standard doctesting if doctests and not doctest_argv: argv += ['--with-doctest'] - else: # custom doctesting - if doctest_argv: # in fact the unplugger would take care of this + else: # custom doctesting + if doctest_argv: # in fact the unplugger would take care of this argv.remove('--with-doctest') plugins += [Unplugger('doctest'), plug] if doctests: diff --git a/numpy/testing/print_coercion_tables.py b/numpy/testing/print_coercion_tables.py index bde82a666..3a359f472 100755 --- a/numpy/testing/print_coercion_tables.py +++ b/numpy/testing/print_coercion_tables.py @@ -21,7 +21,8 @@ class GenericObject(object): def print_cancast_table(ntypes): print('X', end=' ') - for char in ntypes: print(char, end=' ') + for char in ntypes: + print(char, end=' ') print() for row in ntypes: print(row, end=' ') @@ -31,7 +32,8 @@ def print_cancast_table(ntypes): def print_coercion_table(ntypes, inputfirstvalue, inputsecondvalue, firstarray, use_promote_types=False): print('+', end=' ') - for char in ntypes: print(char, end=' ') + for char in ntypes: + print(char, end=' ') print() for row in ntypes: if row == 'O': diff --git a/numpy/testing/setup.py b/numpy/testing/setup.py index 595e48925..7c1c237b9 100755 --- a/numpy/testing/setup.py +++ b/numpy/testing/setup.py @@ -11,10 +11,10 @@ def configuration(parent_package='',top_path=None): if __name__ == '__main__': from numpy.distutils.core import setup - setup(maintainer = "NumPy Developers", - maintainer_email = "numpy-dev@numpy.org", - description = "NumPy test module", - url = "http://www.numpy.org", - license = "NumPy License (BSD Style)", - configuration = configuration, + setup(maintainer="NumPy Developers", + maintainer_email="numpy-dev@numpy.org", + description="NumPy test module", + url="http://www.numpy.org", + license="NumPy License (BSD Style)", + configuration=configuration, ) diff --git a/numpy/testing/tests/test_decorators.py b/numpy/testing/tests/test_decorators.py index 36c7cc7bb..1c7f6c047 100644 --- a/numpy/testing/tests/test_decorators.py +++ b/numpy/testing/tests/test_decorators.py @@ -99,7 +99,6 @@ def test_skip_generators_hardcoded(): else: raise Exception('Failed to mark as known failure') - @dec.knownfailureif(False, "This test is NOT known to fail") def g2(x): for i in range(x): @@ -133,7 +132,6 @@ def test_skip_generators_callable(): else: raise Exception('Failed to mark as known failure') - @dec.knownfailureif(skip_tester, "This test is NOT known to fail") def g2(x): for i in range(x): diff --git a/numpy/testing/tests/test_utils.py b/numpy/testing/tests/test_utils.py index 68075fc3d..e75914833 100644 --- a/numpy/testing/tests/test_utils.py +++ b/numpy/testing/tests/test_utils.py @@ -236,18 +236,18 @@ class TestArrayAlmostEqual(_GenericTest, unittest.TestCase): ainf = np.array([np.inf]) self._assert_func(anan, anan) self.assertRaises(AssertionError, - lambda : self._assert_func(anan, aone)) + lambda: self._assert_func(anan, aone)) self.assertRaises(AssertionError, - lambda : self._assert_func(anan, ainf)) + lambda: self._assert_func(anan, ainf)) self.assertRaises(AssertionError, - lambda : self._assert_func(ainf, anan)) + lambda: self._assert_func(ainf, anan)) def test_inf(self): a = np.array([[1., 2.], [3., 4.]]) b = a.copy() a[0, 0] = np.inf self.assertRaises(AssertionError, - lambda : self._assert_func(a, b)) + lambda: self._assert_func(a, b)) def test_subclass(self): a = np.array([[1., 2.], [3., 4.]]) @@ -265,17 +265,17 @@ class TestAlmostEqual(_GenericTest, unittest.TestCase): def test_nan_item(self): self._assert_func(np.nan, np.nan) self.assertRaises(AssertionError, - lambda : self._assert_func(np.nan, 1)) + lambda: self._assert_func(np.nan, 1)) self.assertRaises(AssertionError, - lambda : self._assert_func(np.nan, np.inf)) + lambda: self._assert_func(np.nan, np.inf)) self.assertRaises(AssertionError, - lambda : self._assert_func(np.inf, np.nan)) + lambda: self._assert_func(np.inf, np.nan)) def test_inf_item(self): self._assert_func(np.inf, np.inf) self._assert_func(-np.inf, -np.inf) self.assertRaises(AssertionError, - lambda : self._assert_func(np.inf, 1)) + lambda: self._assert_func(np.inf, 1)) def test_simple_item(self): self._test_not_equal(1, 2) @@ -350,11 +350,11 @@ class TestApproxEqual(unittest.TestCase): ainf = np.array(np.inf) self._assert_func(anan, anan) self.assertRaises(AssertionError, - lambda : self._assert_func(anan, aone)) + lambda: self._assert_func(anan, aone)) self.assertRaises(AssertionError, - lambda : self._assert_func(anan, ainf)) + lambda: self._assert_func(anan, ainf)) self.assertRaises(AssertionError, - lambda : self._assert_func(ainf, anan)) + lambda: self._assert_func(ainf, anan)) def test_nan_items(self): anan = np.array(np.nan) @@ -362,11 +362,11 @@ class TestApproxEqual(unittest.TestCase): ainf = np.array(np.inf) self._assert_func(anan, anan) self.assertRaises(AssertionError, - lambda : self._assert_func(anan, aone)) + lambda: self._assert_func(anan, aone)) self.assertRaises(AssertionError, - lambda : self._assert_func(anan, ainf)) + lambda: self._assert_func(anan, ainf)) self.assertRaises(AssertionError, - lambda : self._assert_func(ainf, anan)) + lambda: self._assert_func(ainf, anan)) class TestRaises(unittest.TestCase): def setUp(self): @@ -539,7 +539,6 @@ class TestArrayAlmostEqualNulp(unittest.TestCase): self.assertRaises(AssertionError, assert_array_almost_equal_nulp, x, y, nulp) - def test_complex128_pass(self): nulp = 5 x = np.linspace(-20, 20, 50, dtype=np.float64) diff --git a/numpy/testing/utils.py b/numpy/testing/utils.py index 6244b3ea7..97408addb 100644 --- a/numpy/testing/utils.py +++ b/numpy/testing/utils.py @@ -34,7 +34,7 @@ __all__ = ['assert_equal', 'assert_almost_equal', 'assert_approx_equal', verbose = 0 -def assert_(val, msg='') : +def assert_(val, msg=''): """ Assert that works in release mode. Accepts callable msg to allow deferring evaluation until failure. @@ -45,7 +45,7 @@ def assert_(val, msg='') : For documentation on usage, refer to the Python documentation. """ - if not val : + if not val: try: smsg = msg() except TypeError: @@ -118,8 +118,8 @@ def rand(*args): f[i] = random.random() return results -if sys.platform[:5]=='linux': - def jiffies(_proc_pid_stat = '/proc/%s/stat'%(os.getpid()), +if sys.platform[:5] == 'linux': + def jiffies(_proc_pid_stat='/proc/%s/stat' % (os.getpid()), _load_time=[]): """ Return number of jiffies (1/100ths of a second) that this process has been scheduled in user mode. See man 5 proc. """ @@ -127,18 +127,18 @@ if sys.platform[:5]=='linux': if not _load_time: _load_time.append(time.time()) try: - f=open(_proc_pid_stat, 'r') + f = open(_proc_pid_stat, 'r') l = f.readline().split(' ') f.close() return int(l[13]) except: return int(100*(time.time()-_load_time[0])) - def memusage(_proc_pid_stat = '/proc/%s/stat'%(os.getpid())): + def memusage(_proc_pid_stat='/proc/%s/stat' % (os.getpid())): """ Return virtual memory size in bytes of the running python. """ try: - f=open(_proc_pid_stat, 'r') + f = open(_proc_pid_stat, 'r') l = f.readline().split(' ') f.close() return int(l[22]) @@ -155,14 +155,15 @@ else: if not _load_time: _load_time.append(time.time()) return int(100*(time.time()-_load_time[0])) + def memusage(): """ Return memory usage of running python. [Not implemented]""" raise NotImplementedError -if os.name=='nt': +if os.name == 'nt': # Code "stolen" from enthought/debug/memusage.py - def GetPerformanceAttributes(object, counter, instance = None, - inum=-1, format = None, machine=None): + def GetPerformanceAttributes(object, counter, instance=None, + inum=-1, format=None, machine=None): # NOTE: Many counters require 2 samples to give accurate results, # including "% Processor Time" (as by definition, at any instant, a # thread's CPU usage is either 0 or 100). To read counters like this, @@ -172,8 +173,9 @@ if os.name=='nt': # My older explanation for this was that the "AddCounter" process forced # the CPU to 100%, but the above makes more sense :) import win32pdh - if format is None: format = win32pdh.PDH_FMT_LONG - path = win32pdh.MakeCounterPath( (machine, object, instance, None, inum, counter) ) + if format is None: + format = win32pdh.PDH_FMT_LONG + path = win32pdh.MakeCounterPath( (machine, object, instance, None, inum, counter)) hq = win32pdh.OpenQuery() try: hc = win32pdh.AddCounter(hq, path) @@ -256,11 +258,11 @@ def assert_equal(actual,desired,err_msg='',verbose=True): """ if isinstance(desired, dict): - if not isinstance(actual, dict) : + if not isinstance(actual, dict): raise AssertionError(repr(type(actual))) assert_equal(len(actual), len(desired), err_msg, verbose) for k, i in desired.items(): - if k not in actual : + if k not in actual: raise AssertionError(repr(k)) assert_equal(actual[k], desired[k], 'key=%r\n%s' % (k, err_msg), verbose) return @@ -486,7 +488,7 @@ def assert_almost_equal(actual,desired,decimal=7,err_msg='',verbose=True): return except (NotImplementedError, TypeError): pass - if round(abs(desired - actual), decimal) != 0 : + if round(abs(desired - actual), decimal) != 0: raise AssertionError(_build_err_msg()) @@ -550,7 +552,7 @@ def assert_approx_equal(actual,desired,significant=7,err_msg='',verbose=True): import numpy as np (actual, desired) = map(float, (actual, desired)) - if desired==actual: + if desired == actual: return # Normalized the numbers to be in range (-10.0,10.0) # scale = float(pow(10,math.floor(math.log10(0.5*(abs(desired)+abs(actual)))))) @@ -583,7 +585,7 @@ def assert_approx_equal(actual,desired,significant=7,err_msg='',verbose=True): return except (TypeError, NotImplementedError): pass - if np.abs(sc_desired - sc_actual) >= np.power(10., -(significant-1)) : + if np.abs(sc_desired - sc_actual) >= np.power(10., -(significant-1)): raise AssertionError(msg) def assert_array_compare(comparison, x, y, err_msg='', verbose=True, @@ -625,13 +627,13 @@ def assert_array_compare(comparison, x, y, err_msg='', verbose=True, assert_array_equal(x_id, y_id) except AssertionError: msg = build_err_msg([x, y], - err_msg + '\nx and y %s location mismatch:' \ + err_msg + '\nx and y %s location mismatch:' % (hasval), verbose=verbose, header=header, names=('x', 'y'), precision=precision) raise AssertionError(msg) try: - cond = (x.shape==() or y.shape==()) or x.shape == y.shape + cond = (x.shape == () or y.shape == ()) or x.shape == y.shape if not cond: msg = build_err_msg([x, y], err_msg @@ -639,7 +641,7 @@ def assert_array_compare(comparison, x, y, err_msg='', verbose=True, y.shape), verbose=verbose, header=header, names=('x', 'y'), precision=precision) - if not cond : + if not cond: raise AssertionError(msg) if isnumber(x) and isnumber(y): @@ -684,7 +686,7 @@ def assert_array_compare(comparison, x, y, err_msg='', verbose=True, + '\n(mismatch %s%%)' % (match,), verbose=verbose, header=header, names=('x', 'y'), precision=precision) - if not cond : + if not cond: raise AssertionError(msg) except ValueError as e: import traceback @@ -834,6 +836,7 @@ def assert_array_almost_equal(x, y, decimal=6, err_msg='', verbose=True): from numpy.core import around, number, float_, result_type, array from numpy.core.numerictypes import issubdtype from numpy.core.fromnumeric import any as npany + def compare(x, y): try: if npany(gisinf(x)) or npany( gisinf(y)): @@ -856,7 +859,7 @@ def assert_array_almost_equal(x, y, decimal=6, err_msg='', verbose=True): z = abs(x-y) if not issubdtype(z.dtype, number): - z = z.astype(float_) # handle object arrays + z = z.astype(float_) # handle object arrays return around(z, decimal) <= 10.0**(-decimal) @@ -967,7 +970,7 @@ def assert_string_equal(actual, desired): # delay import of difflib to reduce startup time import difflib - if not isinstance(actual, str) : + if not isinstance(actual, str): raise AssertionError(repr(type(actual))) if not isinstance(desired, str): raise AssertionError(repr(type(desired))) @@ -986,7 +989,7 @@ def assert_string_equal(actual, desired): if d2.startswith('? '): l.append(d2) d2 = diff.pop(0) - if not d2.startswith('+ ') : + if not d2.startswith('+ '): raise AssertionError(repr(d2)) l.append(d2) d3 = diff.pop(0) @@ -1002,7 +1005,7 @@ def assert_string_equal(actual, desired): if not diff_list: return msg = 'Differences in strings:\n%s' % (''.join(diff_list)).rstrip() - if actual != desired : + if actual != desired: raise AssertionError(msg) @@ -1028,7 +1031,8 @@ def rundocs(filename=None, raise_on_error=True): >>> np.lib.test(doctests=True) #doctest: +SKIP """ - import doctest, imp + import doctest + import imp if filename is None: f = sys._getframe(1) filename = f.f_globals['__file__'] @@ -1313,6 +1317,7 @@ def assert_allclose(actual, desired, rtol=1e-7, atol=0, equal_nan=False, """ import numpy as np + def compare(x, y): return np.core.numeric.isclose(x, y, rtol=rtol, atol=atol, equal_nan=equal_nan) @@ -1422,7 +1427,7 @@ def assert_array_max_ulp(a, b, maxulp=1, dtype=None): import numpy as np ret = nulp_diff(a, b, dtype) if not np.all(ret <= maxulp): - raise AssertionError("Arrays are not almost equal up to %g ULP" % \ + raise AssertionError("Arrays are not almost equal up to %g ULP" % maxulp) return ret @@ -1468,7 +1473,7 @@ def nulp_diff(x, y, dtype=None): y = np.array(y, dtype=t) if not x.shape == y.shape: - raise ValueError("x and y do not have the same shape: %s - %s" % \ + raise ValueError("x and y do not have the same shape: %s - %s" % (x.shape, y.shape)) def _diff(rx, ry, vdt): @@ -1486,7 +1491,7 @@ def _integer_repr(x, vdt, comp): # http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm rx = x.view(vdt) if not (rx.size == 1): - rx[rx < 0] = comp - rx[rx<0] + rx[rx < 0] = comp - rx[rx < 0] else: if rx < 0: rx = comp - rx @@ -1562,6 +1567,7 @@ class WarningManager(object): It is copied so it can be used in NumPy with older Python versions. """ + def __init__(self, record=False, module=None): self._record = record if module is None: @@ -1579,6 +1585,7 @@ class WarningManager(object): self._showwarning = self._module.showwarning if self._record: log = [] + def showwarning(*args, **kwargs): log.append(WarningMessage(*args, **kwargs)) self._module.showwarning = showwarning @@ -1627,7 +1634,7 @@ def assert_warns(warning_class, func, *args, **kw): raise AssertionError("No warning raised when calling %s" % func.__name__) if not l[0].category is warning_class: - raise AssertionError("First warning for %s is not a " \ + raise AssertionError("First warning for %s is not a " "%s( is %s)" % (func.__name__, warning_class, l[0])) return result @@ -1690,7 +1697,7 @@ def _gen_alignment_data(dtype=float32, type='binary', max_size=24): for o in range(3): for s in range(o + 2, max(o + 3, max_size)): if type == 'unary': - inp = lambda : arange(s, dtype=dtype)[o:] + inp = lambda: arange(s, dtype=dtype)[o:] out = empty((s,), dtype=dtype)[o:] yield out, inp(), ufmt % (o, o, s, dtype, 'out of place') yield inp(), inp(), ufmt % (o, o, s, dtype, 'in place') @@ -1703,8 +1710,8 @@ def _gen_alignment_data(dtype=float32, type='binary', max_size=24): yield inp()[1:], inp()[:-1], ufmt % \ (o + 1, o, s - 1, dtype, 'aliased') if type == 'binary': - inp1 = lambda :arange(s, dtype=dtype)[o:] - inp2 = lambda :arange(s, dtype=dtype)[o:] + inp1 = lambda: arange(s, dtype=dtype)[o:] + inp2 = lambda: arange(s, dtype=dtype)[o:] out = empty((s,), dtype=dtype)[o:] yield out, inp1(), inp2(), bfmt % \ (o, o, o, s, dtype, 'out of place') |