Skip to content

Commit c2bf84d

Browse files
committed
Clean up imports
Define __all__ variables in the modules so that from .<module> import * imports only the functions/classes we use. This is probably a little better than listing them explicitly in __init__.py, since then if we add a new function to a module, we only need to change the __all__ variable in the same module, rather than edit __init__.py. Furthermore it avoids duplicating some of these listings in matlab/__init__.py.
1 parent 9ded2bd commit c2bf84d

11 files changed

Lines changed: 38 additions & 39 deletions

File tree

control/__init__.py

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -45,28 +45,26 @@
4545
"""
4646

4747
# Import functions from within the control system library
48-
# Should probably only import the exact functions we use...
49-
from .bdalg import series, parallel, negate, feedback
50-
from .delay import pade
48+
# Note: the functions we use are specified as __all__ variables in the modules
49+
from .bdalg import *
50+
from .delay import *
5151
from .dtime import *
52-
from .freqplot import bode_plot, nyquist_plot, gangof4_plot
53-
from .freqplot import bode, nyquist, gangof4
52+
from .freqplot import *
5453
from .lti import *
5554
from .margins import *
56-
from .mateqn import lyap, dlyap, care, dare
57-
from .modelsimp import hsvd, modred, balred, era, markov, minreal
55+
from .mateqn import *
56+
from .modelsimp import *
5857
from .nichols import *
59-
from .phaseplot import phase_plot, box_grid
60-
from .pzmap import pzmap
58+
from .phaseplot import *
59+
from .pzmap import *
6160
from .rlocus import *
62-
from .statefbk import place, lqr, ctrb, obsv, gram, acker
61+
from .statefbk import *
6362
from .statesp import *
64-
from .timeresp import forced_response, initial_response, step_response, \
65-
impulse_response
63+
from .timeresp import *
6664
from .xferfcn import *
6765
from .ctrlutil import *
6866
from .frdata import *
69-
from .canonical import canonical_form, reachable_form
67+
from .canonical import *
7068

7169
# Exceptions
7270
from .exception import *
@@ -77,16 +75,6 @@
7775
except ImportError:
7876
__version__ = "dev"
7977

80-
# Import some of the more common (and benign) MATLAB shortcuts
81-
# By default, don't import conflicting commands here
82-
#! TODO (RMM, 4 Nov 2012): remove MATLAB dependencies from __init__.py
83-
#!
84-
#! Eventually, all functionality should be in modules *other* than matlab.
85-
#! This will allow inclusion of the matlab module to set up a different set
86-
#! of defaults from the main package. At that point, the matlab module will
87-
#! allow provide compatibility with MATLAB but no package functionality.
88-
#!
89-
9078
# The following is to use Numpy's testing framework
9179
# Tests go under directory tests/, benchmarks under directory benchmarks/
9280
from numpy.testing import Tester

control/bdalg.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@
5858
from . import statesp as ss
5959
from . import frdata as frd
6060

61+
__all__ = ['series', 'parallel', 'negate', 'feedback', 'append', 'connect']
62+
6163
def series(sys1, sys2):
6264
"""Return the series connection sys2 * sys1 for --> sys1 --> sys2 -->.
6365

control/canonical.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from numpy import zeros, shape, poly
1010
from numpy.linalg import inv
1111

12+
__all__ = ['canonical_form', 'reachable_form']
1213

1314
def canonical_form(xsys, form='reachable'):
1415
"""Convert a system into canonical form

control/delay.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444
# Python 3 compatability (needs to go here)
4545
from __future__ import print_function
4646

47+
__all__ = ['pade']
48+
4749
def pade(T, n=1):
4850
"""
4951
Create a linear system that approximates a delay.

control/freqplot.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@
4949
from .bdalg import feedback
5050
from .lti import isdtime, timebaseEqual
5151

