Skip to content

Commit

Permalink
WIP on #773.
Browse files Browse the repository at this point in the history
  • Loading branch information
mjordan committed May 14, 2024
1 parent fe70000 commit 4eb35e7
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 9 deletions.
5 changes: 5 additions & 0 deletions tests/unit_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -844,6 +844,11 @@ def test_value_is_numeric(self):
res = workbench_utils.value_is_numeric(value)
self.assertTrue(res, "Value " + str(value) + " is not numeric.")

values = ["200.23", "0.5", 999.999]
for value in values:
res = workbench_utils.value_is_numeric(value, allow_decimals=True)
self.assertTrue(res, "Value " + str(value) + " is not numeric.")

def test_value_is_not_numeric(self):
values = ["n200", False, "999-1000"]
for value in values:
Expand Down
56 changes: 50 additions & 6 deletions workbench_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,13 @@ def create(self, config, field_definitions, entity, row, field_name):
):
field_values.append({"value": subvalue, "format": text_format})
else:
if field_definitions[field_name]["field_type"] == "integer":
if field_definitions[field_name][
"field_type"
] == "integer" and value_is_numeric(subvalue):
subvalue = int(subvalue)
if field_definitions[field_name]["field_type"] == "float":
if field_definitions[field_name][
"field_type"
] == "float" and value_is_numeric(subvalue, allow_decimals=True):
subvalue = float(subvalue)
field_values.append({"value": subvalue})
field_values = self.dedupe_values(field_values)
Expand Down Expand Up @@ -163,9 +167,13 @@ def update(
{"value": subvalue, "format": text_format}
)
else:
if field_definitions[field_name]["field_type"] == "integer":
if field_definitions[field_name][
"field_type"
] == "integer" and value_is_numeric(subvalue):
subvalue = int(subvalue)
if field_definitions[field_name]["field_type"] == "float":
if field_definitions[field_name][
"field_type"
] == "float" and value_is_numeric(subvalue, allow_decimals=True):
subvalue = float(subvalue)
entity[field_name].append({"value": subvalue})
entity[field_name] = self.dedupe_values(entity[field_name])
Expand Down Expand Up @@ -199,9 +207,13 @@ def update(
):
field_values.append({"value": subvalue, "format": text_format})
else:
if field_definitions[field_name]["field_type"] == "integer":
if field_definitions[field_name][
"field_type"
] == "integer" and value_is_numeric(subvalue):
subvalue = int(subvalue)
if field_definitions[field_name]["field_type"] == "float":
if field_definitions[field_name][
"field_type"
] == "float" and value_is_numeric(subvalue, allow_decimals=True):
subvalue = float(subvalue)
entity[field_name].append({"value": subvalue})
field_values.append({"value": subvalue})
Expand Down Expand Up @@ -259,6 +271,38 @@ def remove_invalid_values(self, config, field_definitions, field_name, values):
)
logging.warning(message)
return valid_values
elif field_definitions[field_name]["field_type"] == "integer":
valid_values = list()
for subvalue in values:
if value_is_numeric(subvalue) is True:
valid_values.append(subvalue)
else:
message = (
'Value "'
+ subvalue
+ '" in field "'
+ field_name
+ '" is not a valid integer field value.'
)
logging.warning(message)
return valid_values
elif field_definitions[field_name]["field_type"] in ["decimal", "float"]:
valid_values = list()
for subvalue in values:
if value_is_numeric(subvalue, allow_decimals=True) is True:
valid_values.append(subvalue)
else:
message = (
'Value "'
+ subvalue
+ '" in field "'
+ field_name
+ '" is not a valid '
+ field_definitions[field_name]["field_type"]
+ " field value."
)
logging.warning(message)
return valid_values
elif field_definitions[field_name]["field_type"] == "list_string":
valid_values = list()
for subvalue in values:
Expand Down
23 changes: 20 additions & 3 deletions workbench_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6483,9 +6483,26 @@ def get_field_vocabularies(config, field_definitions, field_name):
return False


def value_is_numeric(value):
"""Tests to see if value is numeric."""
var = str(value)
def value_is_numeric(value, allow_decimals=False):
"""Tests to see if value is numeric."""

"""Parameters
----------
value : varies
The value to check. By design, we don't know what data type it is.
allow_decimals: boolean
Whether or not to allow '.' in the value. Decimal and float number types have decimals.
Returns
-------
boolean
"""
if allow_decimals is True:
if "." in str(value):
var = str(value).replace(".", "")
else:
var = str(value)

var = var.strip()
if var.isnumeric():
return True
Expand Down

0 comments on commit 4eb35e7

Please sign in to comment.