Skip to content

sortable_pack

sortable_pack

Sortable items packing utilities.

Classes

SortablePack

Bases: Packable

A packable for managing sortables data.

SortablePack handles the extraction, processing, and application of sortable data for scenarios.

Functions
import_scenario_specific_sheet
import_scenario_specific_sheet(excel_file, sheet_name, scenario, update_set=None)

Import sortables from a scenario-specific sheet.

Source code in src/pyetm/models/packables/sortable_pack.py
def import_scenario_specific_sheet(
    self,
    excel_file: pd.ExcelFile,
    sheet_name: str,
    scenario: Any,
    update_set: Optional[set[str]] = None,
) -> None:
    """Import sortables from a scenario-specific sheet."""
    df = excel_utils.parse_excel_sheet(excel_file, sheet_name, header=None)
    if df is not None and not df.empty:
        self.process_single_scenario_sortables(scenario, df, update_set)
process_single_scenario_sortables
process_single_scenario_sortables(scenario, df, update_set=None)

Process sortables data for a single scenario.

Source code in src/pyetm/models/packables/sortable_pack.py
def process_single_scenario_sortables(
    self, scenario: Any, df: pd.DataFrame, update_set: Optional[set[str]] = None
) -> None:
    """Process sortables data for a single scenario."""
    normalized_data = excel_utils.normalize_sheet(
        df,
        helper_names={"sortables", "hour", "index"},
        reset_index=True,
        rename_map={"heat_network": "heat_network_lt"},
    )

    if normalized_data is None or normalized_data.empty:
        return

    self.apply_sortables_to_scenario(scenario, normalized_data, update_set)
apply_sortables_to_scenario
apply_sortables_to_scenario(scenario, data, update_set=None)

Apply sortables data to scenario with error handling.

Source code in src/pyetm/models/packables/sortable_pack.py
def apply_sortables_to_scenario(
    self, scenario: Any, data: pd.DataFrame, update_set: Optional[set[str]] = None
) -> None:
    """Apply sortables data to scenario with error handling."""
    skip_upload = not self._should_include_upload(update_set)

    try:
        scenario.set_sortables_from_dataframe(data, skip_upload=skip_upload)
        self.log_scenario_warnings(scenario, "_sortables", "Sortables")
    except Exception as e:
        logger.warning("Failed processing sortables for '%s': %s", scenario.identifier(), e)
from_dataframe
from_dataframe(df, update_set=None)

Unpack and update sortables for each scenario from the sheet.

Source code in src/pyetm/models/packables/sortable_pack.py
def from_dataframe(self, df: pd.DataFrame, update_set: Optional[Set[str]] = None) -> None:
    """Unpack and update sortables for each scenario from the sheet."""
    if df is None or getattr(df, "empty", False):
        return
    try:
        df = self._normalize_single_header_sheet(
            df,
            helper_columns={"sortables"},
            drop_empty=True,
            reset_index=False,
        )
    except Exception as e:
        logger.warning("Failed to normalize sortables sheet: %s", e)
        return
    if df is None or df.empty:
        return

    def _apply(scenario: Any, block: pd.DataFrame) -> None:
        scenario.set_sortables_from_dataframe(block)
        self.log_scenario_warnings(scenario, "_sortables", "Sortables")

    if isinstance(df.columns, pd.MultiIndex):
        self.apply_identifier_blocks(df, _apply)
    else:
        for scenario in self.scenarios:
            _apply(scenario, df)