Skip to content

Commit

Permalink
linter
Browse files Browse the repository at this point in the history
  • Loading branch information
killian-scalian committed Sep 13, 2024
1 parent 599b93f commit f08aac1
Show file tree
Hide file tree
Showing 33 changed files with 1,003 additions and 334 deletions.
18 changes: 14 additions & 4 deletions src/antares/api_conf/request_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@

from antares.exceptions.exceptions import APIError

DATA_TYPE = Union[str, bytes, Mapping[str, Any], Iterable[Tuple[str, Optional[str]]], IO]
DATA_TYPE = Union[
str, bytes, Mapping[str, Any], Iterable[Tuple[str, Optional[str]]], IO
]


def _handle_exceptions(response: requests.Response) -> requests.Response:
Expand Down Expand Up @@ -46,16 +48,24 @@ def get(self, url: str, **kwargs: Any) -> requests.Response:
return _handle_exceptions(response)

def post(
self, url: str, data: Optional[DATA_TYPE] = None, json: Optional[Any] = None, **kwargs: Any
self,
url: str,
data: Optional[DATA_TYPE] = None,
json: Optional[Any] = None,
**kwargs: Any
) -> requests.Response:
response = self.session.post(url, data, json, **kwargs)
return _handle_exceptions(response)

def put(self, url: str, data: Optional[DATA_TYPE] = None, **kwargs: Any) -> requests.Response:
def put(
self, url: str, data: Optional[DATA_TYPE] = None, **kwargs: Any
) -> requests.Response:
response = self.session.put(url, data, **kwargs)
return _handle_exceptions(response)

def patch(self, url: str, data: Optional[DATA_TYPE] = None, **kwargs: Any) -> requests.Response:
def patch(
self, url: str, data: Optional[DATA_TYPE] = None, **kwargs: Any
) -> requests.Response:
response = self.session.patch(url, data, **kwargs)
return _handle_exceptions(response)

Expand Down
92 changes: 70 additions & 22 deletions src/antares/exceptions/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,18 @@ def __init__(self, link_name: str, message: str) -> None:

class ThermalCreationError(Exception):
def __init__(self, thermal_name: str, area_id: str, message: str) -> None:
self.message = f"Could not create the thermal cluster {thermal_name} inside area {area_id}: " + message
self.message = (
f"Could not create the thermal cluster {thermal_name} inside area {area_id}: "
+ message
)
super().__init__(self.message)


class ThermalPropertiesUpdateError(Exception):
def __init__(self, thermal_name: str, area_id: str, message: str) -> None:
self.message = (
f"Could not update properties for thermal cluster {thermal_name} inside area {area_id}: " + message
f"Could not update properties for thermal cluster {thermal_name} inside area {area_id}: "
+ message
)
super().__init__(self.message)

Expand All @@ -109,14 +113,18 @@ def __init__(self, area_id: str, message: str) -> None:

class RenewableCreationError(Exception):
def __init__(self, renewable_name: str, area_id: str, message: str) -> None:
self.message = f"Could not create the renewable cluster {renewable_name} inside area {area_id}: " + message
self.message = (
f"Could not create the renewable cluster {renewable_name} inside area {area_id}: "
+ message
)
super().__init__(self.message)


class RenewablePropertiesUpdateError(Exception):
def __init__(self, renewable_name: str, area_id: str, message: str) -> None:
self.message = (
f"Could not update properties for renewable cluster {renewable_name} inside area {area_id}: " + message
f"Could not update properties for renewable cluster {renewable_name} inside area {area_id}: "
+ message
)
super().__init__(self.message)

Expand All @@ -132,30 +140,40 @@ def __init__(self, area_id: str, renewable_names: List[str], message: str) -> No

class STStorageCreationError(Exception):
def __init__(self, st_storage_name: str, area_id: str, message: str) -> None:
self.message = f"Could not create the short term storage {st_storage_name} inside area {area_id}: " + message
self.message = (
f"Could not create the short term storage {st_storage_name} inside area {area_id}: "
+ message
)
super().__init__(self.message)


