Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(tmp): debug lines #371

Merged
merged 19 commits into from
Sep 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
0c75e56
chore(tmp): debug lines
Avantol13-machine-user Aug 31, 2022
6074349
chore(tmp): debug lines
Avantol13-machine-user Aug 31, 2022
c92a218
chore(tmp): debug lines
Avantol13-machine-user Aug 31, 2022
7ff4cd0
chore(tmp): debug lines
Avantol13-machine-user Aug 31, 2022
a1f24e6
fix(transform): use provided type as contents of list
Avantol13-machine-user Aug 31, 2022
04dae3e
chore(tmp): try getting more info
Avantol13-machine-user Aug 31, 2022
44bd231
chore(tmp): debug lines
Avantol13-machine-user Aug 31, 2022
6b8ffa0
chore(tmp): debug
Avantol13-machine-user Aug 31, 2022
b896e4e
fix(transform): infer list of ints if all items can be ints
Avantol13-machine-user Aug 31, 2022
4d2df64
fix(transform): infer list of ints if all items can be ints
Avantol13-machine-user Aug 31, 2022
6ba6257
fix(cast): convert from float to int
Avantol13-machine-user Aug 31, 2022
446233a
fix(transform): infer list of ints if all items can be ints
Avantol13-machine-user Aug 31, 2022
d731dfb
chore(logs): add another log
Avantol13-machine-user Aug 31, 2022
83f7320
chore(logs): add another log
Avantol13-machine-user Aug 31, 2022
3a0cc6d
chore(logs): add another log
Avantol13-machine-user Aug 31, 2022
e2367cb
chore(logs): add another log
Avantol13-machine-user Aug 31, 2022
9dd7126
chore(logs): add another log and refactor
Avantol13-machine-user Aug 31, 2022
c054e40
fix(transform): actually check item not value
Avantol13-machine-user Aug 31, 2022
2801200
chore(logs): cleanup
Avantol13-machine-user Aug 31, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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