10 def abbreviate_time(s):
11 def _plural(count, unit):
14 return "%d %s" % (count, unit)
15 return "%d %ss" % (count, unit)
19 return _plural(s, "second")
21 return _plural(s/60, "minute")
23 return _plural(s/HOUR, "hour")
25 return _plural(s/DAY, "day")
27 return _plural(s/MONTH, "month")
28 return _plural(s/YEAR, "year")
30 def abbreviate_space(s, SI=True):
40 return "%.2f %s%s" % (count, suffix, isuffix)
42 if s < 1024: # 1000-1023 get emitted as bytes, even in SI mode
47 return r(s/(U*U), "M")
49 return r(s/(U*U*U), "G")
51 return r(s/(U*U*U*U), "T")
52 return r(s/(U*U*U*U*U), "P")
54 def abbreviate_space_both(s):
55 return "(%s, %s)" % (abbreviate_space(s, True),
56 abbreviate_space(s, False))
58 def parse_abbreviated_size(s):
59 if s is None or s == "":
61 m = re.match(r"^(\d+)([kKmMgG]?[iB]?[bB]?)$", s)
63 raise ValueError("unparseable value %s" % s)
64 number, suffix = m.groups()
65 suffix = suffix.upper()
66 if suffix.endswith("B"):
72 "G": 1000 * 1000 * 1000,
77 return int(number) * multiplier