Skip to content

Commit

Permalink
add clear() method
Browse files Browse the repository at this point in the history
  • Loading branch information
xflr6 committed Jun 28, 2017
1 parent c419658 commit 2363e08
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGES
Expand Up @@ -5,6 +5,8 @@ Changelog
Version 0.8 (in development)
----------------------------

Add clear()-method for Graph and Digraph.

Add grapviz.version() function.

Drop dot source extra indent for edge statements following dotguide examples.
Expand Down
4 changes: 2 additions & 2 deletions docs/api.rst
Expand Up @@ -18,7 +18,7 @@ Graph
source,
node, edge, edges, attr, subgraph,
format, engine, encoding,
copy, pipe, save, render, view
clear, copy, pipe, save, render, view


Digraph
Expand All @@ -29,7 +29,7 @@ Digraph
source,
node, edge, edges, attr, subgraph,
format, engine, encoding,
copy, pipe, save, render, view
clear, copy, pipe, save, render, view


Source
Expand Down
11 changes: 11 additions & 0 deletions graphviz/dot.py
Expand Up @@ -79,6 +79,17 @@ def _kwargs(self):
})
return result

def clear(self, keep_attrs=False):
"""Reset content to an empty body, clear graph/node/egde_attr mappings.
Args:
keep_attrs(bool): preserve graph/node/egde_attr mappings
"""
if not keep_attrs:
for a in (self.graph_attr, self.node_attr, self.edge_attr):
a.clear()
self.body[:] = []

def __iter__(self, subgraph=False):
"""Yield the DOT source code line by line (as graph or subgraph)."""
if self.comment:
Expand Down
18 changes: 18 additions & 0 deletions tests/test_dot.py
Expand Up @@ -39,6 +39,24 @@ def test__repr_svg_(mocker, cls):
pipe.return_value.decode.assert_called_once_with(c.encoding)


@pytest.mark.parametrize('keep_attrs', [False, True])
def test_clear(cls, keep_attrs):
kwargs = {'%s_attr' % a: {a: a} for a in ('graph', 'node', 'edge')}
c = cls(**kwargs)
assert all(getattr(c, k) == v for k, v in kwargs.items())
c.node('spam')
assert len(c.body) == 1
body = c.body

c.clear(keep_attrs)
assert c.body == []
assert c.body is body
if keep_attrs:
assert all(getattr(c, k) == v for k, v in kwargs.items())
else:
assert all(getattr(c, k) == {} for k, v in kwargs.items())


def test_iter_subgraph_strict(cls):
with pytest.raises(ValueError, match=r'strict'):
cls().subgraph(cls(strict=True))
Expand Down

0 comments on commit 2363e08

Please sign in to comment.