Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/python api #16

Merged
merged 22 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ PGM_IO_API PGM_IO_VnfConverter* PGM_IO_create_vnf_converter(PGM_IO_Handle* handl
* @brief Retrieve the transformed input data from .vnf format to PGM format
* @param handle
* @param converter_ptr A pointer to a PGM_IO_VnfConverter instace.
* @param dataset A pointer to the const dataset supplied by the user.
* @return The pointer to the const dataset instance supplied by the user which has been filled in.
* @return The pointer to the json string instance that holds data in PGM format.
*/
PGM_IO_API char const* PGM_IO_get_vnf_input_data(PGM_IO_Handle* handle, PGM_IO_VnfConverter* converter_ptr);
PGM_IO_API char const* PGM_IO_vnf_pgm_converter_get_input_data(PGM_IO_Handle* handle,
PGM_IO_VnfConverter* converter_ptr);
Laurynas-Jagutis marked this conversation as resolved.
Show resolved Hide resolved

/**
* @brief Destroy the PGM_IO_VnfConverter and free up the memory that was dedicated to it.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ PGM_IO_VnfConverter* PGM_IO_create_vnf_converter(PGM_IO_Handle* handle, char con
PGM_IO_regular_error);
}

char const* PGM_IO_get_vnf_input_data(PGM_IO_Handle* handle, PGM_IO_VnfConverter* converter_ptr) {
char const* PGM_IO_vnf_pgm_converter_get_input_data(PGM_IO_Handle* handle, PGM_IO_VnfConverter* converter_ptr) {
return call_with_catch(
handle, [converter_ptr] { return convert_input_wrapper(converter_ptr).c_str(); }, PGM_IO_regular_error);
}
Expand Down
18 changes: 18 additions & 0 deletions src/power_grid_model_io_native/_core/power_grid_model_io_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ class HandlePtr(c_void_p):
"""


class PgmVnfConverterPtr(c_void_p):
"""
Pointer to serializer
"""


def _load_core() -> CDLL:
"""

Expand Down Expand Up @@ -139,6 +145,18 @@ def error_code(self) -> int: # type: ignore[empty-body]
def error_message(self) -> str: # type: ignore[empty-body]
pass # pragma: no cover

@make_c_binding
def create_vnf_converter(self, data: str, experimental_features: int) -> PgmVnfConverterPtr: # type: ignore[empty-body]
pass # pragma: no cover

@make_c_binding
def vnf_pgm_converter_get_input_data(self, pgmvnfconverter: PgmVnfConverterPtr) -> str: # type: ignore[empty-body]
pass # pragma: no cover

@make_c_binding
def destroy_vnf_converter(self, pgmvnfconverter: PgmVnfConverterPtr) -> None: # type: ignore[empty-body]
pass # pragma: no cover


# make one instance
pgm_io_core = PowerGridModelIoCore()
41 changes: 41 additions & 0 deletions src/power_grid_model_io_native/_core/vnf_converter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# SPDX-FileCopyrightText: Contributors to the Power Grid Model project <[email protected]>
#
# SPDX-License-Identifier: MPL-2.0

from power_grid_model.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
21 changes: 21 additions & 0 deletions tests/unit/test_vnf_converter.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,29 @@
# SPDX-FileCopyrightText: Contributors to the Power Grid Model project <[email protected]>
#
# SPDX-License-Identifier: MPL-2.0
import pytest

from power_grid_model_io_native._core.power_grid_model_io_core import pgm_io_core
from power_grid_model_io_native._core.vnf_converter import PgmVnfConverter


def test_pgmvnfconverter_constructor_without_experimental_features():
"""_summary_"""
Laurynas-Jagutis marked this conversation as resolved.
Show resolved Hide resolved
with pytest.raises(Exception) as e:
_ = PgmVnfConverter("", 0)
print(f"Raised exception: {e.type} - {e.value}")


def test_pgmvnfconverter_constructor_with_experimental_features():
converter = PgmVnfConverter("", 1)
assert hasattr(converter, "_pgm_vnf_converter")


def test_get_pgm_input_data():
converter = PgmVnfConverter("", 1)
result_buffer = converter.get_pgm_input_data()
json_output = '({"version":"1.0","type":"input","is_batch":false,"attributes":{},"data":{}})'
assert result_buffer == json_output


def test_nothing():
Expand Down
Loading