Skip to content

Commit afa21ee

Browse files
MattShannonclaude
andauthored
Remove Python 2 compatibility code and modernize Python style (#20)
- Remove __div__ and __idiv__ methods (Python 2 old-style division) - Remove explicit object inheritance from BandMat class - Replace bare except clauses with except (TypeError, ValueError) - Convert %-style string formatting to f-strings https://claude.ai/code/session_017xmjvL47SYtX11EZjQ2uSJ Co-authored-by: Claude <noreply@anthropic.com>
1 parent ed7f7c1 commit afa21ee

File tree

4 files changed

+27
-66
lines changed

4 files changed

+27
-66
lines changed

bandmat/core.pyx

Lines changed: 10 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ cimport cython
1515
cnp.import_array()
1616
cnp.import_ufunc()
1717

18-
class BandMat(object):
18+
class BandMat:
1919
"""A memory-efficient representation of a square banded matrix.
2020
2121
An N by N matrix with bandwidth D can be stored efficiently by storing its
@@ -56,8 +56,7 @@ class BandMat(object):
5656
assert self.data.shape[0] == self.l + self.u + 1
5757

5858
def __repr__(self):
59-
return ('BandMat(%r, %r, %r, transposed=%r)' %
60-
(self.l, self.u, self.data, self.transposed))
59+
return f'BandMat({self.l!r}, {self.u!r}, {self.data!r}, transposed={self.transposed!r})'
6160

6261
@property
6362
def size(self):
@@ -344,7 +343,7 @@ class BandMat(object):
344343
"""
345344
try:
346345
mult = float(other)
347-
except:
346+
except (TypeError, ValueError):
348347
return NotImplemented
349348

350349
return BandMat(self.l, self.u, self.data * mult,
@@ -365,36 +364,16 @@ class BandMat(object):
365364
"""
366365
try:
367366
mult = float(other)
368-
except:
367+
except (TypeError, ValueError):
369368
return NotImplemented
370369

371370
return BandMat(self.l, self.u, self.data.__floordiv__(mult),
372371
transposed=self.transposed)
373372

374-
def __div__(self, other):
375-
"""Old-style divides a banded matrix by a scalar.
376-
377-
When using old-style division (c.f. `from __future__ import division`),
378-
the expression `a_bm / mult` where `a_bm` is a BandMat is the
379-
equivalent of:
380-
381-
a_full / mult
382-
383-
where `a_full` is a square numpy array.
384-
"""
385-
try:
386-
mult = float(other)
387-
except:
388-
return NotImplemented
389-
390-
return BandMat(self.l, self.u, self.data.__div__(mult),
391-
transposed=self.transposed)
392-
393373
def __truediv__(self, other):
394374
"""Divides a banded matrix by a scalar.
395375
396-
When using new-style division (c.f. `from __future__ import division`),
397-
the expression `a_bm / mult` where `a_bm` is a BandMat is the
376+
The expression `a_bm / mult` where `a_bm` is a BandMat is the
398377
equivalent of:
399378
400379
a_full / mult
@@ -403,7 +382,7 @@ class BandMat(object):
403382
"""
404383
try:
405384
mult = float(other)
406-
except:
385+
except (TypeError, ValueError):
407386
return NotImplemented
408387

409388
return BandMat(self.l, self.u, self.data.__truediv__(mult),
@@ -421,7 +400,7 @@ class BandMat(object):
421400
"""
422401
try:
423402
mult = float(other)
424-
except:
403+
except (TypeError, ValueError):
425404
return NotImplemented
426405

427406
self.data *= mult
@@ -439,36 +418,16 @@ class BandMat(object):
439418
"""
440419
try:
441420
mult = float(other)
442-
except:
421+
except (TypeError, ValueError):
443422
return NotImplemented
444423

445424
self.data.__ifloordiv__(mult)
446425
return self
447426

448-
def __idiv__(self, other):
449-
"""Old-style divides this matrix by a scalar in-place.
450-
451-
When using old-style division (c.f. `from __future__ import division`),
452-
the expression `a_bm /= mult` where `a_bm` is a BandMat is the
453-
equivalent of:
454-
455-
a_full /= mult
456-
457-
where `a_full` is a square numpy array.
458-
"""
459-
try:
460-
mult = float(other)
461-
except:
462-
return NotImplemented
463-
464-
self.data.__itruediv__(mult)
465-
return self
466-
467427
def __itruediv__(self, other):
468428
"""Divides this matrix by a scalar in-place.
469429
470-
When using new-style division (c.f. `from __future__ import division`),
471-
the expression `a_bm /= mult` where `a_bm` is a BandMat is the
430+
The statement `a_bm /= mult` where `a_bm` is a BandMat is the
472431
equivalent of:
473432
474433
a_full /= mult
@@ -477,7 +436,7 @@ class BandMat(object):
477436
"""
478437
try:
479438
mult = float(other)
480-
except:
439+
except (TypeError, ValueError):
481440
return NotImplemented
482441

483442
self.data.__itruediv__(mult)

bandmat/linalg.pyx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def _cholesky_banded(cnp.ndarray[cnp.float64_t, ndim=2] mat,
7676
v0 = mat[<unsigned long>(0), frame]
7777
if v0 <= 0.0:
7878
raise sla.LinAlgError(
79-
'%d-th leading minor not positive definite' % (frame + 1)
79+
f'{frame + 1}-th leading minor not positive definite'
8080
)
8181
iv0 = 1.0 / v0
8282
siv0 = sqrt(iv0)
@@ -167,7 +167,7 @@ def _solve_triangular_banded(cnp.ndarray[cnp.float64_t, ndim=2] a_rect,
167167
denom = a_rect[depth, frame]
168168
if denom == 0.0:
169169
raise sla.LinAlgError(
170-
'singular matrix: resolution failed at diagonal %d' % frame
170+
f'singular matrix: resolution failed at diagonal {frame}'
171171
)
172172
x[frame] = diff / denom
173173

bandmat/test_linalg.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,7 @@ def test__solve_triangular_banded(self, its=100):
167167
b_arg = b.copy()
168168
if badFrame is not None:
169169
msg = (
170-
'singular matrix: resolution failed at diagonal %d' %
171-
badFrame
170+
f'singular matrix: resolution failed at diagonal {badFrame}'
172171
)
173172
msgRe = '^' + re.escape(msg) + '$'
174173
with self.assertRaisesRegex(la.LinAlgError, msgRe):

bandmat/testhelp.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,27 @@
1111
def assert_allclose(actual, desired, rtol=1e-7, atol=1e-14,
1212
msg='items not almost equal'):
1313
if np.shape(actual) != np.shape(desired):
14-
raise AssertionError('%s (wrong shape)\n ACTUAL: %r\n DESIRED: %r' %
15-
(msg, actual, desired))
14+
raise AssertionError(
15+
f'{msg} (wrong shape)\n ACTUAL: {actual!r}\n DESIRED: {desired!r}'
16+
)
1617
if not np.allclose(actual, desired, rtol, atol):
1718
abs_err = np.abs(actual - desired)
1819
rel_err = np.abs((actual - desired) / desired)
19-
raise AssertionError('%s\n ACTUAL:\n%r\n DESIRED:\n%r\n'
20-
' ABS ERR: %r (max %s)\n REL ERR: %r (max %s)' %
21-
(msg, actual, desired,
22-
abs_err, np.max(abs_err),
23-
rel_err, np.max(rel_err)))
20+
raise AssertionError(
21+
f'{msg}\n ACTUAL:\n{actual!r}\n DESIRED:\n{desired!r}\n'
22+
f' ABS ERR: {abs_err!r} (max {np.max(abs_err)})\n'
23+
f' REL ERR: {rel_err!r} (max {np.max(rel_err)})'
24+
)
2425

2526
def assert_allequal(actual, desired, msg='items not equal'):
2627
if np.shape(actual) != np.shape(desired):
27-
raise AssertionError('%s (wrong shape)\n ACTUAL: %r\n DESIRED: %r' %
28-
(msg, actual, desired))
28+
raise AssertionError(
29+
f'{msg} (wrong shape)\n ACTUAL: {actual!r}\n DESIRED: {desired!r}'
30+
)
2931
if not np.all(actual == desired):
30-
raise AssertionError('%s\n ACTUAL:\n%r\n DESIRED:\n%r' %
31-
(msg, actual, desired))
32+
raise AssertionError(
33+
f'{msg}\n ACTUAL:\n{actual!r}\n DESIRED:\n{desired!r}'
34+
)
3235

3336
def randomize_extra_entries(l, u, mat_rect):
3437
"""Randomizes the extra entries of a rectangular matrix.

0 commit comments

Comments
 (0)