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+)([KMGTPE]?[I]?[B]?)$", s.upper())
63 raise ValueError("unparseable value %s" % s)
64 number, suffix = m.groups()
65 if suffix.endswith("B"):
71 "G": 1000 * 1000 * 1000,
72 "T": 1000 * 1000 * 1000 * 1000,
73 "P": 1000 * 1000 * 1000 * 1000 * 1000,
74 "E": 1000 * 1000 * 1000 * 1000 * 1000 * 1000,
77 "GI": 1024 * 1024 * 1024,
78 "TI": 1024 * 1024 * 1024 * 1024,
79 "PI": 1024 * 1024 * 1024 * 1024 * 1024,
80 "EI": 1024 * 1024 * 1024 * 1024 * 1024 * 1024,
82 return int(number) * multiplier