1
- from __future__ import absolute_import , print_function
2
- import numpy as np
1
+ import contextlib
3
2
import warnings
4
3
4
+ import numpy as np
5
+
5
6
6
7
def _bit_length_26 (x ):
7
8
if x == 0 :
@@ -60,9 +61,9 @@ def __init__(self, vstring):
60
61
raise ValueError ("Not a valid numpy version string" )
61
62
62
63
self .version = ver_main .group ()
63
- self .major , self .minor , self .bugfix = [
64
+ self .major , self .minor , self .bugfix = (
64
65
int (x ) for x in self .version .split ("." )
65
- ]
66
+ )
66
67
if len (vstring ) == ver_main .end ():
67
68
self .pre_release = "final"
68
69
else :
@@ -233,9 +234,9 @@ def _next_regular(target):
233
234
p2 = 2 ** _bit_length_26 (quotient - 1 )
234
235
235
236
N = p2 * p35
236
- if N == target :
237
+ if target == N :
237
238
return N
238
- elif N < match :
239
+ elif match > N :
239
240
match = N
240
241
p35 *= 3
241
242
if p35 == target :
@@ -330,21 +331,21 @@ def np_matrix_rank(M, tol=None):
330
331
S = np .linalg .svd (M , compute_uv = False )
331
332
if tol is None :
332
333
tol = S .max () * max (M .shape ) * np .finfo (S .dtype ).eps
333
- return np .sum (S > tol )
334
+ return np .sum (tol < S )
334
335
335
336
336
337
class CacheWriteWarning (UserWarning ):
337
338
pass
338
339
339
340
340
- class CachedAttribute ( object ) :
341
+ class CachedAttribute :
341
342
def __init__ (self , func , cachename = None , resetlist = None ):
342
343
self .fget = func
343
344
self .name = func .__name__
344
345
self .cachename = cachename or "_cache"
345
346
self .resetlist = resetlist or ()
346
347
347
- def __get__ (self , obj , type = None ):
348
+ def __get__ (self , obj , type_ = None ):
348
349
if obj is None :
349
350
return self .fget
350
351
# Get the cache or set a default one if needed
@@ -369,20 +370,18 @@ def __get__(self, obj, type=None):
369
370
# Update the reset list if needed (and possible)
370
371
resetlist = self .resetlist
371
372
if resetlist != ():
372
- try :
373
+ with contextlib . suppress ( AttributeError ) :
373
374
_cache ._resetdict [name ] = self .resetlist
374
- except AttributeError :
375
- pass
376
375
# else:
377
376
# print("Reading %s from cache (%s)" % (name, _cachedval))
378
377
return _cachedval
379
378
380
379
def __set__ (self , obj , value ):
381
380
errmsg = "The attribute '%s' cannot be overwritten" % self .name
382
- warnings .warn (errmsg , CacheWriteWarning )
381
+ warnings .warn (errmsg , CacheWriteWarning , stacklevel = 2 )
383
382
384
383
385
- class _cache_readonly ( object ):
384
+ class _cache_readonly : # noqa N801
386
385
"""
387
386
Decorator for CachedAttribute
388
387
"""
0 commit comments