-
Notifications
You must be signed in to change notification settings - Fork 46
Description
Both str() and repr() of a (Python) PowerGridModel return the default representation of an object in Python:
pgm: PowerGridModel = ...
str(pgm)
# '<power_grid_model._core.power_grid_model.PowerGridModel object at 0x00000209EAB2DA00>'
repr(pgm)
# '<power_grid_model._core.power_grid_model.PowerGridModel object at 0x00000209EAB2DA00>'It would be nice to have a more meaningful string representation.
This behaviour can be changed by implementing the methods __str__ and __repr__ (if __repr__ is implemented but __str__ not, the implementation of __repr__ will be used for str() and repr()).
Usually, __repr__ can be used to directly reconstruct the object and __str__ is used for pretty printing:
from pathlib import Path
path = Path("./a/relative/path")
str(path)
# 'a\\relative\\path'
repr(path)
# "WindowsPath('a/relative/path')"
from pathlib import WindowsPath
assert path = eval(repr(path))I guess implementing __repr__ so that an object of PowerGridModel can be directly reconstructed is not the right choice.
For example, a pandas DataFrame can't be reconstructed and the same implementations are used for __repr__ and __str__:
import pandas as pd
repr(pd.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]}))
# ' a b\n0 1 4\n1 2 5\n2 3 6'pandapower also uses the same implementation for both and returns the number of elements per element type
import pandapower as pp
net: pp.pandapowerNet = ...
str(net)
# This pandapower network includes the following parameter tables:\n - bus (146 elements)\n - load (139 elements)\n - switch (11 elements)\n - ext_grid (1 element)\n - line (147 elements)\n - trafo (2 elements)\n - controller (2 elements)
print(net)
# This pandapower network includes the following parameter tables:
# - bus (146 elements)
# - load (139 elements)
# - switch (11 elements)
# - ext_grid (1 element)
# - line (147 elements)
# - trafo (2 elements)
# - controller (2 elements)Should we have a similar implementation like pandapower by leveraging internally PowerGridModel.all_component_count.
It could look similar to:
pgm.all_component_count
# {<ComponentType.node: 'node'>: 146, <ComponentType.line: 'line'>: 147, <ComponentType.source: 'source'>: 1, <ComponentType.sym_load: 'sym_load'>: 417, <ComponentType.transformer: 'transformer'>: 2, <ComponentType.link: 'link'>: 11}
print(pgm)
# PowerGridModel
# - nodes: 146
# - lines: 147
# - sources: 1
# - sym_loads: 417
# - transformers: 2
# - links: 11Metadata
Metadata
Assignees
Labels
Type
Projects
Status