-
-
Notifications
You must be signed in to change notification settings - Fork 66
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Handle NaN #63
Handle NaN #63
Changes from 17 commits
6d2dcf6
414fe73
69dcb60
b4dbe70
f701644
c8b859b
3c61efd
f43a915
14cbcb1
31ad331
c0a1106
05f6c0f
9245532
b46e0d5
e543798
6b33558
e7f5c2b
b72d21c
e64ad9e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,17 @@ | |
NumberOrString: TypeAlias = "float | str" | ||
|
||
|
||
def format_non_finite(value: float) -> str: | ||
"""Utility function to handle infinite and nan cases.""" | ||
if math.isnan(value): | ||
return "NaN" | ||
if math.isinf(value) and value < 0: | ||
return "-Inf" | ||
elif math.isinf(value) and value > 0: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You may be interested in https://pylint.pycqa.org/en/latest/user_guide/messages/refactor/no-else-return.html :) |
||
return "+Inf" | ||
return "" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function seems overly complicated and not necessary to me. Why not just use the built-in string conversions? "nan", "-inf", and "inf"? if not math.isfinite(float(value)):
return str(float(value)) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My argument for |
||
|
||
|
||
def ordinal(value: NumberOrString, gender: str = "male") -> str: | ||
"""Converts an integer to its ordinal as a string. | ||
|
||
|
@@ -63,6 +74,8 @@ def ordinal(value: NumberOrString, gender: str = "male") -> str: | |
str: Ordinal string. | ||
""" | ||
try: | ||
if not math.isfinite(float(value)): | ||
return format_non_finite(float(value)) | ||
hugovk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
value = int(value) | ||
except (TypeError, ValueError): | ||
return str(value) | ||
|
@@ -135,11 +148,15 @@ def intcomma(value: NumberOrString, ndigits: int | None = None) -> str: | |
try: | ||
if isinstance(value, str): | ||
value = value.replace(thousands_sep, "").replace(decimal_sep, ".") | ||
if not math.isfinite(float(value)): | ||
return format_non_finite(float(value)) | ||
if "." in value: | ||
value = float(value) | ||
else: | ||
value = int(value) | ||
else: | ||
if not math.isfinite(float(value)): | ||
return format_non_finite(float(value)) | ||
float(value) | ||
except (TypeError, ValueError): | ||
return str(value) | ||
|
@@ -208,6 +225,8 @@ def intword(value: NumberOrString, format: str = "%.1f") -> str: | |
be coaxed into an `int`. | ||
""" | ||
try: | ||
if not math.isfinite(float(value)): | ||
return format_non_finite(float(value)) | ||
hugovk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
value = int(value) | ||
except (TypeError, ValueError): | ||
return str(value) | ||
|
@@ -271,6 +290,8 @@ def apnumber(value: NumberOrString) -> str: | |
is returned. | ||
""" | ||
try: | ||
if not math.isfinite(float(value)): | ||
return format_non_finite(float(value)) | ||
hugovk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
value = int(value) | ||
except (TypeError, ValueError): | ||
return str(value) | ||
|
@@ -330,6 +351,8 @@ def fractional(value: NumberOrString) -> str: | |
""" | ||
try: | ||
number = float(value) | ||
if not math.isfinite(number): | ||
return format_non_finite(number) | ||
hugovk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
except (TypeError, ValueError): | ||
return str(value) | ||
whole_number = int(number) | ||
|
@@ -393,6 +416,8 @@ def scientific(value: NumberOrString, precision: int = 2) -> str: | |
} | ||
try: | ||
value = float(value) | ||
if not math.isfinite(value): | ||
return format_non_finite(value) | ||
hugovk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
except (ValueError, TypeError): | ||
return str(value) | ||
fmt = "{:.%se}" % str(int(precision)) | ||
|
@@ -460,6 +485,9 @@ def clamp( | |
if value is None: | ||
return None | ||
|
||
if not math.isfinite(value): | ||
return format_non_finite(value) | ||
|
||
if floor is not None and value < floor: | ||
value = floor | ||
token = floor_token | ||
|
@@ -516,6 +544,8 @@ def metric(value: float, unit: str = "", precision: int = 3) -> str: | |
Returns: | ||
str: | ||
""" | ||
if not math.isfinite(value): | ||
return format_non_finite(value) | ||
hugovk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
exponent = int(math.floor(math.log10(abs(value)))) if value != 0 else 0 | ||
|
||
if exponent >= 27 or exponent < -24: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's have this as a "private" function:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm making it private...will it be merged then?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will merge when it's ready, thank you for all your work!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What more to add?