8000 Small changes to make things more python 3 compatible · MaxGaukler/python-control@43310f7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 43310f7

Browse files
committed
Small changes to make things more python 3 compatible
* Updated unit test code to use proper print functions * Got rid of 'long' as a type for constant systems
1 parent 038785a commit 43310f7

14 files changed

+82
-45
lines changed

ChangeLog

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
2012-11-02 Richard Murray <murray@altura.local>
2+
3+
* src/xferfcn.py, src/statesp.py, src/lti.py, src/bdalg.py:
4+
constants must now be of type 'int' rather than 'long' (for python 3
5+
compatibility)
6+
7+
* tests/statefbk_test.py, tests/slycot_convert_test.py,
8+
tests/convert_test.py: added import __future__ calls for python 3
9+
compatibility; updated print calls
10+
11+
* src/modelsimp.py, src/xferfcn.py, src/delay.py, src/phaseplot.py,
12+
src/margins.py, src/statesp.py, src/mateqn.py: added import
13+
__future__ calls for python 3 compatibility
14+
115
2012-11-02 Richard Murray <murray@altura.local>
216

317
* src/xferfcn.py, src/timeresp.py, src/statesp.py, src/statefbk.py,

src/bdalg.py

Expand all lines: src/bdalg.py
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -215,18 +215,18 @@ def feedback(sys1, sys2, sign=-1):
215215
"""
216216

217217
# Check for correct input types.
218-
if not isinstance(sys1, (int, long, float, complex, tf.TransferFunction,
218+
if not isinstance(sys1, (int, float, complex, tf.TransferFunction,
219219
ss.StateSpace)):
220220
raise TypeError("sys1 must be a TransferFunction or StateSpace object, \
221221
or a scalar.")
222-
if not isinstance(sys2, (int, long, float, complex, tf.TransferFunction,
222+
if not isinstance(sys2, (int, float, complex, tf.TransferFunction,
223223
ss.StateSpace)):
224224
raise TypeError("sys2 must be a TransferFunction or StateSpace object, \
225225
or a scalar.")
226226

227227
# If sys1 is a scalar, convert it to the appropriate LTI type so that we can
228228
# A3E2 its feedback member function.
229-
if isinstance(sys1, (int, long, float, complex)):
229+
if isinstance(sys1, (int, float, complex)):
230230
if isinstance(sys2, tf.TransferFunction):
231231
sys1 = tf._convertToTransferFunction(sys1)
232232
elif isinstance(sys2, ss.StateSpace):

src/delay.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#! TODO: add module docstring
12
# delay.py - functions involving time delays
23
#
34
# Author: Sawyer Fuller
@@ -40,7 +41,8 @@
4041
#
4142
# $Id$
4243

43-
from __future__ import division
44+
# Python 3 compatability (needs to go here)
45+
from __future__ import print_function
4446

4547
def pade(T, n=1):
4648
"""

src/lti.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def timebase(sys, strict=True):
7575
set to False, dt = True will be returned as 1.
7676
"""
7777
# System needs to be either a constant or an Lti system
78-
if isinstance(sys, (int, float, long, complex)):
78+
if isinstance(sys, (int, float, complex)):
7979
return None
8080
elif not isinstance(sys, Lti):
8181
raise ValueError("Timebase not defined")
@@ -104,7 +104,7 @@ def timebaseEqual(sys1, sys2):
104104
def isdtime(sys, strict=False):
105105
# TODO: add docstring
106106
# Check to see if this is a constant
107-
if isinstance(sys, (int, float, long, complex)):
107+
if isinstance(sys, (int, float, complex)):
108108
# OK as long as strict checking is off
109109
return True if not strict else False
110110

@@ -121,7 +121,7 @@ def isdtime(sys, strict=False):
121121
def isctime(sys, strict=False):
122122
# TODO: add docstring
123123
# Check to see if this is a constant
124-
if isinstance(sys, (int, float, long, complex)):
124+
if isinstance(sys, (int, float, complex)):
125125
# OK as long as strict checking is off
126126
return True if not strict else False
127127

src/margins.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from __future__ import print_function
21
"""margin.py
32
43
Functions for computing stability margins and related functions.
@@ -9,6 +8,9 @@
98
margin.phase_crossover_frequencies
109
"""
1110

11+
# Python 3 compatability (needs to go here)
12+
from __future__ import print_function
13+
1214
"""Copyright (c) 2011 by California Institute of Technology
1315
All rights reserved.
1416

