Skip to content

Commit

Permalink
Type annotations added for abbreviate.py
Browse files Browse the repository at this point in the history
  • Loading branch information
danielzwlee committed Mar 9, 2023
1 parent 3b8ba8b commit 33c414f
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/allmydata/util/abbreviate.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@

import re
from datetime import timedelta
from typing import Union, Optional

HOUR = 3600
DAY = 24*3600
WEEK = 7*DAY
MONTH = 30*DAY
YEAR = 365*DAY

def abbreviate_time(s):
def abbreviate_time(s: timedelta) -> str:
"""
Given time in seconds (float or int) or timedelta, summarize as English by
returning unicode string.
Expand All @@ -42,7 +43,7 @@ def abbreviate_time(s):
else:
postfix = ' in the future'
s = -s
def _plural(count, unit):
def _plural(count: Union[float, int], unit: str) -> str:
count = int(count)
if count == 1:
return "%d %s%s" % (count, unit, postfix)
Expand All @@ -61,7 +62,7 @@ def _plural(count, unit):
return _plural(s / MONTH, "month")
return _plural(s / YEAR, "year")

def abbreviate_space(s, SI=True):
def abbreviate_space(s: Optional[int], SI: bool=True):
"""
Given size in bytes summarize as English by returning unicode string.
"""
Expand Down Expand Up @@ -90,11 +91,11 @@ def r(count, suffix):
return r(s/(U*U*U*U*U), "P")
return r(s/(U*U*U*U*U*U), "E")

def abbreviate_space_both(s):
def abbreviate_space_both(s: Optional[int]) -> str:
return "(%s, %s)" % (abbreviate_space(s, True),
abbreviate_space(s, False))

def parse_abbreviated_size(s):
def parse_abbreviated_size(s: Optional[str]) -> Optional[int]:
if s is None or s == "":
return None
m = re.match(r"^(\d+)([KMGTPE]?[I]?[B]?)$", s.upper())
Expand Down

0 comments on commit 33c414f

Please sign in to comment.