Esempio n. 1
0
 def clear(self):
     """Remove all nodes, edges, and attributes from the graph."""
     self.edge_attr.clear()
     self.node_attr.clear()
     self.graph_attr.clear()
     # now "close" existing graph and create a new graph
     name=gv.agnameof(self.handle)
     strict=self.strict
     directed=self.directed
     gv.agclose(self.handle) 
     self.handle=gv.agraphnew(name,strict,directed) 
Esempio n. 2
0
    def __init__(self, thing=None, file=None, name=None,
                 data=None, string=None, strict=True, directed=False,
                 handle=None,**attr):

        # atttempt to guess input type
        if isinstance(thing,dict):
            data=thing # a dictionary of dictionaries (or lists)
        if hasattr(thing,'own'): # a Swig pointer - graph handle
            handle=thing
        if self._is_string_like(thing): 
            if (thing.startswith("strict") or 
                thing.startswith("digraph") or 
                thing.startswith("graph")):
               string=thing # this is a dot format graph in a string
            else:
                file=thing  # assume this is a file name

        self.charset='UTF-8' # default charset encoding 

        # input type guessed or specified - now init graph                
        if handle is None:  # the graph pointer (handle)
            try:
                self.handle=gv.agraphnew(name,strict,directed) # new graph
            except TypeError:
                raise TypeError("Graph name must be a string: %s"%name)
        else:
            self.handle=handle # use pointer to exisiting graph

        # read from filename
        if file is not None:
            self.read(file)

        # load from dict of dicts or dict of lists
        if data is not None:
            for node in data:
                for nbr in data[node]:
                    self.add_edge(node,nbr)
            self.add_nodes_from(data.keys())        

        # load from string
        if string is not None:
            self.from_string(string)

        # assign any attributes specified through keywords
        self.graph_attr=Attribute(self.handle,0) # default graph attributes
        self.graph_attr.update(attr) # apply attributes passed to init
        self.node_attr=Attribute(self.handle,1)  # default node attributes
        self.edge_attr=Attribute(self.handle,2)  # default edge attribtes