src/mateqn.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1-
# mateqn.py - matrix equation solvers (Lyapunov, Riccati)
2-
from __future__ import division
1+
""" mateqn.py
32
4-
""" Implementation of the functions lyap, dlyap, care and dare
3+
Matrix equation solvers (Lyapunov, Riccati)
4+
5+
Implementation of the functions lyap, dlyap, care and dare
56
for solution of Lyapunov and Riccati equations. """
67

8+
# Python 3 compatability (needs to go here)
9+
from __future__ import print_function
10+
711
"""Copyright (c) 2011, All rights reserved.
812
913
Redistribution and use in source and binary forms, with or without

src/modelsimp.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#! TODO: add module docstring
12
# modelsimp.py - tools for model simplification
23
#
34
# Author: Steve Brunton, Kevin Chen, Lauren Padilla
@@ -39,6 +40,9 @@
3940
#
4041
# $Id$
4142

43+
# Python 3 compatability
44+
from __future__ import print_function
45+
4246
# External packages and modules
4347
import numpy as np
4448
import control.ctrlutil as ctrlutil

src/phaseplot.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#! TODO: add module docstring
12
# phaseplot.py - generate 2D phase portraits
23
#
34
# Author: Richard M. Murray
@@ -33,7 +34,9 @@
3334
# IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3435
# POSSIBILITY OF SUCH DAMAGE.
3536

37+
# Python 3 compatability
3638
from __future__ import print_function
39+
3740
import numpy as np
3841
import matplotlib.pyplot as mpl
3942
from matplotlib.mlab import frange, find

src/statesp.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@
3333
3434
"""
3535

36+
# Python 3 compatability (needs to go here)
37+
from __future__ import print_function
38+
3639
"""Copyright (c) 2010 by California Institute of Technology
3740
All rights reserved.
3841
@@ -231,7 +234,7 @@ def __add__(self, other):
231234
"""Add two LTI systems (parallel connection)."""
232235

233236
# Check for a couple of special cases
234-
if (isinstance(other, (int, long, float, complex))):
237+
if (isinstance(other, (int, float, complex))):
235238
# Just adding a scalar; put it in the D matrix
236239
A, B, C = self.A, self.B, self.C;
237240
D = self.D + other;
@@ -288,7 +291,7 @@ def __mul__(self, other):
288291
"""Multiply two LTI objects (serial connection)."""
289292

290293
# Check for a couple of special cases
291-
if isinstance(other, (int, long, float, complex)):
294+
if isinstance(other, (int, float, complex)):
292295
# Just multiplying by a scalar; change the output
293296
A, B = self.A, self.B
294297
C = self.C * other
@@ -329,7 +332,7 @@ def __rmul__(self, other):
329332
"""Right multiply two LTI objects (serial connection)."""
330333

331334
# Check for a couple of special cases
332-
if isinstance(other, (int, long, float, complex)):
335+
if isinstance(other, (int, float, complex)):
333336
# Just multiplying by a scalar; change the input
334337
A, C = self.A, self.C;
335338
B = self.B * other;
@@ -551,7 +554,7 @@ def _convertToStateSpace(sys, **kw):
551554
return StateSpace(lti_sys.A, lti_sys.B, lti_sys.C, lti_sys.D,
552555
sys.dt)
553556

554-
elif isinstance(sys, (int, long, float, complex)):
557+
elif isinstance(sys, (int, float, complex)):
555558
if "inputs" in kw:
556559
inputs = kw["inputs"]
557560
else:

src/xferfcn.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from __future__ import print_function
21
"""xferfcn.py
32
43
Transfer function representation and functions.
@@ -35,6 +34,9 @@
3534
3635
"""
3736

