8000 update repr, str representations on NamedIOSystem objects · python-control/python-control@2264c76 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2264c76

Browse files
committed
update repr, str representations on NamedIOSystem objects
1 parent fb38fd3 commit 2264c76

File tree

4 files changed

+27
-13
lines changed

4 files changed

+27
-13
lines changed

control/flatsys/flatsys.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,11 @@ def __init__(self,
156156

157157
# Save the length of the flat flag
158158

159+
def __str__(self):
160+
return f"{NonlinearIOSystem.__str__(self)}\n\n" \
161+
+ f"Forward: {self.forward}\n" \
162+
+ f"Reverse: {self.reverse}"
163+
159164
def forward(self, x, u, params={}):
160165

161166
"""Compute the flat flag given the states and input.

control/iosys.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -706,6 +706,10 @@ def _out(self, t, x, u):
706706
+ self.D @ np.reshape(u, (-1, 1))
707707
return np.array(y).reshape((-1,))
708708

709+
def __repr__(self):
710+
# Need to define so that I/O system gets used instead of StateSpace
711+
return InputOutputSystem.__repr__(self)
712+
709713
def __str__(self):
710714
return InputOutputSystem.__str__(self) + "\n\n" \
711715
+ StateSpace.__str__(self)
@@ -786,7 +790,7 @@ def __init__(self, updfcn, outfcn=None, inputs=None, outputs=None,
786790

787791
# Initialize the rest of the structure
788792
dt = kwargs.pop('dt', config.defaults['control.default_dt'])
789-
super(NonlinearIOSystem, self).__init__(
793+
super().__init__(
790794
inputs=inputs, outputs=outputs, states=states,
791795
params=params, dt=dt, name=name
792796
)
@@ -816,6 +820,11 @@ def __init__(self, updfcn, outfcn=None, inputs=None, outputs=None,
816820
# Initialize current parameters to default parameters
817821
self._current_params = params.copy()
818822

823+
def __str__(self):
824+
return f"{InputOutputSystem.__str__(self)}\n\n" + \
825+
f"Update: {self.updfcn}\n" + \
826+
f"Output: {self.outfcn}"
827+
819828
# Return the value of a static nonlinear system
820829
def __call__(sys, u, params=None, squeeze=None):
821830
"""Evaluate a (static) nonlinearity at a given input value

control/namedio.py

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,22 +51,16 @@ def __init__(
5151
nstates = None
5252

5353
def __repr__(self):
54-
return str(type(self)) + ": " + self.name if self.name is not None \
55-
else str(type(self))
54+
return f'<{self.__class__.__name__}:{self.name}:' + \
55+
f'{list(self.input_labels)}->{list(self.output_labels)}>'
5656

5757
def __str__(self):
5858
"""String representation of an input/output object"""
59-
str = "Object: " + (self.name if self.name else "(None)") + "\n"
60-
str += "Inputs (%s): " % self.ninputs
61-
for key in self.input_index:
62-
str += key + ", "
63-
str += "\nOutputs (%s): " % self.noutputs
64-
for key in self.output_index:
65-
str += key + ", "
59+
str = f"<{self.__class__.__name__}>: {self.name}\n"
60+
str += f"Inputs ({self.ninputs}): {self.input_labels}\n"
61+
str += f"Outputs ({self.noutputs}): {self.output_labels}\n"
6662
if self.nstates is not None:
67-
str += "\nStates (%s): " % self.nstates
68-
for key in self.state_index:
69-
str += key + ", "
63+
str += f"States ({self.nstates}): {self.state_labels}"
7064
return str
7165

7266
# Find a signal by name

control/tests/namedio_test.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ def test_named_ss():
3131
assert sys.input_labels == ['u[0]', 'u[1]']
3232
assert sys.output_labels == ['y[0]', 'y[1]']
3333
assert sys.state_labels == ['x[0]', 'x[1]']
34+
assert repr(sys) == \
35+
"<LinearIOSystem:sys[0]:['u[0]', 'u[1]']->['y[0]', 'y[1]']>"
3436

3537
# Pass the names as arguments
3638
sys = ct.ss(
@@ -41,6 +43,8 @@ def test_named_ss():
4143
assert sys.input_labels == ['u1', 'u2']
4244
assert sys.output_labels == ['y1', 'y2']
4345
assert sys.state_labels == ['x1', 'x2']
46+
assert repr(sys) == \
47+
"<LinearIOSystem:system:['u1', 'u2']->['y1', 'y2']>"
4448

4549
# Do the same with rss
4650
sys = ct.rss(['x1', 'x2', 'x3'], ['y1', 'y2'], 'u1', name='random')
@@ -49,3 +53,5 @@ def test_named_ss():
4953
assert sys.input_labels == ['u1']
5054
assert sys.output_labels == ['y1', 'y2']
5155
assert sys.state_labels == ['x1', 'x2', 'x3']
56+
assert repr(sys) == \
57+
"<LinearIOSystem:random:['u1']->['y1', 'y2']>"

0 commit comments

Comments
 (0)
0