Skip to content

Commit

Permalink
Merge 34c3cce into 36120f2
Browse files Browse the repository at this point in the history
  • Loading branch information
jarrodmillman committed Aug 3, 2018
2 parents 36120f2 + 34c3cce commit 9a6cc0f
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 22 deletions.
6 changes: 5 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@ python:
- "2.7"
- "3.4"
- "3.5"
- "3.6-dev"
- "3.6"
- "pypy"
addons:
apt:
packages:
- graphviz
matrix:
include:
- os: linux
python: 3.7
dist: xenial
sudo: true
- os: osx
language: generic
before_install: brew install graphviz
Expand Down
79 changes: 58 additions & 21 deletions pygraphviz/agraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,8 +374,10 @@ def nodes_iter(self):
nh = gv.agfstnode(self.handle)
while nh is not None:
yield Node(self, nh=nh)
nh = gv.agnxtnode(self.handle, nh)
raise StopIteration
try:
nh = gv.agnxtnode(self.handle, nh)
except StopIteration:
return

iternodes = nodes_iter

Expand Down Expand Up @@ -597,8 +599,10 @@ def neighbors_iter(self, n):
yield Node(self, t)
else:
yield Node(self, s)
eh = gv.agnxtedge(self.handle, eh, nh)
raise StopIteration
try:
eh = gv.agnxtedge(self.handle, eh, nh)
except StopIteration:
return

def neighbors(self, n):
"""Return a list of the nodes attached to n."""
Expand Down Expand Up @@ -627,8 +631,14 @@ def out_edges_iter(self, nbunch=None, keys=False):
yield (e[0], e[1], e.name)
else:
yield e
eh = gv.agnxtout(self.handle, eh)
nh = gv.agnxtnode(self.handle, nh)
try:
eh = gv.agnxtout(self.handle, eh)
except StopIteration:
break
try:
nh = gv.agnxtnode(self.handle, nh)
except StopIteration:
return
elif nbunch in self: # if nbunch is a single node
n = Node(self, nbunch)
nh = n.handle
Expand All @@ -639,7 +649,10 @@ def out_edges_iter(self, nbunch=None, keys=False):
yield (e[0], e[1], e.name)
else:
yield e
eh = gv.agnxtout(self.handle, eh)
try:
eh = gv.agnxtout(self.handle, eh)
except StopIteration:
return
else: # if nbunch is a sequence of nodes
try:
bunch = [n for n in nbunch if n in self]
Expand All @@ -657,8 +670,10 @@ def out_edges_iter(self, nbunch=None, keys=False):
yield (e[0], e[1], e.name)
else:
yield e
eh = gv.agnxtout(self.handle, eh)
raise StopIteration
try:
eh = gv.agnxtout(self.handle, eh)
except StopIteration:
break


iteroutedges = out_edges_iter
Expand All @@ -683,8 +698,14 @@ def in_edges_iter(self, nbunch=None, keys=False):
yield (e[0], e[1], e.name)
else:
yield e
eh = gv.agnxtin(self.handle, eh)
nh = gv.agnxtnode(self.handle, nh)
try:
eh = gv.agnxtin(self.handle, eh)
except StopIteration:
break
try:
nh = gv.agnxtnode(self.handle, nh)
except StopIteration:
return
elif nbunch in self: # if nbunch is a single node
n = Node(self, nbunch)
nh = n.handle
Expand All @@ -695,7 +716,10 @@ def in_edges_iter(self, nbunch=None, keys=False):
yield (e[0], e[1], e.name)
else:
yield e
eh = gv.agnxtin(self.handle, eh)
try:
eh = gv.agnxtin(self.handle, eh)
except StopIteration:
break
else: # if nbunch is a sequence of nodes
try:
bunch = [n for n in nbunch if n in self]
Expand All @@ -713,8 +737,10 @@ def in_edges_iter(self, nbunch=None, keys=False):
yield (e[0], e[1], e.name)
else:
yield e
eh = gv.agnxtin(self.handle, eh)
raise StopIteration
try:
eh = gv.agnxtin(self.handle, eh)
except StopIteration:
break

def edges_iter(self, nbunch=None, keys=False):
"""Return iterator over edges in the graph.
Expand Down Expand Up @@ -780,8 +806,10 @@ def predecessors_iter(self, n):
yield Node(self, t)
else:
yield Node(self, s)
eh = gv.agnxtin(self.handle, eh)
raise StopIteration
try:
eh = gv.agnxtin(self.handle, eh)
except StopIteration:
return


iterpred = predecessors_iter
Expand All @@ -802,8 +830,11 @@ def successors_iter(self, n):
yield Node(self, t)
else:
yield Node(self, s)
eh = gv.agnxtout(self.handle, eh)
raise StopIteration
try:
eh = gv.agnxtout(self.handle, eh)
except StopIteration:
return


itersucc = successors_iter

Expand Down Expand Up @@ -1089,8 +1120,10 @@ def subgraphs_iter(self):
yield self.__class__(strict=self.strict,
directed=self.directed,
handle=handle)
handle = gv.agnxtsubg(handle)
raise StopIteration
try:
handle = gv.agnxtsubg(handle)
except StopIteration:
return

def subgraphs(self):
"""Return a list of all subgraphs in the graph."""
Expand Down Expand Up @@ -1194,7 +1227,7 @@ def read(self, path):
try:
self.handle = gv.agread(fh, None)
except ValueError:
raise DotError
raise DotError("Invalid Input")

except IOError:
print("IO error reading file")
Expand Down Expand Up @@ -1745,6 +1778,8 @@ def iteritems(self):
gv.agattrdefval(ah).decode(self.encoding))
except KeyError: # gv.agattrdefval returned KeyError, skip
continue
except StopIteration: # gv.agnxtattr is done, as are we
return


class ItemAttribute(Attribute):
Expand Down Expand Up @@ -1816,6 +1851,8 @@ def iteritems(self):
value.decode(self.encoding))
except KeyError: # gv.agxget returned KeyError, skip
continue
except StopIteration: # gv.agnxtattr is done, as are we
return

def _test_suite():
import doctest
Expand Down
5 changes: 5 additions & 0 deletions pygraphviz/tests/test_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,9 +291,14 @@ def test_edges(self):
[('1', '2'), ('2', '3')])
assert_equal(sorted([tuple(sorted(e)) for e in A.in_edges_iter()]),
[('1', '2'), ('2', '3')])
assert_equal(sorted(A.edges(1)), [('1', '2')])
assert_equal(sorted(A.edges([1, 2])), [('1', '2'), ('2', '3')])
assert_equal(sorted(A.edges_iter(1)), [('1', '2')])
assert_equal(sorted(A.out_edges(1)), [('1', '2')])
assert_equal(sorted(A.out_edges([1, 2])), [('1', '2'), ('2', '3')])
assert_equal(sorted(A.out_edges_iter(1)), [('1', '2')])
assert_equal(sorted(A.in_edges(2)), [('1', '2')])
assert_equal(sorted(A.in_edges([1, 2])), [('1', '2')])
assert_equal(sorted(A.in_edges_iter(2)), [('1', '2')])
assert_equal(A.predecessors(1), [])
assert_equal(list(A.predecessors_iter(1)),[])
Expand Down

0 comments on commit 9a6cc0f

Please sign in to comment.