Skip to content

Commit

Permalink
Defer fractions.Fraction import to save 5ms (#201)
Browse files Browse the repository at this point in the history
  • Loading branch information
hugovk authored Sep 9, 2024
2 parents 74dcc60 + 99c196e commit 94fc781
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/humanize/number.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import math
import re
import sys
from fractions import Fraction
from typing import TYPE_CHECKING

from .i18n import _gettext as _
Expand Down Expand Up @@ -359,6 +358,8 @@ def fractional(value: NumberOrString) -> str:
return _format_not_finite(number)
except (TypeError, ValueError):
return str(value)
from fractions import Fraction

whole_number = int(number)
frac = Fraction(number - whole_number).limit_denominator(1000)
numerator = frac.numerator
Expand Down

0 comments on commit 94fc781

Please sign in to comment.