|
16 | 16 | from .exception import ControlIndexError |
17 | 17 |
|
18 | 18 | __all__ = ['InputOutputSystem', 'NamedSignal', 'issiso', 'timebase', |
19 | | - 'common_timebase', 'isdtime', 'isctime'] |
| 19 | + 'common_timebase', 'isdtime', 'isctime', 'iosys_repr'] |
20 | 20 |
|
21 | 21 | # Define module default parameter values |
22 | 22 | _iosys_defaults = { |
|
31 | 31 | 'iosys.indexed_system_name_suffix': '$indexed', |
32 | 32 | 'iosys.converted_system_name_prefix': '', |
33 | 33 | 'iosys.converted_system_name_suffix': '$converted', |
34 | | - 'iosys.repr_format': 'iosys', |
| 34 | + 'iosys.repr_format': 'info', |
35 | 35 | } |
36 | 36 |
|
37 | 37 |
|
@@ -163,6 +163,8 @@ class InputOutputSystem(object): |
163 | 163 | Set the prefix for output signals. Default = 'y'. |
164 | 164 | state_prefix : string, optional |
165 | 165 | Set the prefix for state signals. Default = 'x'. |
| 166 | + repr_format : str |
| 167 | + String representation format. See :func:`control.iosys_repr`. |
166 | 168 |
|
167 | 169 | """ |
168 | 170 | # Allow NDarray * IOSystem to give IOSystem._rmul_() priority |
@@ -241,16 +243,30 @@ def _generic_name_check(self): |
241 | 243 | nstates = None |
242 | 244 |
|
243 | 245 | def __repr__(self): |
244 | | - return f'<{self.__class__.__name__}:{self.name}:' + \ |
245 | | - f'{list(self.input_labels)}->{list(self.output_labels)}>' |
| 246 | + return iosys_repr(self, format=self.repr_format) |
246 | 247 |
|
247 | | - def iosys_repr(self, format=None): |
248 | | - raise NotImplementedError( |
249 | | - f"`iosys_repr` is not implemented for {self.__class__}") |
| 248 | + def _repr_info_(self): |
| 249 | + return f'<{self.__class__.__name__} {self.name}: ' + \ |
| 250 | + f'{list(self.input_labels)} -> {list(self.output_labels)}>' |
250 | 251 |
|
251 | 252 | @property |
252 | 253 | def repr_format(self): |
253 | | - """Set the string representation format ('iosys' or 'loadable').""" |
| 254 | + """String representation format. |
| 255 | +
|
| 256 | + Format used in creating the representation for the system: |
| 257 | +
|
| 258 | + * 'info' : <IOSystemType:sysname:[inputs]->[outputs] |
| 259 | + * 'eval' : system specific, loadable representation |
| 260 | + * 'latex' : latex representation of the object |
| 261 | +
|
| 262 | + The default representation for an input/output is set to 'info'. |
| 263 | + This value can be changed for an individual system by setting the |
| 264 | + `repr_format` parameter when the system is created or by setting |
| 265 | + the `repr_format` property after system creation. Set |
| 266 | + config.defaults['iosys.repr_format'] to change for all I/O systems |
| 267 | + or use the `repr_format` parameter/attribute for a single system. |
| 268 | +
|
| 269 | + """ |
254 | 270 | return self._repr_format if self._repr_format is not None \ |
255 | 271 | else config.defaults['iosys.repr_format'] |
256 | 272 |
|
@@ -740,6 +756,47 @@ def isctime(sys=None, dt=None, strict=False): |
740 | 756 | return sys.isctime(strict) |
741 | 757 |
|
742 | 758 |
|
| 759 | +def iosys_repr(sys, format=None): |
| 760 | + """Return representation of an I/O system. |
| 761 | +
|
| 762 | + Parameters |
| 763 | + ---------- |
| 764 | + sys : InputOutputSystem |
| 765 | + System for which the representation is generated. |
| 766 | + format : str |
| 767 | + Format to use in creating the representation: |
| 768 | +
|
| 769 | + * 'info' : <IOSystemType:sysname:[inputs]->[outputs] |
| 770 | + * 'eval' : system specific, loadable representation |
| 771 | + * 'latex' : latex representation of the object |
| 772 | +
|
| 773 | + Returns |
| 774 | + ------- |
| 775 | + str |
| 776 | + String representing the input/output system. |
| 777 | +
|
| 778 | + Notes |
| 779 | + ----- |
| 780 | + By default, the representation for an input/output is set to 'info'. |
| 781 | + Set config.defaults['iosys.repr_format'] to change for all I/O systems |
| 782 | + or use the `repr_format` parameter for a single system. |
| 783 | +
|
| 784 | + Jupyter will automatically use the 'latex' representation for I/O |
| 785 | + systems, when available. |
| 786 | +
|
| 787 | + """ |
| 788 | + format = config.defaults['iosys.repr_format'] if format is None else format |
| 789 | + match format: |
| 790 | + case 'info': |
| 791 | + return sys._repr_info_() |
| 792 | + case 'eval': |
| 793 | + return sys._repr_eval_() |
| 794 | + case 'latex': |
| 795 | + return sys._repr_latex_() |
| 796 | + case _: |
| 797 | + raise ValueError(f"format '{format}' unknown") |
| 798 | + |
| 799 | + |
743 | 800 | # Utility function to parse iosys keywords |
744 | 801 | def _process_iosys_keywords( |
745 | 802 | keywords={}, defaults={}, static=False, end=False): |
|
0 commit comments