52+
__all__ = ['bode_plot', 'nyquist_plot', 'gangof4_plot',
53+
'bode', 'nyquist', 'gangof4']
54+
5255
#
5356
# Main plotting functions
5457
#

control/mateqn.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@
4646
from scipy.linalg import eigvals, solve_discrete_are
4747
from .exception import ControlSlycot, ControlArgument
4848

49+
__all__ = ['lyap', 'dlyap', 'dare', 'care']
50+
4951
#### Lyapunov equation solvers lyap and dlyap
5052

5153
def lyap(A,Q,C=None,E=None):

control/matlab/__init__.py

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,6 @@
5050
5151
"""
5252

53-
# Libraries that we make use of
54-
import scipy as sp # SciPy library (used all over)
55-
import numpy as np # NumPy library
56-
import re # regular expressions
57-
5853
# Import MATLAB-like functions that are defined in other packages
5954
from scipy.signal import zpk2ss, ss2zpk, tf2zpk, zpk2tf
6055
from numpy import linspace, logspace
@@ -76,19 +71,19 @@
7671
from ..xferfcn import *
7772
from ..lti import *
7873
from ..frdata import *
79-
from ..dtime import sample_system
74+
from ..dtime import *
8075
from ..exception import ControlArgument
8176

8277
# Import MATLAB-like functions that can be used as-is
8378
from ..ctrlutil import *
8479
from ..freqplot import nyquist, gangof4
85-
from ..nichols import nichols, nichols_grid
86-
from ..bdalg import series, parallel, negate, feedback, append, connect
87-
from ..pzmap import pzmap
88-
from ..statefbk import ctrb, obsv, gram, place, lqr
89-
from ..delay import pade
90-
from ..modelsimp import hsvd, balred, modred, minreal
91-
from ..mateqn import lyap, dlyap, dare, care
80+
from ..nichols import nichols
81+
from ..bdalg import *
82+
from ..pzmap import *
83+
from ..statefbk import *
84+
from ..delay import *
85+
from ..modelsimp import *
86+
from ..mateqn import *
9287
from ..margins import margin
9388
from ..rlocus import rlocus
9489
from ..dtime import c2d

control/modelsimp.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@
5050
from .statesp import StateSpace
5151
from .statefbk import gram
5252

53+
__all__ = ['hsvd', 'balred', 'modred', 'era', 'markov', 'minreal']
54+
5355
# Hankel Singular Value Decomposition
5456
# The following returns the Hankel singular values, which are singular values
5557
#of the matrix formed by multiplying the controllability and observability

control/phaseplot.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343
from scipy.integrate import odeint
4444
from .exception import ControlNotImplemented
4545

46+
__all__ = ['phase_plot', 'box_grid']
47+
4648
def phase_plot(odefun, X=None, Y=None, scale=1, X0=None, T=None,
4749
lingrid=None, lintime=None, logtime=None, timepts=None,
4850
parms=(), verbose=True):

control/pzmap.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,11 @@
4040
#
4141
# $Id:pzmap.py 819 2009-05-29 21:28:07Z murray $
4242

43-
import matplotlib.pyplot as plt
44-
#import scipy as sp
45-
#import numpy as np
4643
from numpy import real, imag
4744
from .lti import LTI
4845

46+
__all__ = ['pzmap']
47+
4948
# TODO: Implement more elegant cross-style axes. See:
5049
# http://matplotlib.sourceforge.net/examples/axes_grid/demo_axisline_style.html
5150
# http://matplotlib.sourceforge.net/examples/axes_grid/demo_curvelinear_grid.html
@@ -75,6 +74,7 @@ def pzmap(sys, Plot=True, title='Pole Zero Map'):
7574
zeros = sys.zero()
7675

7776
if (Plot):
77+
import matplotlib.pyplot as plt
7878
# Plot the locations of the poles and zeros
7979
if len(poles) > 0:
8080
plt.scatter(real(poles), imag(poles), s=50, marker='x')

0 commit comments

Comments
 (0)