Skip to content

Commit

Permalink
ENH: better handling of non-numeric value in gformat()
Browse files Browse the repository at this point in the history
Closes: #845
  • Loading branch information
newville authored and reneeotten committed Mar 26, 2023
1 parent da30d0c commit 67f306f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
5 changes: 5 additions & 0 deletions lmfit/printfuncs.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,15 @@ def gformat(val, length=11):
Positive values will have leading blank.
"""
if val is None or isinstance(val, bool):
return f'{repr(val):>{length}s}'
try:
expon = int(log10(abs(val)))
except (OverflowError, ValueError):
expon = 0
except TypeError:
return f'{repr(val):>{length}s}'

length = max(length, 7)
form = 'e'
prec = length - 7
Expand Down
4 changes: 2 additions & 2 deletions tests/test_printfuncs.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,11 +306,11 @@ def test_report_modelpars(fitresult):


def test_report_parvalue_non_numeric(fitresult):
"""Verify that a non-numeric value is caught (can this ever happens?)."""
"""Verify that a non-numeric value is handled gracefully."""
fitresult.params['center'].value = None
fitresult.params['center'].stderr = None
report = fitresult.fit_report()
assert 'center: Non Numeric Value?' in report
assert len(report) > 50


def test_report_zero_value_spercent(fitresult):
Expand Down

0 comments on commit 67f306f

Please sign in to comment.