Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
20 changes: 20 additions & 0 deletions src/power_grid_model/_core/power_grid_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,26 @@ def __deepcopy__(self, memo: dict[int, Any]) -> "PowerGridModel":

return new_model

def __repr__(self) -> str:
"""Return a string representation of the current model.

This includes the total number of components and the number of components per component type of the model.

Returns:
String representation of the model
"""
try:
component_count = self.all_component_count
except TypeError:
component_count = {}

message = f"{self.__class__.__name__} ({sum(component_count.values())} components)\n"

for component_type, number in component_count.items():
message += f" - {component_type.value}: {number}\n"

return message

def __new__(cls, *_args, **_kwargs):
instance = super().__new__(cls)
instance._model_ptr = ModelPtr()
Expand Down
21 changes: 17 additions & 4 deletions tests/unit/test_power_grid_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,11 @@ def model(input):
return PowerGridModel(input)


@pytest.fixture
def empty_model():
return PowerGridModel({})


def test_simple_power_flow(model: PowerGridModel, sym_output):
result = model.calculate_power_flow()
compare_result(result, sym_output, rtol=0.0, atol=1e-8)
Expand Down Expand Up @@ -176,11 +181,9 @@ def test_copy_model(model: PowerGridModel, sym_output):
compare_result(result, sym_output, rtol=0.0, atol=1e-8)


def test_deepcopy_model(model: PowerGridModel, sym_output, update_batch, sym_output_batch):
model_other = PowerGridModel({})

def test_deepcopy_model(model: PowerGridModel, empty_model: PowerGridModel, sym_output, update_batch, sym_output_batch):
# list containing different models twice
model_list = [model, model_other, model, model_other]
model_list = [model, empty_model, model, empty_model]

new_model_list = deepcopy(model_list)

Expand All @@ -207,6 +210,16 @@ def test_deepcopy_model(model: PowerGridModel, sym_output, update_batch, sym_out
compare_result(result, sym_output, rtol=0.0, atol=1e-8)


def test_repr_and_str(model: PowerGridModel, empty_model: PowerGridModel):
repr_empty_model_expected = "PowerGridModel (0 components)\n"
assert repr_empty_model_expected == repr(empty_model)
assert repr_empty_model_expected == str(empty_model)

repr_model_expected = "PowerGridModel (3 components)\n - node: 1\n - source: 1\n - sym_load: 1\n"
assert repr_model_expected == repr(model)
assert repr_model_expected == str(model)


def test_get_indexer(model: PowerGridModel):
ids = np.array([2, 2])
expected_indexer = np.array([0, 0])
Expand Down
Loading