class STStoragePropertiesUpdateError(Exception):
def __init__(self, st_storage_name: str, area_id: str, message: str) -> None:
self.message = (
f"Could not update properties for short term storage {st_storage_name} inside area {area_id}: " + message
f"Could not update properties for short term storage {st_storage_name} inside area {area_id}: "
+ message
)
super().__init__(self.message)


class STStorageMatrixDownloadError(Exception):
def __init__(self, area_name: str, storage_name: str, matrix_name: str, message: str) -> None:
def __init__(
self, area_name: str, storage_name: str, matrix_name: str, message: str
) -> None:
self.message = (
f"Could not download {matrix_name} matrix for storage {storage_name} inside area {area_name}: " + message
f"Could not download {matrix_name} matrix for storage {storage_name} inside area {area_name}: "
+ message
)
super().__init__(self.message)


class STStorageMatrixUploadError(Exception):
def __init__(self, area_name: str, storage_name: str, matrix_name: str, message: str) -> None:
def __init__(
self, area_name: str, storage_name: str, matrix_name: str, message: str
) -> None:
self.message = (
f"Could not upload {matrix_name} matrix for storage {storage_name} inside area {area_name}: " + message
f"Could not upload {matrix_name} matrix for storage {storage_name} inside area {area_name}: "
+ message
)
super().__init__(self.message)

Expand All @@ -171,30 +189,43 @@ def __init__(self, area_id: str, st_storage_names: List[str], message: str) -> N

class BindingConstraintCreationError(Exception):
def __init__(self, constraint_name: str, message: str) -> None:
self.message = f"Could not create the binding constraint {constraint_name}: " + message
self.message = (
f"Could not create the binding constraint {constraint_name}: " + message
)
super().__init__(self.message)


class ConstraintPropertiesUpdateError(Exception):
def __init__(self, constraint_name: str, message: str) -> None:
self.message = f"Could not update properties for binding constraint {constraint_name}: " + message
self.message = (
f"Could not update properties for binding constraint {constraint_name}: "
+ message
)
super().__init__(self.message)


class ConstraintMatrixUpdateError(Exception):
def __init__(self, constraint_name: str, matrix_name: str, message: str) -> None:
self.message = f"Could not update matrix {matrix_name} for binding constraint {constraint_name}: " + message
self.message = (
f"Could not update matrix {matrix_name} for binding constraint {constraint_name}: "
+ message
)
super().__init__(self.message)


class ConstraintMatrixDownloadError(Exception):
def __init__(self, constraint_name: str, matrix_name: str, message: str) -> None:
self.message = f"Could not download matrix {matrix_name} for binding constraint {constraint_name}: " + message
self.message = (
f"Could not download matrix {matrix_name} for binding constraint {constraint_name}: "
+ message
)
super().__init__(self.message)


