Code Monkey home page Code Monkey logo

Comments (3)

xflr6 avatar xflr6 commented on August 22, 2024

The example code looks incorrect (base class must be called with instance as first argument; ClusterGraph, as defined, requires a string argument for instance creation). NB: latest version?

from graphviz import Graph

class ClusterGraph(Graph):
    def __init__(self, name):
        super(ClusterGraph, self).__init__('cluster_' + name)

g1 = Graph()
g2 = ClusterGraph('spam')
g1.subgraph(g2)
print g1.source + g2.source
graph {
    subgraph cluster_spam {
    }
}
graph cluster_spam {
}

Consider passing **kwargs down to the base class.
The type check isinstance(graph, self.__class__) succedes with subclass instances. It prevents putting a graph into a subgraph when issubclass(InnerGraph, OuterGraph) holds (also prevents mixing directed and undirected graphs).
Is there a specific reason requiring the outer graph to be a subclass of the inner graph?

from graphviz.

dizcza avatar dizcza commented on August 22, 2024

I have the same error. It's very sad that the users cannot subclass the graph.

import graphviz

class GraphArea(graphviz.Digraph):
    def __init__(self, **kwargs):
        super().__init__(name='graph', format='svg',
                         graph_attr=dict(rankdir='LR'),
                         node_attr=dict(shape='box'))
        with self.subgraph(name="cluster_0") as c:
            print(c)


GraphArea()

Recursion error.

from graphviz.

xflr6 avatar xflr6 commented on August 22, 2024

Thanks. AFAICT subclassing works as intended. I understand the use case is to define defaults, e.g.:

In [1]: import graphviz
   ...: 
   ...: class GraphArea(graphviz.Digraph):
   ...:     def __init__(self,
   ...:                  name='graph',
   ...:                  format='svg',
   ...:                  graph_attr={'rankdir': 'LR'},
   ...:                  node_attr={'shape': 'box'},
   ...:                  **kwargs):
   ...:         super().__init__(name=name,
   ...:                          format=format,
   ...:                          graph_attr=graph_attr,
   ...:                          node_attr=node_attr,
   ...:                          **kwargs)
   ...:                          
   ...:         
   ...: 
   ...: g = GraphArea()
   ...: with g.subgraph(name="cluster_0") as s:
   ...:     s.node('spam')
   ...: print(g)
digraph "graph" {
	graph [rankdir=LR]
	node [shape=box]
	subgraph cluster_0 {
		spam
	}
}

Note that in the code you have given GraphArea silently ignores any given arguments (not passing kwargs).

If setting defaults is all that is needed, one can do it much simpler with functools.partial:

import functools

GraphArea = functools.partial(graphviz.Digraph,
                              name='graph',
                              format='svg',
                              graph_attr={'rankdir': 'LR'},
                              node_attr={'shape': 'box'})

It understand you might want to precreate nodes/edges and possibly subgraphs in __init__. Not sure if that makes sense (maybe a (factory) function, e.g. in form of an alternate constructor would be much simpler), but that does work for nodes and edges (with subgraphs created by that class also containing the precreated content): You are getting RecursionError from adding a subgraph in the initializer, which means that this subgraph again creates a subgraph and so on recursively, so I think the error is correct here.

from graphviz.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.