37+
# Python 3 compatability (needs to go here)
38+
from __future__ import print_function
39+
3840
"""Copyright (c) 2010 by California Institute of Technology
3941
All rights reserved.
4042
@@ -142,15 +144,15 @@ def __init__(self, *args):
142144
# this is a shallow copy! This should be okay, but be careful.
143145
data = [num, den]
144146
for i in range(len(data)):
145-
if isinstance(data[i], (int, float, long, complex)):
147+
if isinstance(data[i], (int, float, complex)):
146148
# Convert scalar to list of list of array.
147149
if (isinstance(data[i], int)):
148150
# Convert integers to floats at this point
149151
data[i] = [[array([data[i]], dtype=float)]]
150152
else:
151153
data[i] = [[array([data[i]])]]
152154
elif (isinstance(data[i], (list, tuple, ndarray)) and
153-
isinstance(data[i][0], (int, float, long, complex))):
155+
isinstance(data[i][0], (int, float, complex))):
154156
# Convert array to list of list of array.
155157
if (isinstance(data[i][0], int)):
156158
# Convert integers to floats at this point
@@ -161,7 +163,7 @@ def __init__(self, *args):
161163
elif (isinstance(data[i], list) and
162164
isinstance(data[i][0], list) and
163165
isinstance(data[i][0][0], (list, tuple, ndarray)) and
164-
isinstance(data[i][0][0][0], (int, float, long, complex))):
166+
isinstance(data[i][0][0][0], (int, float, complex))):
165167
# We might already have the right format. Convert the
166168
# coefficient vectors to arrays, if necessary.
167169
for j in range(len(data[i])):
@@ -362,7 +364,7 @@ def __mul__(self, other):
362364
"""Multiply two LTI objects (serial connection)."""
363365

364366
# Convert the second argument to a transfer function.
365-
if isinstance(other, (int, float, long, complex)):
367+
if isinstance(other, (int, float, complex)):
366368
other = _convertToTransferFunction(other, inputs=self.inputs,
367369
outputs=self.inputs)
368370
else:
@@ -412,7 +414,7 @@ def __rmul__(self, other):
412414
def __div__(self, other):
413415
"""Divide two LTI objects."""
414416

415-
if isinstance(other, (int, float, long, complex)):
417+
if isinstance(other, (int, float, complex)):
416418
other = _convertToTransferFunction(other, inputs=self.inputs,
417419
outputs=self.inputs)
418420
else:
@@ -440,7 +442,7 @@ def __div__(self, other):
440442
# TODO: Division of MIMO transfer function objects is not written yet.
441443
def __rdiv__(self, other):
442444
"""Right divide two LTI objects."""
443-
if isinstance(other, (int, float, long, complex)):
445+
if isinstance(other, (int, float, complex)):
444446
other = _convertToTransferFunction(other, inputs=self.inputs,
445447
outputs=self.inputs)
446448
else:
@@ -906,7 +908,7 @@ def _convertToTransferFunction(sys, **kw):
906908

907909
return TransferFunction(num, den, sys.dt)
908910

909-
elif isinstance(sys, (int, long, float, complex)):
911+
elif isinstance(sys, (int, float, complex)):
910912
if "inputs" in kw:
911913
inputs = kw["inputs"]
912914
else:

tests/convert_test.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
1515
"""
1616

17+
from __future__ import print_function
1718
import unittest
1819
import numpy as np
1920
import control
@@ -38,8 +39,8 @@ def printSys(self, sys, ind):
3839
"""Print system to the standard output."""
3940

4041
if self.debug:
41-
print "sys%i:\n" % ind
42-
print sys
42+
print("sys%i:\n" % ind)
43+
print(sys)
4344

4445
def testConvert(self):
4546
"""Test state space to transfer function conversion."""
@@ -64,12 +65,12 @@ def testConvert(self):
6465
Cmat = control.ctrb(ssOriginal.A, ssOriginal.B)
6566
if (np.linalg.matrix_rank(Cmat) != states):
6667
if (verbose):
67-
print " skipping (not reachable)"
68+
print(" skipping (not reachable)")
6869
continue
6970
Omat = control.obsv(ssOriginal.A, ssOriginal.C)
7071
if (np.linalg.matrix_rank(Omat) != states):
7172
if (verbose):
72-
print " skipping (not observable)"
73+
print(" skipping (not observable)")
7374
continue
7475

7576
tfOriginal = matlab.tf(ssOriginal)
@@ -86,18 +87,18 @@ def testConvert(self):
8687

8788
# Check to see if the state space systems have same dim
8889
if (ssOriginal.states != ssTransformed.states):
89-
print "WARNING: state space dimension mismatch: " + \
90+
print("WARNING: state space dimension mismatch: " + \
9091
"%d versus %d" % \
91-
(ssOriginal.states, ssTransformed.states)
92+
(ssOriginal.states, ssTransformed.states))
9293

