Skip to content

Commit

Permalink
lint applied
Browse files Browse the repository at this point in the history
  • Loading branch information
vargastat authored and MartinBelthle committed Dec 4, 2024
1 parent f9b161e commit 08c935d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 32 deletions.
61 changes: 34 additions & 27 deletions src/antares/service/local_services/binding_constraint_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,25 @@
BindingConstraintOperator,
BindingConstraintProperties,
BindingConstraintPropertiesLocal,
ClusterData,
ConstraintMatrixName,
ConstraintTerm, LinkData, ClusterData,
ConstraintTerm,
LinkData,
)
from antares.service.base_services import BaseBindingConstraintService
from antares.tools.ini_tool import IniFile, IniFileTypes
from antares.tools.matrix_tool import df_save
from antares.tools.time_series_tool import TimeSeriesFileType


def serialize_term_data(data: Union[LinkData,ClusterData], offset: Optional[int], weight: Optional[float]) -> Union[str, None]:
def serialize_term_data(
data: Union[LinkData, ClusterData], offset: Optional[int], weight: Optional[float]
) -> Union[str, None]:
"""
Serializes the term data to be correctly written in INI.
"""
if isinstance(data, LinkData):

if offset is not None:
if offset is not None:
return f"0.000000%{offset}"
if weight is not None:
return f"{weight}"
Expand All @@ -46,6 +49,7 @@ def serialize_term_data(data: Union[LinkData,ClusterData], offset: Optional[int]
else:
return None


class BindingConstraintLocalService(BaseBindingConstraintService):
def __init__(self, config: LocalConfiguration, study_name: str, **kwargs: Any) -> None:
super().__init__(**kwargs)
Expand All @@ -71,11 +75,10 @@ def create_binding_constraint(
constraint.properties = constraint.local_properties.yield_binding_constraint_properties()

current_ini_content = self.ini_file.ini_dict_binding_constraints or {}
if any(
values.get("name") == name
for values in current_ini_content.values()
):
raise BindingConstraintCreationError(constraint_name=name, message= f"A binding constraint with the name {name} already exists.")
if any(values.get("name") == name for values in current_ini_content.values()):
raise BindingConstraintCreationError(
constraint_name=name, message=f"A binding constraint with the name {name} already exists."
)

self._write_binding_constraint_ini(constraint.properties, name, name, terms)

Expand Down Expand Up @@ -118,11 +121,11 @@ def _check_if_empty_ts(time_step: BindingConstraintFrequency, time_series: Optio
return time_series if time_series is not None else pd.DataFrame(np.zeros([time_series_length, 1]))

def _write_binding_constraint_ini(
self,
properties: BindingConstraintProperties,
constraint_name: str,
constraint_id: str,
terms: Optional[list[ConstraintTerm]] = None,
self,
properties: BindingConstraintProperties,
constraint_name: str,
constraint_id: str,
terms: Optional[list[ConstraintTerm]] = None,
) -> None:
"""
Write or update a binding constraint in the INI file.
Expand All @@ -131,7 +134,6 @@ def _write_binding_constraint_ini(

current_ini_content = self.ini_file.ini_dict_binding_constraints or {}


existing_section = next(
(section for section, values in current_ini_content.items() if values.get("name") == constraint_name),
None,
Expand All @@ -142,8 +144,9 @@ def _write_binding_constraint_ini(
existing_terms = current_ini_content[existing_section]

# Serialize the terms data (this assumes you want to serialize LinkData or ClusterData in `terms`)
serialized_terms = {term.id: serialize_term_data(term.data, term.offset, term.weight) for term in terms} if terms else {}

serialized_terms = (
{term.id: serialize_term_data(term.data, term.offset, term.weight) for term in terms} if terms else {}
)

existing_terms.update(serialized_terms) # type: ignore
current_ini_content[existing_section] = existing_terms
Expand All @@ -152,18 +155,22 @@ def _write_binding_constraint_ini(
self.ini_file.ini_dict_binding_constraints = current_ini_content
self.ini_file.write_ini_file()
else:


terms_dict = {
term.id: ConstraintTerm(data=term.data, offset=term.offset, weight=term.weight)
if isinstance(term.data, (LinkData, ClusterData))
else term
for term in terms
} if terms else {}
terms_dict = (
{
term.id: ConstraintTerm(data=term.data, offset=term.offset, weight=term.weight)
if isinstance(term.data, (LinkData, ClusterData))
else term
for term in terms
}
if terms
else {}
)

full_properties = BindingConstraintPropertiesLocal(
constraint_name=constraint_name, constraint_id=constraint_id, terms=terms_dict,
**properties.model_dump()
constraint_name=constraint_name,
constraint_id=constraint_id,
terms=terms_dict,
**properties.model_dump(),
)

section_index = len(current_ini_content)
Expand Down
6 changes: 1 addition & 5 deletions src/antares/tools/ini_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

from pydantic import BaseModel

from antares.model.binding_constraint import ConstraintTerm
from antares.tools.custom_raw_config_parser import CustomRawConfigParser
from antares.tools.model_tools import filter_out_empty_model_fields

Expand Down Expand Up @@ -93,10 +92,7 @@ def ini_dict(self, new_ini_dict: dict[str, dict[str, str]]) -> None:

@property
def ini_dict_binding_constraints(self) -> dict[str, dict[str, str]]:
return {
section: dict(self._ini_contents[section])
for section in self._ini_contents.sections()
}
return {section: dict(self._ini_contents[section]) for section in self._ini_contents.sections()}

@ini_dict_binding_constraints.setter
def ini_dict_binding_constraints(self, new_ini_dict: dict[str, dict[str, str]]) -> None:
Expand Down

0 comments on commit 08c935d

Please sign in to comment.