Skip to content

Commit

Permalink
Merge pull request #113 from eastgenomics/improve_url_value_typing
Browse files Browse the repository at this point in the history
better specificity of utils.is_numeric()
  • Loading branch information
mattgarner authored Jul 13, 2022
2 parents f61b0ce + c7ec477 commit 9a83593
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
10 changes: 7 additions & 3 deletions resources/home/dnanexus/generate_workbook/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
19 changes: 17 additions & 2 deletions resources/home/dnanexus/generate_workbook/utils/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from re import L


def is_numeric(value:str) -> bool:
"""
Returns true if given value is in some form numeric
Expand All @@ -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()
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

0 comments on commit 9a83593

Please sign in to comment.