[MATRIX-SIG] namespaces, constructors, and global variables

Konrad Hinsen hinsen@ibs.ibs.fr
Thu, 10 Jul 1997 11:39:04 +0200


> We're working on a generic plotting interface to gnuplot, gist, ncar
> graphics, etc., and are currently stumped by our lack of understanding

Great! However, I wonder whether a unification of plotting packages
is the right way to go. I think it would be not much more effort, but
much more useful, to write Python code that generates low-level graphics
elements (lines, circles, etc.) directly to make up a plot. Then there
could be several low-level drivers that render this representation
via Tk canvases, OpenGL, PostScript, or whatever.

This may sound like reinventing wheels, but given the problems and/or
limitations that all known plotting packages cause, it may be the
better way to happiness.

> 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

I would not recommend to use *any* global objects unless you have a
very good reason. If I understand your intention well, it would be
impossible to construct two plots in parallel. Why that restriction?
Just define a "plot" class that contains all the data.

> 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?

Without analyzing anything in detail, I'll point to a critical
spot:

> class BuildPlot:
...
> 	    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__

__main__ will clearly be different in the two situations. And like
global variables, accessing __main__ is something that a well-behaved
Python program should not do.

Konrad.
-- 
-------------------------------------------------------------------------------
Konrad Hinsen                          | E-Mail: hinsen@ibs.ibs.fr
Laboratoire de Dynamique Moleculaire   | Tel.: +33-4.76.88.99.28
Institut de Biologie Structurale       | Fax:  +33-4.76.88.54.94
41, av. des Martyrs                    | Deutsch/Esperanto/English/
38027 Grenoble Cedex 1, France         | Nederlands/Francais
-------------------------------------------------------------------------------

_______________
MATRIX-SIG  - SIG on Matrix Math for Python

send messages to: matrix-sig@python.org
administrivia to: matrix-sig-request@python.org
_______________