[geneagrapher] 163/226: - Replaced loop with list comprehension. - Modified Graph.get_node to raise exception when the node requested does not exist.
Doug Torrance
dtorrance-guest at moszumanska.debian.org
Sat Jul 11 17:11:00 UTC 2015
This is an automated email from the git hooks/post-receive script.
dtorrance-guest pushed a commit to branch master
in repository geneagrapher.
commit 76e00bbedabf88ed0a5f849dbef336f569d05c55
Author: David Alber <alber.david at gmail.com>
Date: Wed Nov 2 22:02:19 2011 -0700
- Replaced loop with list comprehension.
- Modified Graph.get_node to raise exception when the node requested does
not exist.
---
geneagrapher/graph/graph.py | 11 ++++-------
tests/test_graph_methods.py | 3 +--
2 files changed, 5 insertions(+), 9 deletions(-)
diff --git a/geneagrapher/graph/graph.py b/geneagrapher/graph/graph.py
index 7c08a7b..1e39460 100644
--- a/geneagrapher/graph/graph.py
+++ b/geneagrapher/graph/graph.py
@@ -30,10 +30,10 @@ class Graph:
if not isinstance(head, Node):
raise TypeError("Unexpected parameter type: expected list of Node objects for 'heads'")
- self.nodes = {}
if self.heads is not None:
- for head in self.heads:
- self.nodes[head.get_id()] = head
+ self.nodes = dict([(head.get_id(), head) for head in self.heads])
+ else:
+ self.nodes = {}
def has_node(self, id):
"""
@@ -46,10 +46,7 @@ class Graph:
Return the node in the graph with given id. Returns
None if no such node exists.
"""
- if self.has_node(id):
- return self.nodes[id]
- else:
- return None
+ return self.nodes[id]
def get_node_list(self): # NOTE: this method is unused
"""
diff --git a/tests/test_graph_methods.py b/tests/test_graph_methods.py
index f137881..907cf91 100644
--- a/tests/test_graph_methods.py
+++ b/tests/test_graph_methods.py
@@ -40,8 +40,7 @@ class TestGraphMethods(unittest.TestCase):
def test007_get_node_not_found(self):
# Test the get_node() method for a case where the node does not exist.
- node = self.graph1.get_node(1)
- self.assertEquals(node, None)
+ self.assertRaises(KeyError, self.graph1.get_node, 1)
def test008_get_node_list(self):
# Test the get_node_list() method.
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/geneagrapher.git
More information about the debian-science-commits
mailing list