aboutsummaryrefslogtreecommitdiff
path: root/src/utils
diff options
context:
space:
mode:
authorGravatar jesopo2018-10-12 11:16:39 +0100
committerGravatar jesopo2018-10-12 11:16:39 +0100
commit9771fc9f7adccfacd582caddb79c04696be54864 (patch)
treece1768fde756ac2630bbf1fd916e1a33b845a34f /src/utils
parentCheck a channel's automode when automode is turned on in modules/auto_mode.py (diff)
signature
Add utils.parse_number, to turn 1k/1m/1b in to 1_000/1_000_000/1_000_000_000
Diffstat (limited to 'src/utils')
-rw-r--r--src/utils/__init__.py19
1 files changed, 19 insertions, 0 deletions
diff --git a/src/utils/__init__.py b/src/utils/__init__.py
index a700dd95..0cb131ff 100644
--- a/src/utils/__init__.py
+++ b/src/utils/__init__.py
@@ -85,6 +85,25 @@ def to_pretty_time(total_seconds, minimum_unit=UNIT_SECOND, max_units=6):
units += 1
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_FALSE = ["false", "no", "off", "n"]
def bool_or_none(s):