Skip to content

Commit

Permalink
chore(tmp): debug lines (#371)
Browse files Browse the repository at this point in the history
* chore(tmp): debug lines

* chore(tmp): debug lines

* chore(tmp): debug lines

* chore(tmp): debug lines

* fix(transform): use provided type as contents of list

* chore(tmp): try getting more info

* chore(tmp): debug lines

* chore(tmp): debug

* fix(transform): infer list of ints if all items can be ints

* fix(transform): infer list of ints if all items can be ints

* fix(cast): convert from float to int

* fix(transform): infer list of ints if all items can be ints

* chore(logs): add another log

* chore(logs): add another log

* chore(logs): add another log

* chore(logs): add another log

* chore(logs): add another log and refactor

* fix(transform): actually check item not value

* chore(logs): cleanup

Co-authored-by: Alexander VT <[email protected]>
  • Loading branch information
Avantol13 and Avantol13-machine-user authored Sep 1, 2022
1 parent 0258577 commit d52b058
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ repos:
- id: no-commit-to-branch
args: [--branch, develop, --branch, master, --pattern, release/.*]
- repo: https://github.com/psf/black
rev: 20.8b1
rev: 22.3.0
hooks:
- id: black
55 changes: 49 additions & 6 deletions sheepdog/utils/transforms/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def parse_bool_from_string(value):
return mapping.get(strip(value).lower(), value)


def parse_list_from_string(value):
def parse_list_from_string(value, list_type=None):
"""
Handle array fields by converting them to a list.
Try to cast to float to handle arrays of numbers.
Expand All @@ -39,11 +39,38 @@ def parse_list_from_string(value):
1,2,3 -> [1,2,3]
"""
items = [x.strip() for x in value.split(",")]

all_ints = True
try:
items = [float(x) for x in items]
except ValueError:
pass # not an array of numbers
return items
# TODO: Actually pass in and use list_type as the expected type
# and don't try to infer it this way.
for item in items:
if not float(item).is_integer():
all_ints = False
break
except ValueError as exc:
current_app.logger.warning(
f"list of values {items} are likely NOT ints or floats so we're leaving "
f"them as-is. Exception: {exc}"
)
return items

if all_ints:
current_app.logger.warning(
f"list of values {items} could all be integers, so we are ASSUMING they "
"are instead of defaulting to float."
)
# all can be ints, infer `int` as correct type
new_items = [int(float(item)) for item in items]
else:
current_app.logger.warning(
f"list of values {items} are NOT all integers, so we are ASSUMING they "
"they are all float by default."
)
# default to float for backwards compatibility
new_items = [float(item) for item in items]

return new_items


def set_row_type(row):
Expand Down Expand Up @@ -207,13 +234,29 @@ def value_to_list_value(self, cls, link_name, prop, value):

@staticmethod
def get_converted_type_from_list(cls, prop_name, value):
current_app.logger.debug(f"cls.__pg_properties__:{cls.__pg_properties__}")
types = cls.__pg_properties__.get(prop_name, (str,))
current_app.logger.debug(f"types:{types}")
value_type = types[0]

property_list = cls.get_property_list()
current_app.logger.debug(f"property_list:{property_list}")

# TODO: list_type is not used b/c for some reason it's always
# str even if the dictionary says it's an array of ints
list_type = None
if len(types) > 1:
list_type = types[1]

current_app.logger.debug(f"prop_name:{prop_name}")
current_app.logger.debug(f"value:{value}")
current_app.logger.debug(f"value_type:{value_type}")

try:
if value_type == bool:
return parse_bool_from_string(value)
elif value_type == list:
return parse_list_from_string(value)
return parse_list_from_string(value, list_type=list_type)
elif value_type == float:
if float(value).is_integer():
return int(float(value))
Expand Down

0 comments on commit d52b058

Please sign in to comment.