[MATRIX-SIG] namespaces, constructors, and global variables
Phil Austin
phil@geog.ubc.ca
09 Jul 1997 09:34:23 -0700
The following message is a courtesy copy of an article
that has been posted as well.
We're working on a generic plotting interface to gnuplot, gist, ncar
graphics, etc., and are currently stumped by our lack of understanding
of namespaces. The interface puts a plot together by creating a
series of objects (plots, axes, ticks, legends, etc.) with methods to
add or modify fonts, colors, line widths, whatever. The state of the
plot is stored in a global dictionary, and an execute method goes
through the dictionary and builds the plot for the appropriate
software package. An example:
>>> from PlotAll import *
>>> plotIt=PlotDriver(device="screen")
>>> plot1=BuildPlot(time, xarray)
>>> leftAxis=BuildAxis(plot1,axis="left",label="xarray")
>>> print Master['plot1']['leftAxis']['label']
xarray
Our problem is that we would like a botAxis and a leftAxis object to
be created by default when a plot object is instantiated without the
argument "axes=False". I'm appending a version of PlotAll.py, which
does this without a hitch from within the interpreter:
>>> import PlotAll
>>> PlotAll.runit()
Master.keys(): ['plotCount', 'device', 'plot1']
inside BuildAxis: Master.keys()= ['plotCount', 'device', 'plot1']
inside BuildAxis: Master.keys()= ['plotCount', 'device', 'plot1']
Bottom: Master['plot1']['leftAxis']['label']= ytest
>>> leftAxis
<BuildAxis instance at 820e290>
Running from the shell, however, doesn't work, because the global
variable Master doesn't appear to have it's keys:
<phil@curlew: src.dir/python.dir> python PlotAll.py
Master.keys(): ['plotCount', 'device', 'plot1']
inside BuildAxis: Master.keys()= []
Can someone walk me through why this is failing, and how I can fix it?
Thanks, Phil
______________________________PlotAll.py_______________________________
Master={}
global Master
class PlotDriver:
global Master
def __init__(self,device="postscript"):
self.RCSId="$Id: PlotAll.py,v 1.5 1997/07/09 16:31:05 phil Exp $"
Master['plotCount']=0
Master['device']=device
class BuildPlot:
global Master
def __init__(self,xdata,ydata,plotType="linePlot",axes="default"):
Master['plotCount'] = Master['plotCount'] + 1
self.plotName="plot%d" % Master['plotCount']
Master[self.plotName]={}
Master[self.plotName]['xdata']=xdata
Master[self.plotName]['ydata']=ydata
Master[self.plotName]['plotType']=plotType
if(axes=="default"):
import __main__
plot=self
global Master
ns=vars()
print "Master.keys(): ", Master.keys()
code=["from PlotAll import *",
"global Master",
"leftAxis=BuildAxis(plot,axis=\"left\",label=\"ytest\")",
"botAxis=BuildAxis(plot,axis=\"bot\",label=\"x\")"]
for stmt in code:
exec stmt in ns,__main__.__dict__
class BuildAxis:
global Master
def __init__(self,Plot,axis="left",label="default",limit="default"):
global Master
print "inside BuildAxis: Master.keys()=", Master.keys()
self.plotName=Plot.plotName
self.axisName="%sAxis" % axis
Master[self.plotName][self.axisName]={}
Master[self.plotName][self.axisName]['label']=label
Master[self.plotName][self.axisName]['limit']=limit
def runit():
global Master
xarray=[1,2,3,4]
time=[5,6,7,8]
#controls items common to all plots
plotIt=PlotDriver(device="screen")
#build an individual plot
plot=BuildPlot(time, xarray)
print "Bottom: Master['plot1']['leftAxis']['label']=", Master['plot1']['leftAxis']['label']
if __name__ == "__main__":runit()
_______________
MATRIX-SIG - SIG on Matrix Math for Python
send messages to: matrix-sig@python.org
administrivia to: matrix-sig-request@python.org
_______________