Skip to content

Commit e0a0ef0

Browse files
authored
Merge pull request #42 from hugovk/fixes
2 parents afdde01 + 289f8ee commit e0a0ef0

File tree

5 files changed

+94
-68
lines changed

5 files changed

+94
-68
lines changed

src/humanize/filesize.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,22 @@ def naturalsize(
6060

6161
if abs_bytes == 1 and not gnu:
6262
return "%d Byte" % bytes_
63-
elif abs_bytes < base and not gnu:
63+
64+
if abs_bytes < base and not gnu:
6465
return "%d Bytes" % bytes_
65-
elif abs_bytes < base and gnu:
66+
67+
if abs_bytes < base and gnu:
6668
return "%dB" % bytes_
6769

6870
for i, s in enumerate(suffix):
6971
unit = base ** (i + 2)
72+
7073
if abs_bytes < unit and not gnu:
7174
return (format + " %s") % ((base * bytes_ / unit), s)
72-
elif abs_bytes < unit and gnu:
75+
76+
if abs_bytes < unit and gnu:
7377
return (format + "%s") % ((base * bytes_ / unit), s)
78+
7479
if gnu:
7580
return (format + "%s") % ((base * bytes_ / unit), s)
7681
return (format + " %s") % ((base * bytes_ / unit), s)

src/humanize/number.py

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,8 @@ def intcomma(value: NumberOrString, ndigits: int | None = None) -> str:
145145
new = re.sub(r"^(-?\d+)(\d{3})", rf"\g<1>{sep}\g<2>", orig)
146146
if orig == new:
147147
return new
148-
else:
149-
return intcomma(new)
148+
149+
return intcomma(new)
150150

151151

152152
powers = [10**x for x in (3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 100)]
@@ -213,27 +213,26 @@ def intword(value: NumberOrString, format: str = "%.1f") -> str:
213213

214214
if value < powers[0]:
215215
return negative_prefix + str(value)
216-
for ordinal, power in enumerate(powers[1:], 1):
216+
217+
for ordinal_, power in enumerate(powers[1:], 1):
217218
if value < power:
218-
chopped = value / float(powers[ordinal - 1])
219+
chopped = value / float(powers[ordinal_ - 1])
219220
if float(format % chopped) == float(10**3):
220-
chopped = value / float(powers[ordinal])
221-
singular, plural = human_powers[ordinal]
222-
return (
223-
negative_prefix
224-
+ " ".join(
225-
[format, _ngettext(singular, plural, math.ceil(chopped))]
226-
)
227-
) % chopped
228-
else:
229-
singular, plural = human_powers[ordinal - 1]
221+
chopped = value / float(powers[ordinal_])
222+
singular, plural = human_powers[ordinal_]
230223
return (
231224
negative_prefix
232225
+ " ".join(
233226
[format, _ngettext(singular, plural, math.ceil(chopped))]
234227
)
235228
) % chopped
236229

230+
singular, plural = human_powers[ordinal_ - 1]
231+
return (
232+
negative_prefix
233+
+ " ".join([format, _ngettext(singular, plural, math.ceil(chopped))])
234+
) % chopped
235+
237236
return negative_prefix + str(value)
238237

239238

@@ -334,10 +333,11 @@ def fractional(value: NumberOrString) -> str:
334333
# this means that an integer was passed in
335334
# (or variants of that integer like 1.0000)
336335
return f"{whole_number:.0f}"
337-
elif not whole_number:
336+
337+
if not whole_number:
338338
return f"{numerator:.0f}/{denominator:.0f}"
339-
else:
340-
return f"{whole_number:.0f} {numerator:.0f}/{denominator:.0f}"
339+
340+
return f"{whole_number:.0f} {numerator:.0f}/{denominator:.0f}"
341341

342342

343343
def scientific(value: NumberOrString, precision: int = 2) -> str:
@@ -464,13 +464,14 @@ def clamp(
464464

465465
if isinstance(format, str):
466466
return token + format.format(value)
467-
elif callable(format):
467+
468+
if callable(format):
468469
return token + format(value)
469-
else:
470-
raise ValueError(
471-
"Invalid format. Must be either a valid formatting string, or a function "
472-
"that accepts value and returns a string."
473-
)
470+
471+
raise ValueError(
472+
"Invalid format. Must be either a valid formatting string, or a function "
473+
"that accepts value and returns a string."
474+
)
474475

475476

476477
def metric(value: float, unit: str = "", precision: int = 3) -> str:
@@ -515,15 +516,15 @@ def metric(value: float, unit: str = "", precision: int = 3) -> str:
515516

516517
value /= 10 ** (exponent // 3 * 3)
517518
if exponent >= 3:
518-
ordinal = "kMGTPEZY"[exponent // 3 - 1]
519+
ordinal_ = "kMGTPEZY"[exponent // 3 - 1]
519520
elif exponent < 0:
520-
ordinal = "mμnpfazy"[(-exponent - 1) // 3]
521+
ordinal_ = "mμnpfazy"[(-exponent - 1) // 3]
521522
else:
522-
ordinal = ""
523+
ordinal_ = ""
523524
value_ = format(value, ".%if" % (precision - (exponent % 3) - 1))
524-
if not (unit or ordinal) or unit in ("°", "′", "″"):
525+
if not (unit or ordinal_) or unit in ("°", "′", "″"):
525526
space = ""
526527
else:
527528
space = " "
528529

529-
return f"{value_}{space}{ordinal}{unit}"
530+
return f"{value_}{space}{ordinal_}{unit}"

src/humanize/time.py

Lines changed: 54 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,8 @@ def naturaldelta(
151151
_ngettext("%d microsecond", "%d microseconds", delta.microseconds)
152152
% delta.microseconds
153153
)
154-
elif min_unit == Unit.MILLISECONDS or (
154+
155+
if min_unit == Unit.MILLISECONDS or (
155156
min_unit == Unit.MICROSECONDS and 1000 <= delta.microseconds < 1_000_000
156157
):
157158
milliseconds = delta.microseconds / 1000
@@ -160,47 +161,59 @@ def naturaldelta(
160161
% milliseconds
161162
)
162163
return _("a moment")
163-
elif seconds == 1:
164+
165+
if seconds == 1:
164166
return _("a second")
165-
elif seconds < 60:
167+
168+
if seconds < 60:
166169
return _ngettext("%d second", "%d seconds", seconds) % seconds
167-
elif 60 <= seconds < 120:
170+
171+
if 60 <= seconds < 120:
168172
return _("a minute")
169-
elif 120 <= seconds < 3600:
173+
174+
if 120 <= seconds < 3600:
170175
minutes = seconds // 60
171176
return _ngettext("%d minute", "%d minutes", minutes) % minutes
172-
elif 3600 <= seconds < 3600 * 2:
177+
178+
if 3600 <= seconds < 3600 * 2:
173179
return _("an hour")
174-
elif 3600 < seconds:
180+
181+
if 3600 < seconds:
175182
hours = seconds // 3600
176183
return _ngettext("%d hour", "%d hours", hours) % hours
184+
177185
elif years == 0:
178186
if days == 1:
179187
return _("a day")
188+
180189
if not use_months:
181190
return _ngettext("%d day", "%d days", days) % days
182-
else:
183-
if not num_months:
184-
return _ngettext("%d day", "%d days", days) % days
185-
elif num_months == 1:
186-
return _("a month")
187-
else:
188-
return _ngettext("%d month", "%d months", num_months) % num_months
191+
192+
if not num_months:
193+
return _ngettext("%d day", "%d days", days) % days
194+
195+
if num_months == 1:
196+
return _("a month")
197+
198+
return _ngettext("%d month", "%d months", num_months) % num_months
199+
189200
elif years == 1:
190201
if not num_months and not days:
191202
return _("a year")
192-
elif not num_months:
203+
204+
if not num_months:
193205
return _ngettext("1 year, %d day", "1 year, %d days", days) % days
194-
elif use_months:
206+
207+
if use_months:
195208
if num_months == 1:
196209
return _("1 year, 1 month")
197-
else:
198-
return (
199-
_ngettext("1 year, %d month", "1 year, %d months", num_months)
200-
% num_months
201-
)
202-
else:
203-
return _ngettext("1 year, %d day", "1 year, %d days", days) % days
210+
211+
return (
212+
_ngettext("1 year, %d month", "1 year, %d months", num_months)
213+
% num_months
214+
)
215+
216+
return _ngettext("1 year, %d day", "1 year, %d days", days) % days
204217

205218
return _ngettext("%d year", "%d years", years).replace("%d", "%s") % intcomma(years)
206219

@@ -265,12 +278,16 @@ def naturalday(value: dt.date | dt.datetime, format: str = "%b %d") -> str:
265278
# Date arguments out of range
266279
return str(value)
267280
delta = value - dt.date.today()
281+
268282
if delta.days == 0:
269283
return _("today")
270-
elif delta.days == 1:
284+
285+
if delta.days == 1:
271286
return _("tomorrow")
272-
elif delta.days == -1:
287+
288+
if delta.days == -1:
273289
return _("yesterday")
290+
274291
return value.strftime(format)
275292

276293

@@ -323,10 +340,11 @@ def _quotient_and_remainder(
323340
"""
324341
if unit == minimum_unit:
325342
return value / divisor, 0
326-
elif unit in suppress:
343+
344+
if unit in suppress:
327345
return 0, value
328-
else:
329-
return divmod(value, divisor)
346+
347+
return divmod(value, divisor)
330348

331349

332350
def _carry(
@@ -361,10 +379,11 @@ def _carry(
361379
"""
362380
if unit == min_unit:
363381
return value1 + value2 / ratio, 0
364-
elif unit in suppress:
382+
383+
if unit in suppress:
365384
return 0, value2 + value1 * ratio
366-
else:
367-
return value1, value2
385+
386+
return value1, value2
368387

369388

370389
def _suitable_minimum_unit(min_unit: Unit, suppress: typing.Iterable[Unit]) -> Unit:
@@ -405,16 +424,16 @@ def _suppress_lower_units(min_unit: Unit, suppress: typing.Iterable[Unit]) -> se
405424
['MICROSECONDS', 'MILLISECONDS', 'DAYS']
406425
"""
407426
suppress = set(suppress)
408-
for u in Unit:
409-
if u == min_unit:
427+
for unit in Unit:
428+
if unit == min_unit:
410429
break
411-
suppress.add(u)
430+
suppress.add(unit)
412431

413432
return suppress
414433

415434

416435
def precisedelta(
417-
value: dt.timedelta | int,
436+
value: dt.timedelta | int | None,
418437
minimum_unit: str = "seconds",
419438
suppress: typing.Iterable[str] = (),
420439
format: str = "%0.2f",

tests/test_i18n.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ def test_default_locale_path_null__file__() -> None:
158158
def test_default_locale_path_undefined__file__() -> None:
159159
i18n = importlib.import_module("humanize.i18n")
160160
del i18n.__file__
161-
i18n._get_default_locale_path() is None
161+
assert i18n._get_default_locale_path() is None
162162

163163

164164
class TestActivate:

tests/test_time.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,8 @@ def test_precisedelta_suppress_units(
629629

630630

631631
def test_precisedelta_bogus_call() -> None:
632+
assert humanize.precisedelta(None) == "None"
633+
632634
with pytest.raises(ValueError):
633635
humanize.precisedelta(1, minimum_unit="years", suppress=["years"])
634636

@@ -640,7 +642,6 @@ def test_time_unit() -> None:
640642
years, minutes = time.Unit["YEARS"], time.Unit["MINUTES"]
641643
assert minutes < years
642644
assert years > minutes
643-
assert minutes == minutes
644645

645646
with pytest.raises(TypeError):
646647
_ = years < "foo"

0 commit comments

Comments
 (0)