commit 2fce9d7b05e3f5d324b64d1e3339eda12590123a
parent 4dc017b801b785a9248262e31ad0231743982e14
Author: nolash <dev@holbrook.no>
Date: Fri, 7 Jan 2022 12:13:41 +0000
Add free exponentional number parser
Diffstat:
5 files changed, 26 insertions(+), 11 deletions(-)
diff --git a/CHANGELOG b/CHANGELOG
@@ -0,0 +1,4 @@
+- 0.2.0
+ * Add free number exponential number parser
+- 0.1.0
+ * Add number parser for common thousands step postfixes
diff --git a/aiee/numbers.py b/aiee/numbers.py
@@ -1,12 +1,12 @@
# standard imports
import re
-re_number_postfix = r'^(\d+(\.\d+)?)([kmgtpezy]?)$'
+re_number_postfix = r'^(\d+(\.\d+)?)(([kmgtpezyE])(\d+)?)?$'
def postfix_to_int(s):
s = s.replace(' ', '')
m = re.match(re_number_postfix, s)
if not m:
- raise ValueError('invalid postfix expansion value')
+ raise ValueError('invalid postfix expansion value: {}'.format(s))
v = 0
d = 0
p = 0
@@ -20,21 +20,23 @@ def postfix_to_int(s):
p -= len(f)
v = int(n + f)
- if m[3] == 'k':
+ if m[4] == 'k':
p += 3
- elif m[3] == 'm':
+ elif m[4] == 'm':
p += 6
- elif m[3] == 'g':
+ elif m[4] == 'g':
p += 9
- elif m[3] == 't':
+ elif m[4] == 't':
p += 12
- elif m[3] == 'p':
+ elif m[4] == 'p':
p += 15
- elif m[3] == 'e':
+ elif m[4] == 'e':
p += 18
- elif m[3] == 'z':
+ elif m[4] == 'z':
p += 21
- elif m[3] == 'y':
+ elif m[4] == 'y':
p += 24
+ elif m[4] == 'E':
+ p += int(m[5])
return v * (10 ** p)
diff --git a/aiee/runnable/main.py b/aiee/runnable/main.py
@@ -0,0 +1,5 @@
+import sys
+
+from aiee.numbers import postfix_to_int
+
+print(postfix_to_int(sys.argv[1]))
diff --git a/setup.cfg b/setup.cfg
@@ -1,6 +1,6 @@
[metadata]
name = aiee
-version = 0.1.0
+version = 0.2.0
description = Common command line interfacing utils
author = Louis Holbrook
author_email = dev@holbrook.no
diff --git a/tests/test_numbers.py b/tests/test_numbers.py
@@ -33,6 +33,10 @@ class TestNumbers(unittest.TestCase):
x = v[1] * (10 ** ((i * 3) - v[2]))
self.assertEqual(x, r)
+ r = postfix_to_int('42E11')
+ x = 42 * (10 ** 11)
+ self.assertEqual(x, r)
+
if __name__ == '__main__':
unittest.main()