Add utils.parse_number, to turn 1k/1m/1b in to 1_000/1_000_000/1_000_000_000
This commit is contained in:
parent
e96c983dca
commit
9771fc9f7a
1 changed files with 19 additions and 0 deletions
|
@ -85,6 +85,25 @@ def to_pretty_time(total_seconds, minimum_unit=UNIT_SECOND, max_units=6):
|
||||||
units += 1
|
units += 1
|
||||||
return out
|
return out
|
||||||
|
|
||||||
|
def parse_number(s):
|
||||||
|
if s.isdigit():
|
||||||
|
return s
|
||||||
|
|
||||||
|
unit = s[-1].lower()
|
||||||
|
number = s[:-1]
|
||||||
|
if not number.isdigit():
|
||||||
|
raise ValueError("Invalid format '%s' passed to parse_number")
|
||||||
|
|
||||||
|
if unit == "k":
|
||||||
|
number *= 1_000
|
||||||
|
elif unit == "m":
|
||||||
|
number *= 1_000_000
|
||||||
|
elif unit == "b":
|
||||||
|
number *= 1_000_000_000
|
||||||
|
else:
|
||||||
|
raise ValueError("Unknown unit '%s' given to parse_number")
|
||||||
|
return str(number)
|
||||||
|
|
||||||
IS_TRUE = ["true", "yes", "on", "y"]
|
IS_TRUE = ["true", "yes", "on", "y"]
|
||||||
IS_FALSE = ["false", "no", "off", "n"]
|
IS_FALSE = ["false", "no", "off", "n"]
|
||||||
def bool_or_none(s):
|
def bool_or_none(s):
|
||||||
|
|
Loading…
Reference in a new issue