00001 __copyright__ = """
00002 Copyright 2008 Sean Ross-Ross
00003 """
00004 __license__ = """
00005 This file is part of SLIMpy .
00006
00007 SLIMpy is free software: you can redistribute it and/or modify
00008 it under the terms of the GNU Lesser General Public License as published by
00009 the Free Software Foundation, either version 3 of the License, or
00010 (at your option) any later version.
00011
00012 SLIMpy is distributed in the hope that it will be useful,
00013 but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00015 GNU Lesser General Public License for more details.
00016
00017 You should have received a copy of the GNU Lesser General Public License
00018 along with SLIMpy . If not, see <http://www.gnu.org/licenses/>.
00019 """
00020
00021
00022 even = lambda x: not bool(x%2)
00023
00024 class GraphPrinter(object):
00025
00026
00027 @staticmethod
00028 def nodeToString(node):
00029 return str(node)
00030
00031 @staticmethod
00032 def _print(*things):
00033 return map(str,list( things ) )
00034
00035
00036 @classmethod
00037 def printAdj( cls, graph, v, depth=0 , fetch=False ):
00038 s = []
00039 extend = s.extend
00040 lines = cls._print ( "%s+ %s" %( "".join( [' ']*depth ) , cls.nodeToString(v) ) )
00041 extend(lines)
00042 for u in graph.adj( v ):
00043 lines = cls.printAdj(graph, u, depth+1 )
00044 extend(lines)
00045 return "\n".join(s)
00046
00047 @classmethod
00048 def printInvAdj( cls, graph, v, spacer="" , fetch=False ):
00049
00050 s = []
00051 extend = s.extend
00052 push = s.append
00053
00054
00055 lines = cls._print ( "%s+-%s" %( spacer, cls.nodeToString(v) ) )
00056 extend(lines)
00057 if not even(len(spacer)/3):
00058 spacer += '| '
00059 else:
00060 spacer += ' '
00061
00062 for u in graph.invAdj( v ):
00063 lines = cls.printInvAdj(graph, u, spacer )
00064 push(lines)
00065 return "\n".join(s)
00066
00067 @classmethod
00068 def printDep( cls, graph, fetch=False ):
00069 s = []
00070 extend = s.extend
00071
00072 lines = cls._print ( "---------Graph---------" )
00073 extend(lines)
00074 for u in graph :
00075 lines = cls._print ( '+' , cls.nodeToString(u) )
00076 extend(lines)
00077 for v in graph.adj( u ):
00078 lines = cls._print ( '--+', cls.nodeToString(v),"colour = %s , Etype=%s" %(graph.getEdgeColour(u, v),graph.getEdgeType(u, v)) )
00079 extend(lines)
00080 lines = cls._print ( "------Graph--End-------" )
00081 extend(lines)
00082 return "\n".join(s)
00083
00084 @classmethod
00085 def printInvDep( cls, graph, fetch=False ):
00086 s = []
00087 extend = s.extend
00088
00089 lines = cls._print ( "---------Graph---------" )
00090 extend(lines)
00091 for u in graph:
00092 lines = cls._print ( '+' + cls.nodeToString(u) )
00093 extend(lines)
00094 for v in graph.invAdj( u ):
00095 lines = cls._print ( '--+' +cls.nodeToString(v) )
00096 extend(lines)
00097
00098 lines = cls._print ( "------Graph--End-------" )
00099 extend(lines)
00100 return "\n".join(s)
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128