Skip to content

Commit

Permalink
Remove get_data_to_set_scenario_alternatives
Browse files Browse the repository at this point in the history
  • Loading branch information
manuelma committed Oct 5, 2023
1 parent f3b9984 commit 9e4f5eb
Showing 1 changed file with 0 additions and 47 deletions.
47 changes: 0 additions & 47 deletions spinedb_api/db_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -731,53 +731,6 @@ def update_ext_entity_metadata(self, *items, **kwargs):
def update_ext_parameter_value_metadata(self, *items, **kwargs):
return self._update_ext_item_metadata("parameter_value_metadata", *items, **kwargs)

def get_data_to_set_scenario_alternatives(self, *scenarios, strict=True):
"""Returns data to add and remove, in order to set wide scenario alternatives.
Args:
*scenarios: One or more wide scenario :class:`dict` objects to set.
Each item must include the following keys:
- "id": integer scenario id
- "alternative_id_list": list of alternative ids for that scenario
Returns
list: scenario_alternative :class:`dict` objects to add.
set: integer scenario_alternative ids to remove
"""

def _is_equal(to_add, to_rm):
return all(to_rm[k] == v for k, v in to_add.items())

scen_alts_to_add = []
scen_alt_ids_to_remove = {}
errors = []
for scen in scenarios:
current_scen = self.mapped_table("scenario").find_item(scen)
if current_scen is None:
error = f"no scenario matching {scen} to set alternatives for"
if strict:
raise SpineIntegrityError(error)
errors.append(error)
continue
for k, alternative_id in enumerate(scen.get("alternative_id_list", ())):
item_to_add = {"scenario_id": current_scen["id"], "alternative_id": alternative_id, "rank": k + 1}
scen_alts_to_add.append(item_to_add)
for k, alternative_name in enumerate(scen.get("alternative_name_list", ())):
item_to_add = {"scenario_id": current_scen["id"], "alternative_name": alternative_name, "rank": k + 1}
scen_alts_to_add.append(item_to_add)
for alternative_id in current_scen["alternative_id_list"]:
scen_alt = {"scenario_id": current_scen["id"], "alternative_id": alternative_id}
current_scen_alt = self.mapped_table("scenario_alternative").find_item(scen_alt)
scen_alt_ids_to_remove[current_scen_alt["id"]] = current_scen_alt
# Remove items that are both to add and to remove
for id_, to_rm in list(scen_alt_ids_to_remove.items()):
i = next((i for i, to_add in enumerate(scen_alts_to_add) if _is_equal(to_add, to_rm)), None)
if i is not None:
del scen_alts_to_add[i]
del scen_alt_ids_to_remove[id_]
return scen_alts_to_add, set(scen_alt_ids_to_remove), errors

def remove_unused_metadata(self):
used_metadata_ids = set()
for x in self.mapped_table("entity_metadata").valid_values():
Expand Down

0 comments on commit 9e4f5eb

Please sign in to comment.