class ConstraintTermAdditionError(Exception):
def __init__(self, constraint_name: str, terms_ids: List[str], message: str) -> None:
def __init__(
self, constraint_name: str, terms_ids: List[str], message: str
) -> None:
self.message = (
f"Could not add the following constraint terms: {', '.join(terms_ids)} inside constraint {constraint_name}: "
+ message
Expand All @@ -204,13 +235,18 @@ def __init__(self, constraint_name: str, terms_ids: List[str], message: str) ->

class BindingConstraintDeletionError(Exception):
def __init__(self, constraint_name: str, message: str) -> None:
self.message = f"Could not delete the binding constraint {constraint_name}: " + message
self.message = (
f"Could not delete the binding constraint {constraint_name}: " + message
)
super().__init__(self.message)


class ConstraintTermDeletionError(Exception):
def __init__(self, constraint_id: str, term_id: str, message: str) -> None:
self.message = f"Could not delete the term {term_id} of the binding constraint {constraint_id}: " + message
self.message = (
f"Could not delete the term {term_id} of the binding constraint {constraint_id}: "
+ message
)
super().__init__(self.message)


Expand Down Expand Up @@ -240,21 +276,29 @@ def __init__(self, area_name: str, message: str) -> None:

class LoadMatrixDownloadError(Exception):
def __init__(self, area_name: str, message: str) -> None:
self.message = f"Could not download load matrix for area {area_name}: " + message
self.message = (
f"Could not download load matrix for area {area_name}: " + message
)
super().__init__(self.message)


class ThermalMatrixDownloadError(Exception):
def __init__(self, area_name: str, cluster_name: str, matrix_name: str, message: str) -> None:
def __init__(
self, area_name: str, cluster_name: str, matrix_name: str, message: str
) -> None:
self.message = (
f"Could not download {matrix_name} for cluster {cluster_name} inside area {area_name}: " + message
f"Could not download {matrix_name} for cluster {cluster_name} inside area {area_name}: "
+ message
)
super().__init__(self.message)


class RenewableMatrixDownloadError(Exception):
def __init__(self, area_name: str, renewable_name: str, message: str) -> None:
self.message = f"Could not download matrix for cluster {renewable_name} inside area {area_name}: " + message
self.message = (
f"Could not download matrix for cluster {renewable_name} inside area {area_name}: "
+ message
)
super().__init__(self.message)


Expand All @@ -272,5 +316,9 @@ def __init__(self, message: str = "Error") -> None:

class ConfigurationError(Exception):
def __init__(self, message: str = "Error") -> None:
self.message = "Unsupported configuration type" + f" {message}" if message != "Error" else ""
self.message = (
"Unsupported configuration type" + f" {message}"
if message != "Error"
else ""
)
super().__init__(self.message)
78 changes: 61 additions & 17 deletions src/antares/model/area.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ class AdequacyPatchMode(EnumIgnoreCase):


# todo: Warning, with filesystem, we want to avoid camel case and use link_aliasing.
class AreaProperties(BaseModel, extra="forbid", populate_by_name=True, alias_generator=to_camel):
class AreaProperties(
BaseModel, extra="forbid", populate_by_name=True, alias_generator=to_camel
):
"""
DTO for updating area properties
"""
Expand Down Expand Up @@ -78,10 +80,14 @@ def __init__(
**kwargs: Optional[Any],
):
super().__init__(**kwargs)
self._energy_cost_unsupplied = input_area_properties.energy_cost_unsupplied or 0.0
self._energy_cost_unsupplied = (
input_area_properties.energy_cost_unsupplied or 0.0
)
self._energy_cost_spilled = input_area_properties.energy_cost_spilled or 0.0
self._non_dispatch_power = (
input_area_properties.non_dispatch_power if input_area_properties.non_dispatch_power is not None else True
input_area_properties.non_dispatch_power
if input_area_properties.non_dispatch_power is not None
else True
)
self._dispatch_hydro_power = (
input_area_properties.dispatch_hydro_power
Expand Down Expand Up @@ -112,8 +118,12 @@ def __init__(
if input_area_properties.adequacy_patch_mode
else AdequacyPatchMode.OUTSIDE
)
self._spread_spilled_energy_cost = input_area_properties.spread_spilled_energy_cost or 0.0
self._spread_unsupplied_energy_cost = input_area_properties.spread_unsupplied_energy_cost or 0.0
self._spread_spilled_energy_cost = (
input_area_properties.spread_spilled_energy_cost or 0.0
)
self._spread_unsupplied_energy_cost = (
input_area_properties.spread_unsupplied_energy_cost or 0.0
)

@computed_field # type: ignore[misc]
@property
Expand All @@ -132,12 +142,20 @@ def nodal_optimization(self) -> Mapping[str, str]:
@property
def filtering(self) -> Mapping[str, str]:
return {
"filter-synthesis": ", ".join(filter_value for filter_value in sort_filter_values(self._filter_synthesis)),
"filter-year-by-year": ", ".join(filter_value for filter_value in sort_filter_values(self._filter_by_year)),
"filter-synthesis": ", ".join(
filter_value
for filter_value in sort_filter_values(self._filter_synthesis)
),
"filter-year-by-year": ", ".join(
filter_value
for filter_value in sort_filter_values(self._filter_by_year)
),
}

def adequacy_patch_mode(self) -> dict[str, dict[str, str]]:
return {"adequacy-patch": {"adequacy-patch-mode": self._adequacy_patch_mode.value}}
return {
"adequacy-patch": {"adequacy-patch-mode": self._adequacy_patch_mode.value}
}

def yield_area_properties(self) -> AreaProperties:
return AreaProperties(
Expand All @@ -154,7 +172,9 @@ def yield_area_properties(self) -> AreaProperties:
)


class AreaUi(BaseModel, extra="forbid", populate_by_name=True, alias_generator=to_camel):
class AreaUi(
BaseModel, extra="forbid", populate_by_name=True, alias_generator=to_camel
):
"""
DTO for updating area UI
"""
Expand Down Expand Up @@ -184,7 +204,11 @@ def __init__(
super().__init__(**kwargs)
self._x = input_area_ui.x or 0
self._y = input_area_ui.y or 0
self._color_r, self._color_g, self._color_b = input_area_ui.color_rgb or [230, 108, 44]
self._color_r, self._color_g, self._color_b = input_area_ui.color_rgb or [
230,
108,
44,
]
self._layers = input_area_ui.layer or 0
self._layer_x = input_area_ui.layer_x or {self._layers: self._x}
self._layer_y = input_area_ui.layer_y or {self._layers: self._y}
Expand Down Expand Up @@ -300,7 +324,9 @@ def ui(self) -> AreaUi:
def create_thermal_cluster(
self, thermal_name: str, properties: Optional[ThermalClusterProperties] = None
) -> ThermalCluster:
thermal = self._area_service.create_thermal_cluster(self.id, thermal_name, properties)
thermal = self._area_service.create_thermal_cluster(
self.id, thermal_name, properties
)
self._thermals[thermal.id] = thermal
return thermal

Expand All @@ -315,20 +341,36 @@ def create_thermal_cluster_with_matrices(
fuelCost: Optional[pd.DataFrame],
) -> ThermalCluster:
thermal = self._area_service.create_thermal_cluster_with_matrices(
self.id, cluster_name, parameters, prepro, modulation, series, CO2Cost, fuelCost
self.id,
cluster_name,
parameters,
prepro,
modulation,
series,
CO2Cost,
fuelCost,
)
self._thermals[thermal.id] = thermal
return thermal

def create_renewable_cluster(
self, renewable_name: str, properties: Optional[RenewableClusterProperties], series: Optional[pd.DataFrame]
self,
renewable_name: str,
properties: Optional[RenewableClusterProperties],
series: Optional[pd.DataFrame],
) -> RenewableCluster:
renewable = self._area_service.create_renewable_cluster(self.id, renewable_name, properties, series)
renewable = self._area_service.create_renewable_cluster(
self.id, renewable_name, properties, series
)
self._renewables[renewable.id] = renewable
return renewable

def create_st_storage(self, st_storage_name: str, properties: Optional[STStorageProperties] = None) -> STStorage:
storage = self._area_service.create_st_storage(self.id, st_storage_name, properties)
def create_st_storage(
self, st_storage_name: str, properties: Optional[STStorageProperties] = None
) -> STStorage:
storage = self._area_service.create_st_storage(
self.id, st_storage_name, properties
)
self._st_storages[storage.id] = storage

return storage
Expand All @@ -347,7 +389,9 @@ def delete_thermal_clusters(self, thermal_clusters: List[ThermalCluster]) -> Non
def delete_thermal_cluster(self, thermal_cluster: ThermalCluster) -> None:
self.delete_thermal_clusters([thermal_cluster])

def delete_renewable_clusters(self, renewable_clusters: List[RenewableCluster]) -> None:
def delete_renewable_clusters(
self, renewable_clusters: List[RenewableCluster]
) -> None:
self._area_service.delete_renewable_clusters(self, renewable_clusters)
for cluster in renewable_clusters:
self._renewables.pop(cluster.id)
Expand Down
Loading

0 comments on commit f08aac1

Please sign in to comment.