Skip to content

Commit a85c171

Browse files
committed
Defer importing math to improve startup speed
1 parent 4762bdd commit a85c171

2 files changed

Lines changed: 20 additions & 3 deletions

File tree

src/humanize/number.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
from __future__ import annotations
44

5-
import math
6-
75
from .i18n import _gettext as _
86
from .i18n import _ngettext, decimal_separator, thousands_separator
97
from .i18n import _ngettext_noop as NS_
@@ -25,6 +23,8 @@
2523

2624
def _format_not_finite(value: float) -> str:
2725
"""Utility function to handle infinite and nan cases."""
26+
import math
27+
2828
if math.isnan(value):
2929
return "NaN"
3030
if math.isinf(value) and value < 0:
@@ -71,6 +71,8 @@ def ordinal(value: NumberOrString, gender: str = "male") -> str:
7171
Returns:
7272
str: Ordinal string.
7373
"""
74+
import math
75+
7476
try:
7577
if not math.isfinite(float(value)):
7678
return _format_not_finite(float(value))
@@ -142,6 +144,8 @@ def intcomma(value: NumberOrString, ndigits: int | None = None) -> str:
142144
Returns:
143145
str: String containing commas every three digits.
144146
"""
147+
import math
148+
145149
thousands_sep = thousands_separator()
146150
decimal_sep = decimal_separator()
147151
try:
@@ -226,6 +230,8 @@ def intword(value: NumberOrString, format: str = "%.1f") -> str:
226230
str: Friendly text representation as a string, unless the value passed could not
227231
be coaxed into an `int`.
228232
"""
233+
import math
234+
229235
try:
230236
if not math.isfinite(float(value)):
231237
return _format_not_finite(float(value))
@@ -293,6 +299,8 @@ def apnumber(value: NumberOrString) -> str:
293299
returns a string unless the value was not `int`-able, then `str(value)`
294300
is returned.
295301
"""
302+
import math
303+
296304
try:
297305
if not math.isfinite(float(value)):
298306
return _format_not_finite(float(value))
@@ -354,6 +362,8 @@ def fractional(value: NumberOrString) -> str:
354362
Returns:
355363
str: Fractional number as a string.
356364
"""
365+
import math
366+
357367
try:
358368
number = float(value)
359369
if not math.isfinite(number):
@@ -408,6 +418,8 @@ def scientific(value: NumberOrString, precision: int = 2) -> str:
408418
Returns:
409419
str: Number in scientific notation z.wq x 10ⁿ.
410420
"""
421+
import math
422+
411423
exponents = {
412424
"0": "⁰",
413425
"1": "¹",
@@ -491,6 +503,8 @@ def clamp(
491503
will be prepended with a token indicating as such.
492504
493505
"""
506+
import math
507+
494508
if value is None:
495509
return None
496510

@@ -554,6 +568,8 @@ def metric(value: float, unit: str = "", precision: int = 3) -> str:
554568
Returns:
555569
str:
556570
"""
571+
import math
572+
557573
if not math.isfinite(value):
558574
return _format_not_finite(value)
559575
exponent = int(math.floor(math.log10(abs(value)))) if value != 0 else 0

src/humanize/time.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
from __future__ import annotations
77

88
import datetime as dt
9-
import math
109
from enum import Enum
1110
from functools import total_ordering
1211

@@ -594,6 +593,8 @@ def precisedelta(
594593
if fmt_value > 0 or (not texts and unit == min_unit):
595594
_fmt_value = 2 if 1 < fmt_value < 2 else int(fmt_value)
596595
fmt_txt = _ngettext(singular_txt, plural_txt, _fmt_value)
596+
import math
597+
597598
if unit == min_unit and math.modf(fmt_value)[0] > 0:
598599
fmt_txt = fmt_txt.replace("%d", format)
599600
elif unit == YEARS:

0 commit comments

Comments
 (0)