Skip to content

Commit

Permalink
improve perf using pydantic (#3)
Browse files Browse the repository at this point in the history
* first round of simplification

* remove last usage of json module

* final commit
  • Loading branch information
MartinBelthle authored Sep 20, 2024
1 parent c4f39f9 commit c658687
Show file tree
Hide file tree
Showing 10 changed files with 27 additions and 39 deletions.
15 changes: 7 additions & 8 deletions src/antares/service/api_services/area_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#
# This file is part of the Antares project.

import json
from pathlib import PurePosixPath
from typing import Optional, Dict, Union, List

Expand Down Expand Up @@ -100,7 +99,7 @@ def create_area(

if properties:
url = f"{base_area_url}/{area_id}/properties/form"
body = json.loads(properties.model_dump_json(exclude_none=True))
body = properties.model_dump(mode="json", exclude_none=True)
if body:
self._wrapper.put(url, json=body)
if ui:
Expand Down Expand Up @@ -166,7 +165,7 @@ def create_thermal_cluster(
url = f"{self._base_url}/studies/{self.study_id}/areas/{area_id}/clusters/thermal"
body = {"name": thermal_name.lower()}
if properties:
camel_properties = json.loads(properties.model_dump_json(by_alias=True, exclude_none=True))
camel_properties = properties.model_dump(mode="json", by_alias=True, exclude_none=True)
body = {**body, **camel_properties}
response = self._wrapper.post(url, json=body)
json_response = response.json()
Expand Down Expand Up @@ -223,7 +222,7 @@ def create_thermal_cluster_with_matrices(
raise TypeError("body['args'] must be a dictionary")

if parameters:
camel_properties = json.loads(parameters.model_dump_json(by_alias=True, exclude_none=True))
camel_properties = parameters.model_dump(mode="json", by_alias=True, exclude_none=True)
args["parameters"].update(camel_properties)

if prepro is not None:
Expand Down Expand Up @@ -305,7 +304,7 @@ def create_renewable_cluster(
url = f"{self._base_url}/studies/{self.study_id}/areas/{area_id}/clusters/renewable"
body = {"name": renewable_name.lower()}
if properties:
camel_properties = json.loads(properties.model_dump_json(by_alias=True, exclude_none=True))
camel_properties = properties.model_dump(mode="json", by_alias=True, exclude_none=True)
body = {**body, **camel_properties}
response = self._wrapper.post(url, json=body)
json_response = response.json()
Expand Down Expand Up @@ -347,7 +346,7 @@ def create_st_storage(
url = f"{self._base_url}/studies/{self.study_id}/areas/{area_id}/storages"
body = {"name": st_storage_name}
if properties:
camel_properties = json.loads(properties.model_dump_json(by_alias=True, exclude_none=True))
camel_properties = properties.model_dump(mode="json", by_alias=True, exclude_none=True)
body = {**body, **camel_properties}
response = self._wrapper.post(url, json=body)
json_response = response.json()
Expand Down Expand Up @@ -407,7 +406,7 @@ def create_hydro(
url = f"{self._base_url}/studies/{self.study_id}/areas/{area_id}/hydro/form"
body = {}
if properties:
camel_properties = json.loads(properties.model_dump_json(by_alias=True, exclude_none=True))
camel_properties = properties.model_dump(mode="json", by_alias=True, exclude_none=True)
body = {**camel_properties}
self._wrapper.put(url, json=body)

Expand Down Expand Up @@ -439,7 +438,7 @@ def _create_hydro_series(self, area_id: str, matrices: Dict[HydroMatrixName, pd.
def update_area_properties(self, area: Area, properties: AreaProperties) -> AreaProperties:
url = f"{self._base_url}/studies/{self.study_id}/areas/{area.id}/properties/form"
try:
body = json.loads(properties.model_dump_json(exclude_none=True))
body = properties.model_dump(mode="json", exclude_none=True)
if not body:
return area.properties

Expand Down
5 changes: 2 additions & 3 deletions src/antares/service/api_services/binding_constraint_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#
# This file is part of the Antares project.

import json
from pathlib import PurePosixPath
from typing import Optional, List

Expand Down Expand Up @@ -75,7 +74,7 @@ def create_binding_constraint(
try:
body = {"name": name}
if properties:
camel_properties = json.loads(properties.model_dump_json(by_alias=True, exclude_none=True))
camel_properties = properties.model_dump(mode="json", by_alias=True, exclude_none=True)
body = {**body, **camel_properties}
for matrix, matrix_name in zip(
[less_term_matrix, equal_term_matrix, greater_term_matrix],
Expand Down Expand Up @@ -118,7 +117,7 @@ def update_binding_constraint_properties(
) -> BindingConstraintProperties:
url = f"{self._base_url}/studies/{self.study_id}/bindingconstraints/{binding_constraint.id}"
try:
body = json.loads(properties.model_dump_json(by_alias=True, exclude_none=True))
body = properties.model_dump(mode="json", by_alias=True, exclude_none=True)
if not body:
return binding_constraint.properties

Expand Down
18 changes: 8 additions & 10 deletions src/antares/service/api_services/link_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#
# This file is part of the Antares project.

import json
from types import MappingProxyType
from typing import Optional

Expand All @@ -26,7 +25,6 @@
from antares.model.area import Area
from antares.model.link import LinkProperties, LinkUi, Link
from antares.service.base_services import BaseLinkService
from antares.tools.ini_tool import check_if_none


class LinkApiService(BaseLinkService):
Expand Down Expand Up @@ -73,10 +71,10 @@ def create_link(
json_file = response.json()
# TODO update to use check_if_none or similar
if properties or ui:
link_properties = json.loads(
check_if_none(properties, LinkProperties()).model_dump_json(by_alias=True, exclude_none=True)
link_properties = (properties or LinkProperties()).model_dump(
mode="json", by_alias=True, exclude_none=True
)
link_ui = json.loads(check_if_none(ui, LinkUi()).model_dump_json(by_alias=True, exclude_none=True))
link_ui = (ui or LinkUi()).model_dump(mode="json", by_alias=True, exclude_none=True)
body = {**link_properties, **link_ui}
if body:
json_file = _join_filter_values_for_json(json_file, body)
Expand All @@ -92,13 +90,13 @@ def create_link(
else:
json_properties[key] = value
del json_file[key]
link_ui = LinkUi.model_validate(json_file)
link_properties = LinkProperties.model_validate(json_properties)
ui = LinkUi.model_validate(json_file)
created_properties = LinkProperties.model_validate(json_properties)

except APIError as e:
raise LinkCreationError(area_from.id, area_to.id, e.message) from e

return Link(area_from, area_to, self, link_properties, link_ui)
return Link(area_from, area_to, self, created_properties, ui)

def delete_link(self, link: Link) -> None:
area_from_id = link.area_from.id
Expand All @@ -114,7 +112,7 @@ def update_link_properties(self, link: Link, properties: LinkProperties) -> Link
area1_id, area2_id = sorted([link.area_from.id, link.area_to.id])
raw_url = f"{self._base_url}/studies/{self.study_id}/raw?path=input/links/{area1_id}/properties/{area2_id}"
try:
new_properties = json.loads(properties.model_dump_json(by_alias=True, exclude_none=True))
new_properties = properties.model_dump(mode="json", by_alias=True, exclude_none=True)
if not new_properties:
return link.properties

Expand Down Expand Up @@ -146,7 +144,7 @@ def update_link_ui(self, link: Link, ui: LinkUi) -> LinkUi:
area1_id, area2_id = sorted([link.area_from.id, link.area_to.id])
raw_url = f"{self._base_url}/studies/{self.study_id}/raw?path=input/links/{area1_id}/properties/{area2_id}"
try:
new_ui = json.loads(ui.model_dump_json(by_alias=True, exclude_none=True))
new_ui = ui.model_dump(mode="json", by_alias=True, exclude_none=True)
if not new_ui:
return link.ui

Expand Down
3 changes: 1 addition & 2 deletions src/antares/service/api_services/renewable_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#
# This file is part of the Antares project.

import json
from pathlib import PurePosixPath

import pandas as pd
Expand All @@ -36,7 +35,7 @@ def update_renewable_properties(
) -> RenewableClusterProperties:
url = f"{self._base_url}/studies/{self.study_id}/areas/{renewable_cluster.area_id}/clusters/renewable/{renewable_cluster.id}"
try:
body = json.loads(properties.model_dump_json(by_alias=True, exclude_none=True))
body = properties.model_dump(mode="json", by_alias=True, exclude_none=True)
if not body:
return renewable_cluster.properties

Expand Down
4 changes: 1 addition & 3 deletions src/antares/service/api_services/st_storage_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
#
# This file is part of the Antares project.

import json

import pandas as pd

from antares.api_conf.api_conf import APIconf
Expand Down Expand Up @@ -39,7 +37,7 @@ def update_st_storage_properties(
) -> STStorageProperties:
url = f"{self._base_url}/studies/{self.study_id}/areas/{st_storage.area_id}/storages/{st_storage.id}"
try:
body = json.loads(properties.model_dump_json(by_alias=True, exclude_none=True))
body = properties.model_dump(mode="json", by_alias=True, exclude_none=True)
if not body:
return st_storage.properties

Expand Down
3 changes: 1 addition & 2 deletions src/antares/service/api_services/study_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#
# This file is part of the Antares project.

import json
from typing import Optional

from antares.api_conf.api_conf import APIconf
Expand Down Expand Up @@ -48,7 +47,7 @@ def _returns_study_settings(
"playlist": ("playlist", None),
}
if settings:
json_settings = json.loads(settings.model_dump_json(by_alias=True, exclude_none=True))
json_settings = settings.model_dump(mode="json", by_alias=True, exclude_none=True)
if not json_settings and update:
return None

Expand Down
3 changes: 1 addition & 2 deletions src/antares/service/api_services/thermal_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#
# This file is part of the Antares project.

import json
from pathlib import PurePosixPath

import pandas as pd
Expand All @@ -36,7 +35,7 @@ def update_thermal_properties(
) -> ThermalClusterProperties:
url = f"{self._base_url}/studies/{self.study_id}/areas/{thermal_cluster.area_id}/clusters/thermal/{thermal_cluster.id}"
try:
body = json.loads(properties.model_dump_json(by_alias=True, exclude_none=True))
body = properties.model_dump(mode="json", by_alias=True, exclude_none=True)
if not body:
return thermal_cluster.properties

Expand Down
9 changes: 4 additions & 5 deletions tests/antares/services/api_services/test_area_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#
# This file is part of the Antares project.

import json

from antares.api_conf.api_conf import APIconf
from antares.exceptions.exceptions import (
Expand Down Expand Up @@ -101,7 +100,7 @@ def test_update_area_ui_fails(self):
def test_create_thermal_success(self):
with requests_mock.Mocker() as mocker:
url = f"https://antares.com/api/v1/studies/{self.study_id}/areas/{self.area.id}/clusters/thermal"
json_response = json.loads(ThermalClusterProperties().model_dump_json(by_alias=True))
json_response = ThermalClusterProperties().model_dump(mode="json", by_alias=True)
thermal_name = "thermal_cluster"
mocker.post(url, json={"name": thermal_name, "id": thermal_name, **json_response}, status_code=201)
thermal = self.area.create_thermal_cluster(thermal_name=thermal_name)
Expand All @@ -122,7 +121,7 @@ def test_create_thermal_fails(self):
def test_create_renewable_success(self):
with requests_mock.Mocker() as mocker:
url = f"https://antares.com/api/v1/studies/{self.study_id}/areas/{self.area.id}/clusters/renewable"
json_response = json.loads(RenewableClusterProperties().model_dump_json(by_alias=True))
json_response = RenewableClusterProperties().model_dump(mode="json", by_alias=True)
renewable_name = "renewable_cluster"
mocker.post(url, json={"name": renewable_name, "id": renewable_name, **json_response}, status_code=201)

Expand All @@ -148,7 +147,7 @@ def test_create_renewable_fails(self):
def test_create_st_storage_success(self):
with requests_mock.Mocker() as mocker:
url = f"https://antares.com/api/v1/studies/{self.study_id}/areas/{self.area.id}/storages"
json_response = json.loads(STStorageProperties().model_dump_json(by_alias=True))
json_response = STStorageProperties().model_dump(mode="json", by_alias=True)
st_storage_name = "short_term_storage"
mocker.post(url, json={"name": st_storage_name, "id": st_storage_name, **json_response}, status_code=201)

Expand Down Expand Up @@ -202,7 +201,7 @@ def test_create_thermal_cluster_with_matrices(self):

def test_create_hydro_success(self):
url_hydro_form = f"https://antares.com/api/v1/studies/{self.study_id}/areas/{self.area.id}/hydro/form"
json_for_post = json.loads(HydroProperties().model_dump_json(by_alias=True))
json_for_post = HydroProperties().model_dump(mode="json", by_alias=True)
series = pd.DataFrame(data=np.ones((150, 1)))

url_for_command = f"https://antares.com/api/v1/studies/{self.study_id}/commands"
Expand Down
3 changes: 1 addition & 2 deletions tests/antares/services/api_services/test_link_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#
# This file is part of the Antares project.

import json

from antares.api_conf.api_conf import APIconf
from antares.exceptions.exceptions import LinkUiUpdateError, LinkPropertiesUpdateError
Expand Down Expand Up @@ -48,7 +47,7 @@ def test_update_links_properties_success(self):
mocker.post(raw_url, status_code=200)
mocker.get(
raw_url,
json={**ui.model_dump(by_alias=True), **json.loads(properties.model_dump_json(by_alias=True))},
json={**ui.model_dump(by_alias=True), **properties.model_dump(mode="json", by_alias=True)},
status_code=200,
)

Expand Down
3 changes: 1 addition & 2 deletions tests/antares/services/api_services/test_study_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#
# This file is part of the Antares project.

import json

from antares.api_conf.api_conf import APIconf
import requests_mock
Expand Down Expand Up @@ -180,7 +179,7 @@ def test_create_link_fails(self):
def test_create_binding_constraint_success(self):
with requests_mock.Mocker() as mocker:
url = f"https://antares.com/api/v1/studies/{self.study_id}/bindingconstraints"
json_response = json.loads(BindingConstraintProperties().model_dump_json(by_alias=True))
json_response = BindingConstraintProperties().model_dump(mode="json", by_alias=True)
constraint_name = "bc_1"
mocker.post(url, json={"id": "id", "name": constraint_name, "terms": [], **json_response}, status_code=201)
constraint = self.study.create_binding_constraint(name=constraint_name)
Expand Down

0 comments on commit c658687

Please sign in to comment.