xflr6 / graphviz

Simple Python interface for Graphviz
https://graphviz.readthedocs.io
MIT License
1.65k stars 213 forks source link

Add statistics for node and edge statements procuded #120

Open PhilippWillms opened 4 years ago

PhilippWillms commented 4 years ago

After adding edges and nodes via the straight-forward methods of classes "Graph" and "Digraph", it would be highly helpful to quickly retrieve the total count of nodes and edges via getter-methods. Currently, I just see the "source"-method to get dot source code, but this rather provides a list of edges than the integer count. The latter one would be relevant for consistency checks and assertions.

xflr6 commented 4 years ago

Thanks for the proposal.

Re: testing your code, I think you might want to go further than checking numbers of calls. This can be done by using the mock module as a 'spy' with the wraps keyword (you can assert on number of calls but also unpack arguments to make more specific assertions and hopefully also get useful diffing, see https://docs.python.org/3/library/unittest.mock.html):

In [1]: import mock
   ...: 
   ...: import graphviz
   ...: 
   ...: g = graphviz.Graph()
   ...: 
   ...: with mock.patch.object(g, 'node', wraps=g.node) as node_spy,\
   ...:     mock.patch.object(g, 'edge', wraps=g.edge) as edge_spy:
   ...:     g.node('spam')
   ...:     g.edge('spam', 'eggs')
   ...: 
   ...: node_spy.mock_calls, edge_spy.mock_calls
Out[1]: ([call('spam')], [call('spam', 'eggs')])
In [2]: g
Out[2]: 

spam

At the same time, I totally agree that it would be nice, e.g. for the repr() of a Graph instance to include the node and edge count (in general, statistics about the kinds of statements produced), and I also think that it would be nice to make them easily accessible (probably via properties). I currently don't see any blocker, marking this as enhancement.

Would you like to contribute here?