pretty printing graphs
Michele Simionato
mis6 at pitt.edu
Tue Jul 29 10:00:36 EDT 2003
Here is an enhanced version that also draws the metaclass instantiation
relationship:
import os
def label(i,n):
if n>1: return '[label="%s"]' % (i+1)
return ""
def dotMRO(cls):
"Generate the dot code for the MRO directed graph"
yield "digraph MRO_of_%s{\n" % cls.__name__
for c in cls.__mro__:
n=len(c.__bases__)
yield ' '.join([
' edge [style=solid]; %s -> %s %s;' %
(b.__name__,c.__name__,label(i,n))
for i,b in enumerate(c.__bases__)])
if type(c) is not type: yield \
' edge [style=dashed]; %s -> %s;' % (type(c).__name__,c.__name__)
yield '}'
class M(type): pass
O=object
class F(O): pass
class E(O): pass
class D(O): pass
class G(O): __metaclass__=M
class C(F,D,G): pass
class B(E,D): pass
class A(B,C): pass
dotcode='\n'.join(dotMRO(A)); print dotcode
os.system('echo "%s" | dot -Tps > prova.ps' % dotcode)
os.system('gv prova.ps&')
See the graph at
http://www.phyast.pitt.edu/~micheles/prova.ps
Here I had a problem due to my lack of knowledge of "dot":
class C inherits from F,D and G (in this order, see the labels
1,2,3). However, "dot" draws D before F and, without looking
at the labels, one would think that D comes before F, which
is not the case. IOW, the line FC should cross the line DC,
and the F-arrow should be on the left of the D-arrow.
Is there any way of fixing that?
TIA,
Michele
More information about the Python-list
mailing list