Skip to content

Commit ff71b1c

Browse files
committed
ruff+black utils.py
1 parent 3a89bc5 commit ff71b1c

File tree

1 file changed

+13
-14
lines changed

1 file changed

+13
-14
lines changed

spglm/utils.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
from __future__ import absolute_import, print_function
2-
import numpy as np
1+
import contextlib
32
import warnings
43

4+
import numpy as np
5+
56

67
def _bit_length_26(x):
78
if x == 0:
@@ -60,9 +61,9 @@ def __init__(self, vstring):
6061
raise ValueError("Not a valid numpy version string")
6162

6263
self.version = ver_main.group()
63-
self.major, self.minor, self.bugfix = [
64+
self.major, self.minor, self.bugfix = (
6465
int(x) for x in self.version.split(".")
65-
]
66+
)
6667
if len(vstring) == ver_main.end():
6768
self.pre_release = "final"
6869
else:
@@ -233,9 +234,9 @@ def _next_regular(target):
233234
p2 = 2 ** _bit_length_26(quotient - 1)
234235

235236
N = p2 * p35
236-
if N == target:
237+
if target == N:
237238
return N
238-
elif N < match:
239+
elif match > N:
239240
match = N
240241
p35 *= 3
241242
if p35 == target:
@@ -330,21 +331,21 @@ def np_matrix_rank(M, tol=None):
330331
S = np.linalg.svd(M, compute_uv=False)
331332
if tol is None:
332333
tol = S.max() * max(M.shape) * np.finfo(S.dtype).eps
333-
return np.sum(S > tol)
334+
return np.sum(tol < S)
334335

335336

336337
class CacheWriteWarning(UserWarning):
337338
pass
338339

339340

340-
class CachedAttribute(object):
341+
class CachedAttribute:
341342
def __init__(self, func, cachename=None, resetlist=None):
342343
self.fget = func
343344
self.name = func.__name__
344345
self.cachename = cachename or "_cache"
345346
self.resetlist = resetlist or ()
346347

347-
def __get__(self, obj, type=None):
348+
def __get__(self, obj, type_=None):
348349
if obj is None:
349350
return self.fget
350351
# Get the cache or set a default one if needed
@@ -369,20 +370,18 @@ def __get__(self, obj, type=None):
369370
# Update the reset list if needed (and possible)
370371
resetlist = self.resetlist
371372
if resetlist != ():
372-
try:
373+
with contextlib.suppress(AttributeError):
373374
_cache._resetdict[name] = self.resetlist
374-
except AttributeError:
375-
pass
376375
# else:
377376
# print("Reading %s from cache (%s)" % (name, _cachedval))
378377
return _cachedval
379378

380379
def __set__(self, obj, value):
381380
errmsg = "The attribute '%s' cannot be overwritten" % self.name
382-
warnings.warn(errmsg, CacheWriteWarning)
381+
warnings.warn(errmsg, CacheWriteWarning, stacklevel=2)
383382

384383

385-
class _cache_readonly(object):
384+
class _cache_readonly: # noqa N801
386385
"""
387386
Decorator for CachedAttribute
388387
"""

0 commit comments

Comments
 (0)