9394
# Now make sure the frequency responses match
9495
# Since bode() only handles SISO, go through each I/O pair
9596
# For phase, take sine and cosine to avoid +/- 360 offset
9697
for inputNum in range(inputs):
9798
for outputNum in range(outputs):
9899
if (verbose):
99-
print "Checking input %d, output %d" \
100-
% (inputNum, outputNum)
100+
print("Checking input %d, output %d" \
101+
% (inputNum, outputNum))
101102
ssorig_mag, ssorig_phase, ssorig_omega = \
102103
control.bode(_mimo2siso(ssOriginal, \
103104
inputNum, outputNum), \

tests/matlab_test.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
# nothing crashes. It doesn't test actual functionality; the module
99
# specific unit tests will do that.
1010

11+
from __future__ import print_function
1112
import unittest
1213
import numpy as np
1314
import scipy as sp
@@ -208,14 +209,14 @@ def testDcgain(self):
208209
gain_numden = dcgain(np.squeeze(num), den)
209210
gain_sys_ss = dcgain(sys_ss)
210211
print
211-
print 'gain_abcd:', gain_abcd, 'gain_zpk:', gain_zpk
212-
print 'gain_numden:', gain_numden, 'gain_sys_ss:', gain_sys_ss
212+
print('gain_abcd:', gain_abcd, 'gain_zpk:', gain_zpk)
213+
print('gain_numden:', gain_numden, 'gain_sys_ss:', gain_sys_ss)
213214

214215
#Compute the gain with a long simulation
215216
t = linspace(0, 1000, 1000)
216217
y, _t = step(sys_ss, t)
217218
gain_sim = y[-1]
218-
print 'gain_sim:', gain_sim
219+
print('gain_sim:', gain_sim)
219220

220221
#All gain values must be approximately equal to the known gain
221222
np.testing.assert_array_almost_equal(
@@ -225,7 +226,7 @@ def testDcgain(self):
225226

226227
#Test with MIMO system, which contains ``siso_ss1`` twice
227228
gain_mimo = dcgain(self.mimo_ss1)
228-
print 'gain_mimo: \n', gain_mimo
229+
print('gain_mimo: \n', gain_mimo)
229230
np.testing.assert_array_almost_equal(gain_mimo, [[59., 0 ],
230231
[0, 59.]])
231232

tests/slycot_convert_test.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# slycot_convert_test.py - test SLICOT-based conversions
44
# RMM, 30 Mar 2011 (based on TestSlycot from v0.4a)
55

6+
from __future__ import print_function
67
import unittest
78
import numpy as np
89
import control.matlab as matlab
@@ -36,11 +37,11 @@ def testTF(self, verbose=False):
3637
for testNum in range(self.numTests):
3738
ssOriginal = matlab.rss(states, inputs, outputs)
3839
if (verbose):
39-
print '====== Original SS =========='
40-
print ssOriginal
41-
print 'states=',states
42-
print 'inputs=',inputs
43-
print 'outputs=',outputs
40+
print('====== Original SS ==========')
41+
print(ssOriginal)
42+
print('states=', states)
43+
print('inputs=', inputs)
44+
print('outputs=', outputs)
4445

4546

4647
tfOriginal_Actrb, tfOriginal_Bctrb, tfOriginal_Cctrb, tfOrigingal_nctrb, tfOriginal_index,\
@@ -55,8 +56,8 @@ def testTF(self, verbose=False):
5556
inputs,outputs,ssTransformed_A, ssTransformed_B, ssTransformed_C,ssTransformed_D,tol1=0.0)
5657
#print 'size(Trans_A)=',ssTransformed_A.shape
5758
if (verbose):
58-
print '===== Transformed SS =========='
59-
print matlab.ss(ssTransformed_A, ssTransformed_B, ssTransformed_C, ssTransformed_D)
59+
print('===== Transformed SS ==========')
60+
print(matlab.ss(ssTransformed_A, ssTransformed_B, ssTransformed_C, ssTransformed_D))
6061
# print 'Trans_nr=',ssTransformed_nr
6162
# print 'tfOrig_index=',tfOriginal_index
6263
# print 'tfOrig_ucoeff=',tfOriginal_ucoeff

0 commit comments

Comments
 (0)
0