-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from PowerGridModel/feature/python-api
Feature/python api
- Loading branch information
Showing
11 changed files
with
212 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
# SPDX-FileCopyrightText: Contributors to the Power Grid Model project <[email protected]> | ||
# | ||
# SPDX-License-Identifier: MPL-2.0 | ||
|
||
""" | ||
Error handling | ||
""" | ||
|
||
from power_grid_model._core.error_handling import ( | ||
PGM_NO_ERROR, | ||
PGM_REGULAR_ERROR, | ||
PGM_SERIALIZATION_ERROR, | ||
VALIDATOR_MSG, | ||
PowerGridBatchError, | ||
PowerGridSerializationError, | ||
_interpret_error, | ||
) | ||
|
||
from power_grid_model_io_native._core.power_grid_model_io_core import pgm_io_core as pgmic | ||
|
||
# def _interpret_error_pgm_io(message: str, decode_error: bool = True) -> PowerGridError: | ||
# if decode_error: | ||
# for pattern, type_ in _ERROR_MESSAGE_PATTERNS.items(): | ||
# if pattern.search(message) is not None: | ||
# return type_(message) | ||
|
||
# return PowerGridError(message) | ||
|
||
|
||
def find_error(batch_size: int = 1, decode_error: bool = True) -> RuntimeError | None: | ||
""" | ||
Check if there is an error and return it | ||
Args: | ||
batch_size: (int, optional): Size of batch. Defaults to 1. | ||
decode_error (bool, optional): Decode the error message(s) to derived error classes. Defaults to True | ||
Returns: error object, can be none | ||
""" | ||
_ = batch_size | ||
error_code: int = pgmic.error_code() | ||
if error_code == PGM_NO_ERROR: | ||
return None | ||
if error_code == PGM_REGULAR_ERROR: | ||
error_message = pgmic.error_message() | ||
error_message += VALIDATOR_MSG | ||
return _interpret_error(error_message, decode_error=decode_error) | ||
if error_code == PGM_SERIALIZATION_ERROR: | ||
return PowerGridSerializationError(pgmic.error_message()) | ||
return RuntimeError("Unknown error!") | ||
|
||
|
||
def assert_no_error(batch_size: int = 1, decode_error: bool = True): | ||
""" | ||
Assert there is no error in the last operation | ||
If there is an error, raise it | ||
Args: | ||
batch_size (int, optional): Size of batch. Defaults to 1. | ||
decode_error (bool, optional): Decode the error message(s) to derived error classes. Defaults to True | ||
Returns: | ||
""" | ||
error = find_error(batch_size=batch_size, decode_error=decode_error) | ||
if error is not None: | ||
raise error | ||
|
||
|
||
def handle_errors( | ||
continue_on_batch_error: bool, batch_size: int = 1, decode_error: bool = True | ||
) -> PowerGridBatchError | None: | ||
""" | ||
Handle any errors in the way that is specified. | ||
Args: | ||
continue_on_batch_error (bool): Return the error when the error type is a batch error instead of reraising it. | ||
batch_size (int, optional): Size of batch. Defaults to 1. | ||
decode_error (bool, optional): Decode the error message(s) to derived error classes. Defaults to True | ||
Raises: | ||
error: Any errors previously encountered, unless it was a batch error and continue_on_batch_error was True. | ||
Returns: | ||
PowerGridBatchError | None: None if there were no errors, or the previously encountered | ||
error if it was a batch error and continue_on_batch_error was True. | ||
""" | ||
error: RuntimeError | None = find_error(batch_size=batch_size, decode_error=decode_error) | ||
if error is None: | ||
return None | ||
|
||
if continue_on_batch_error and isinstance(error, PowerGridBatchError): | ||
# continue on batch error | ||
return error | ||
|
||
# raise normal error | ||
raise error |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# SPDX-FileCopyrightText: Contributors to the Power Grid Model project <[email protected]> | ||
# | ||
# SPDX-License-Identifier: MPL-2.0 | ||
|
||
""" | ||
Power grid model io native converter for vnf files | ||
""" | ||
|
||
from power_grid_model_io_native._core.error_handling import assert_no_error | ||
from power_grid_model_io_native._core.power_grid_model_io_core import PgmVnfConverterPtr, pgm_io_core as pgmic | ||
|
||
|
||
class PgmVnfConverter: | ||
"""A converter class which will convert a given string representation of .vnf data to the PowerGridModel format""" | ||
|
||
_pgm_vnf_converter: PgmVnfConverterPtr | ||
_serialized_data: str | ||
|
||
def __new__( | ||
cls, | ||
string_buffer: str, | ||
experimental_feature: int, | ||
): | ||
instance = super().__new__(cls) | ||
|
||
instance._pgm_vnf_converter = pgmic.create_vnf_converter(string_buffer, experimental_feature) | ||
assert_no_error() | ||
|
||
return instance | ||
|
||
def __del__(self): | ||
if hasattr(self, "_pgm_vnf_converter"): | ||
pgmic.destroy_vnf_converter(self._pgm_vnf_converter) | ||
|
||
def get_pgm_input_data(self): | ||
"""A function of the PgmVnfConverter class which will convert and return the data in PGM format | ||
Returns: | ||
str: json data in PGM format | ||
""" | ||
pgm_data = pgmic.vnf_pgm_converter_get_input_data(self._pgm_vnf_converter) | ||
assert_no_error() | ||
self._serialized_data = pgm_data | ||
return self._serialized_data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# SPDX-FileCopyrightText: Contributors to the Power Grid Model project <[email protected]> | ||
# | ||
# SPDX-License-Identifier: MPL-2.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# SPDX-FileCopyrightText: Contributors to the Power Grid Model project <[email protected]> | ||
# | ||
# SPDX-License-Identifier: MPL-2.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters