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

Process ANSWER-style defaults in apply_composite_tag #234

Merged
merged 5 commits into from
Mar 24, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
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 xl2times/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,11 @@ def convert_xl_to_times(
transforms.generate_dummy_processes,
transforms.process_time_slices,
transforms.process_transform_table_variants,
transforms.apply_tag_specified_defaults,
transforms.process_transform_tables,
transforms.process_tradelinks,
transforms.process_processes,
transforms.process_topology,
transforms.apply_tag_specified_defaults,
transforms.process_flexible_import_tables, # slow
transforms.process_user_constraint_tables,
transforms.process_commodity_emissions,
Expand Down
35 changes: 30 additions & 5 deletions xl2times/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,38 @@ def apply_composite_tag(table: datatypes.EmbeddedXlTable) -> datatypes.EmbeddedX
and table tag simplified.
"""
if table.defaults:
varname = table.defaults
defaults = table.defaults
df = table.dataframe
if "attribute" in df.columns:
df["attribute"] = df["attribute"].fillna(varname)
# Check for ANSWER-style defaults
if "=" in defaults:
# Split multiple comma-separated defaults
if "," in defaults:
defaults = defaults.split(",")
else:
# Make it a list
defaults = list([defaults])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
defaults = list([defaults])
defaults = [defaults]

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed the if else statemement, because str.split() returns a list.

# Check whether there are invalid values on the list
invalid_defaults = [default for default in defaults if "=" not in default]
if invalid_defaults:
logger.warning(
f"Expected ANSWER-style defaults, got {invalid_defaults}"
)
defaults = [default.split("=") for default in defaults]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
defaults = [default.split("=") for default in defaults]
defaults = [default.split("=") for default in defaults if '=' in defaults]

Perhaps we want to do this for the non invalid defaults?

# TODO: check whether a column is allowed in a particular table type
for col, val in defaults:
colname = col.lower()
if colname in df.columns:
df[colname] = df[colname].fillna(val.upper())
else:
df[colname] = val.upper()
return replace(table, dataframe=df)
else:
df["attribute"] = varname
return replace(table, dataframe=df)
# TODO: Resolve the default value (it doesn't have to be an attribute)
if "attribute" in df.columns:
df["attribute"] = df["attribute"].fillna(defaults)
else:
df["attribute"] = defaults
return replace(table, dataframe=df)
else:
return table

Expand Down
Loading