Skip to content

Commit 470ecb0

Browse files
authored
Merge pull request PowerGridModel#1202 from kornerc/repr
implement `__repr__` for PowerGridModel
2 parents 58a4434 + 859ed58 commit 470ecb0

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

src/power_grid_model/_core/power_grid_model.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,26 @@ def __deepcopy__(self, memo: dict[int, Any]) -> "PowerGridModel":
120120

121121
return new_model
122122

123+
def __repr__(self) -> str:
124+
"""Return a string representation of the current model.
125+
126+
This includes the total number of components and the number of components per component type of the model.
127+
128+
Returns:
129+
String representation of the model
130+
"""
131+
try:
132+
component_count = self.all_component_count
133+
except TypeError:
134+
component_count = {}
135+
136+
message = f"{self.__class__.__name__} ({sum(component_count.values())} components)\n"
137+
138+
for component_type, number in component_count.items():
139+
message += f" - {component_type.value}: {number}\n"
140+
141+
return message
142+
123143
def __new__(cls, *_args, **_kwargs):
124144
instance = super().__new__(cls)
125145
instance._model_ptr = ModelPtr()

tests/unit/test_power_grid_model.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,11 @@ def model(input):
145145
return PowerGridModel(input)
146146

147147

148+
@pytest.fixture
149+
def empty_model():
150+
return PowerGridModel({})
151+
152+
148153
def test_simple_power_flow(model: PowerGridModel, sym_output):
149154
result = model.calculate_power_flow()
150155
compare_result(result, sym_output, rtol=0.0, atol=1e-8)
@@ -176,11 +181,9 @@ def test_copy_model(model: PowerGridModel, sym_output):
176181
compare_result(result, sym_output, rtol=0.0, atol=1e-8)
177182

178183

179-
def test_deepcopy_model(model: PowerGridModel, sym_output, update_batch, sym_output_batch):
180-
model_other = PowerGridModel({})
181-
184+
def test_deepcopy_model(model: PowerGridModel, empty_model: PowerGridModel, sym_output, update_batch, sym_output_batch):
182185
# list containing different models twice
183-
model_list = [model, model_other, model, model_other]
186+
model_list = [model, empty_model, model, empty_model]
184187

185188
new_model_list = deepcopy(model_list)
186189

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

209212

213+
def test_repr_and_str(model: PowerGridModel, empty_model: PowerGridModel):
214+
repr_empty_model_expected = "PowerGridModel (0 components)\n"
215+
assert repr_empty_model_expected == repr(empty_model)
216+
assert repr_empty_model_expected == str(empty_model)
217+
218+
repr_model_expected = "PowerGridModel (3 components)\n - node: 1\n - source: 1\n - sym_load: 1\n"
219+
assert repr_model_expected == repr(model)
220+
assert repr_model_expected == str(model)
221+
222+
210223
def test_get_indexer(model: PowerGridModel):
211224
ids = np.array([2, 2])
212225
expected_indexer = np.array([0, 0])

0 commit comments

Comments
 (0)