diff --git a/resources/home/dnanexus/generate_workbook/tests/test_utils.py b/resources/home/dnanexus/generate_workbook/tests/test_utils.py index 6c436cb5..3821e1c9 100644 --- a/resources/home/dnanexus/generate_workbook/tests/test_utils.py +++ b/resources/home/dnanexus/generate_workbook/tests/test_utils.py @@ -11,10 +11,14 @@ def test_is_numeric(): """ Tests that all forms of numerical value are correctly identified as - being numeric + being numeric, and things we don't want to are treat as non-numeric """ - numeric_values = ['5', '5.5', '-0.1', '1e-5'] - non_numeric_values = ['5,6', '5, 6', '.', '3/4', ] + numeric_values = ['5', '5.5', '-0.1', '1e-5', '.1'] + + non_numeric_values = [ + '5,6', '5, 6', '.', '3/4', '1..1', '1.1.1', '5ee5', '123e', '-e1', + '.e1', 'e1', '1.1.1', '01', '0001' + ] assert all([is_numeric(x) for x in numeric_values]), ( 'some numeric values do not properly evaluate to being numeric' diff --git a/resources/home/dnanexus/generate_workbook/utils/utils.py b/resources/home/dnanexus/generate_workbook/utils/utils.py index e325548e..f793bc66 100644 --- a/resources/home/dnanexus/generate_workbook/utils/utils.py +++ b/resources/home/dnanexus/generate_workbook/utils/utils.py @@ -1,3 +1,6 @@ +from re import L + + def is_numeric(value:str) -> bool: """ Returns true if given value is in some form numeric @@ -12,5 +15,17 @@ def is_numeric(value:str) -> bool: bool True if value can be numeric """ - return str(value).lstrip('-').replace('.', '').replace( - 'e-', '', 1).replace('e', '').isdigit() \ No newline at end of file + try: + if str(value)[0] == '0' and not str(value)[1] == '.': + # catch things such as 01 which aren't real numbers + return False + except Exception: + # could raise lots of errors (i.e IndexErrors from slicing) + # lazily catch all and continue + pass + + try: + float(value) + return True + except ValueError: + return False