Skip to content

Commit d8cf657

Browse files
committed
Add option to exclude attributes from __pyaml_repr__.
1 parent 21365b6 commit d8cf657

3 files changed

Lines changed: 22 additions & 8 deletions

File tree

pyaml/common/element.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,19 @@
88
from ..common.element_holder import ElementHolder
99

1010

11-
def __pyaml_repr__(obj):
11+
def __pyaml_repr__(obj, exclude: list[str] | None = None):
1212
"""
13-
Returns a string representation of a pyaml object
13+
Returns a string representation of a pyaml object.
14+
15+
Parameters
16+
----------
17+
exclude : list[str] | None
18+
Attribute/property names to exclude from the output.
1419
"""
1520

21+
if exclude is None:
22+
exclude = []
23+
1624
cls_name = obj.__class__.__name__
1725

1826
# Keep the old behavior when _cfg exists
@@ -31,19 +39,19 @@ def __pyaml_repr__(obj):
3139

3240
# Instance attributes
3341
for k, v in obj.__dict__.items():
34-
# Exclude private attributes
35-
if not k.startswith("_"):
42+
# Exclude private attributes and excluded
43+
if not k.startswith("_") and k not in exclude:
3644
attrs[k] = v
3745

3846
# Properties
3947
for name, attr in vars(type(obj)).items():
40-
if isinstance(attr, property):
48+
if isinstance(attr, property) and name not in exclude:
4149
try:
4250
attrs[name] = getattr(obj, name)
4351
except Exception as e:
4452
attrs[name] = f"<error: {e}>"
4553

46-
if isinstance(obj, Element) and "name" not in attrs:
54+
if isinstance(obj, Element) and "name" not in attrs and "name" not in exclude:
4755
try:
4856
attrs["name"] = obj.get_name()
4957
except Exception as e:

pyaml/rf/rf_plant.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
from .. import PyAMLException
55
from ..common import abstract
6-
from ..common.element import Element
6+
from ..common.element import Element, __pyaml_repr__
77
from ..validation import DynamicValidation, register_schema
88
from .rf_transmitter import RFTransmitter
99

@@ -90,3 +90,6 @@ def set_and_wait(self, value: float):
9090

9191
def unit(self) -> str:
9292
return self.__trans[0].phase_device_access.unit()
93+
94+
def __repr__(self):
95+
return __pyaml_repr__(self, exclude=["frequency", "voltage"])

pyaml/rf/rf_transmitter.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
from .. import PyAMLException
55
from ..common import abstract
6-
from ..common.element import Element
6+
from ..common.element import Element, __pyaml_repr__
77
from ..validation import DynamicValidation, register_schema
88

99
# Define the main class name for this module
@@ -104,3 +104,6 @@ def attach(
104104
obj.__phase = phase
105105
obj._peer = peer
106106
return obj
107+
108+
def __repr__(self):
109+
return __pyaml_repr__(self, exclude=["voltage", "phase"])

0 commit comments

Comments
 (0)