[geneagrapher] 150/226: Completed transition from GGraph.py to separate modules.
Doug Torrance
dtorrance-guest at moszumanska.debian.org
Sat Jul 11 17:10:57 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 970b89f73faec5239793b445fd3667b6e9edcd02
Author: David Alber <alber.david at gmail.com>
Date: Sun Oct 30 10:24:50 2011 -0700
Completed transition from GGraph.py to separate modules.
---
geneagrapher/GGraph.py | 258 -----------------------------------------
geneagrapher/geneagrapher.py | 4 +-
geneagrapher/graph/__init__.py | 3 +
geneagrapher/graph/graph.py | 3 +
geneagrapher/graph/node.py | 2 +
tests/tests.py | 105 ++++++++---------
6 files changed, 63 insertions(+), 312 deletions(-)
diff --git a/geneagrapher/GGraph.py b/geneagrapher/GGraph.py
deleted file mode 100644
index 1fdddca..0000000
--- a/geneagrapher/GGraph.py
+++ /dev/null
@@ -1,258 +0,0 @@
-"""A set of classes for storing the genealogy graph."""
-
-class DuplicateNodeError(Exception):
- def __init__(self, value):
- self.value = value
- def __str__(self):
- return repr(self.value)
-
-class Record:
- """
- Container class storing record of a mathematician in the graph.
- """
- def __init__(self, name, institution=None, year=None, id=None):
- """
- Record class constructor.
-
- Parameters:
- name: string containing mathematician's name
- institution: string containing mathematician's institution
- (empty if none)
- year: integer containing year degree was earned
- id: integer containing Math Genealogy Project id value
- """
- self.name = name
- self.institution = institution
- self.year = year
- self.id = id
-
- # Verify we got the types wanted.
- if not isinstance(self.name, basestring):
- raise TypeError("Unexpected parameter type: expected string value for 'name'")
- if not isinstance(self.institution, basestring) and self.institution is not None:
- raise TypeError("Unexpected parameter type: expected string value for 'institution'")
- if not isinstance(self.year, int) and self.year is not None:
- raise TypeError("Unexpected parameter type: expected integer value for 'year'")
- if not isinstance(self.id, int) and self.id is not None:
- raise TypeError("Unexpected parameter type: expected integer value for 'id'")
-
- def __cmp__(self, r2):
- """
- Compare a pair of mathematician records based on ids.
- """
- return self.id.__cmp__(r2.id)
-
- def has_institution(self):
- """
- Return True if this record has an institution associated with it,
- else False.
- """
- return self.institution is not None
-
- def has_year(self):
- """
- Return True if this record has a year associated with it, else
- False.
- """
- return self.year is not None
-
-
-class Node:
- """
- Container class storing a node in the graph.
- """
- def __init__(self, record, ancestors, descendants):
- """
- Node class constructor.
-
- Parameters:
- record: instance of the Record class
- ancestors: list of the record's genealogical ancestors'
- IDs
- descendants: list of this record's genealogical
- descendants' IDs
- """
-
- self.record = record
- self.ancestors = ancestors
- self.descendants = descendants
- self.already_printed = False
-
- # Verify parameter types.
- if not isinstance(self.record, Record):
- raise TypeError("Unexpected parameter type: expected Record object for 'record'")
- if not isinstance(self.ancestors, list):
- raise TypeError("Unexpected parameter type: expected list object for 'ancestors'")
- if not isinstance(self.descendants, list):
- raise TypeError("Unexpected parameter type: expected list object for 'descendants'")
-
- def __str__(self):
- if self.record.has_institution():
- if self.record.has_year():
- return self.record.name.encode('utf-8', 'replace') + ' \\n' + self.record.institution.encode('utf-8', 'replace') + ' (' + str(self.record.year) + ')'
- else:
- return self.record.name.encode('utf-8', 'replace') + ' \\n' + self.record.institution.encode('utf-8', 'replace')
- else:
- if self.record.has_year():
- return self.record.name.encode('utf-8', 'replace') + ' \\n(' + str(self.record.year) + ')'
- else:
- return self.record.name.encode('utf-8', 'replace')
-
- def __cmp__(self, n2):
- return self.record.__cmp__(n2.record)
-
- def add_ancestor(self, ancestor): # NOTE: is this used?
- """
- Append an ancestor id to the ancestor list.
- """
- # Verify we were passed an int.
- if not isinstance(ancestor, int):
- raise TypeError("Unexpected parameter type: expected int for 'ancestor'")
- self.ancestors.append(ancestor)
-
- def get_id(self):
- """
- Accessor method to retrieve the id of this node's record.
- """
- return self.record.id
-
- def set_id(self, id):
- """
- Sets the record id.
- """
- self.record.id = id
-
-
-class Graph:
- """
- Class storing the representation of a genealogy graph.
- """
- def __init__(self, heads=None):
- """
- Graph class constructor.
-
- Parameters:
- heads: a list of Node objects representing the tree head
- (can be omitted to create an empty graph)
- """
- self.heads = heads
- self.supp_id = -1
-
- # Verify type of heads is what we expect.
- if self.heads is not None:
- if not isinstance(self.heads, list):
- raise TypeError("Unexpected parameter type: expected list of Node objects for 'heads'")
- for head in self.heads:
- 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
-
- def has_node(self, id):
- """
- Check if the graph contains a node with the given id.
- """
- return self.nodes.has_key(id)
-
- def get_node(self, id):
- """
- 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
-
- def get_node_list(self): # NOTE: this method is unused
- """
- Return a list of the nodes in the graph.
- """
- return self.nodes.keys()
-
- def add_node(self, name, institution, year, id, ancestors, descendants, isHead=False):
- """
- Add a new node to the graph if a matching node is not already
- present.
- """
- record = Record(name, institution, year, id)
- node = Node(record, ancestors, descendants)
- self.add_node_object(node, isHead)
-
- def add_node_object(self, node, isHead=False):
- """
- Add a new node object to the graph if a node with the same id
- is not already present.
- """
- if node.get_id() is not None and self.has_node(node.get_id()):
- msg = "node with id {} already exists".format(node.get_id())
- raise DuplicateNodeError(msg)
- if node.get_id() is None:
- # Assign a "dummy" id.
- node.set_id(self.supp_id)
- self.supp_id -= 1
- self.nodes[node.get_id()] = node
- if self.heads is None:
- self.heads = [node]
- elif isHead:
- self.heads.append(node)
-
- def generate_dot_file(self, include_ancestors, include_descendants):
- """
- Return a string that contains the content of the Graphviz dotfile
- format for this graph.
- """
- if self.heads is None:
- return ""
-
- queue = []
- for head in self.heads:
- queue.append(head.get_id())
- edges = ""
- dotfile = ""
-
- dotfile += """digraph genealogy {
- graph [charset="utf-8"];
- node [shape=plaintext];
- edge [style=bold];\n\n"""
-
- while len(queue) > 0:
- node_id = queue.pop()
- if not self.has_node(node_id):
- # Skip this id if a corresponding node is not present.
- continue
- node = self.get_node(node_id)
-
- if node.already_printed:
- continue
- else:
- node.already_printed = True
-
- if include_ancestors:
- # Add this node's advisors to queue.
- queue += node.ancestors
-
- if include_descendants:
- # Add this node's descendants to queue.
- queue += node.descendants
-
- # Print this node's information.
- nodestr = " {} [label=\"{}\"];".format(node_id, node)
- dotfile += nodestr
-
- # Store the connection information for this node.
- for advisor in node.ancestors:
- if self.has_node(advisor):
- edgestr = "\n {} -> {};".format(advisor, node_id)
- edges += edgestr
-
- dotfile += "\n"
-
- # Now print the connections between the nodes.
- dotfile += edges
-
- dotfile += "\n}\n"
- return dotfile
diff --git a/geneagrapher/geneagrapher.py b/geneagrapher/geneagrapher.py
index f94caa8..4feb077 100644
--- a/geneagrapher/geneagrapher.py
+++ b/geneagrapher/geneagrapher.py
@@ -1,6 +1,6 @@
from optparse import OptionParser
import pkg_resources
-import GGraph
+from graph import Graph
from grabber import Grabber
class Geneagrapher:
@@ -9,7 +9,7 @@ class Geneagrapher:
extracted from the Mathematics Genealogy Project website.
"""
def __init__(self):
- self.graph = GGraph.Graph()
+ self.graph = Graph()
self.leaf_ids = []
self.get_ancestors = False
self.get_descendants = False
diff --git a/geneagrapher/graph/__init__.py b/geneagrapher/graph/__init__.py
new file mode 100644
index 0000000..0bdfa1b
--- /dev/null
+++ b/geneagrapher/graph/__init__.py
@@ -0,0 +1,3 @@
+from graph import DuplicateNodeError, Graph
+from node import Node
+from record import Record
diff --git a/geneagrapher/graph/graph.py b/geneagrapher/graph/graph.py
index 6f31496..8d53550 100644
--- a/geneagrapher/graph/graph.py
+++ b/geneagrapher/graph/graph.py
@@ -1,3 +1,6 @@
+from node import Node
+from record import Record
+
class DuplicateNodeError(Exception):
def __init__(self, value):
self.value = value
diff --git a/geneagrapher/graph/node.py b/geneagrapher/graph/node.py
index 8305982..3c11d67 100644
--- a/geneagrapher/graph/node.py
+++ b/geneagrapher/graph/node.py
@@ -1,3 +1,5 @@
+from record import Record
+
class Node:
"""
Container class storing a node in the graph.
diff --git a/tests/tests.py b/tests/tests.py
index 8450c1c..32d11b5 100644
--- a/tests/tests.py
+++ b/tests/tests.py
@@ -1,16 +1,17 @@
import sys
import unittest
-from geneagrapher import GGraph, geneagrapher
+from geneagrapher import geneagrapher
from geneagrapher.grabber import Grabber
+from geneagrapher.graph import DuplicateNodeError, Graph, Node, Record
-# Unit tests for GGraph.
+# Unit tests for graph-related classes.
class TestRecordMethods(unittest.TestCase):
"""
- Unit tests for the GGraph.Record class.
+ Unit tests for the Record class.
"""
def test001_init(self):
# Test the constructor.
- record = GGraph.Record("Carl Friedrich Gauss", "Universitaet Helmstedt", 1799, 18231)
+ record = Record("Carl Friedrich Gauss", "Universitaet Helmstedt", 1799, 18231)
self.assertEqual(record.name, "Carl Friedrich Gauss")
self.assertEqual(record.institution, "Universitaet Helmstedt")
self.assertEqual(record.year, 1799)
@@ -18,64 +19,64 @@ class TestRecordMethods(unittest.TestCase):
def test002_init_bad_name(self):
# Test constructor with bad 'name' parameter.
- self.assertRaises(TypeError, GGraph.Record, 1, "Universitaet Helmstedt", 1799, 18231)
+ self.assertRaises(TypeError, Record, 1, "Universitaet Helmstedt", 1799, 18231)
def test003_init_bad_institution(self):
# Test constructor with bad 'institution' parameter.
- self.assertRaises(TypeError, GGraph.Record, "Carl Friedrich Gauss", 1, 1799, 18231)
+ self.assertRaises(TypeError, Record, "Carl Friedrich Gauss", 1, 1799, 18231)
def test004_init_bad_year(self):
# Test constructor with bad 'year' parameter.
- self.assertRaises(TypeError, GGraph.Record, "Carl Friedrich Gauss",
+ self.assertRaises(TypeError, Record, "Carl Friedrich Gauss",
"Universitaet Helmstedt", "1799", 18231)
def test005_init_bad_id(self):
# Test constructor with bad 'id' parameter.
- self.assertRaises(TypeError, GGraph.Record, "Carl Friedrich Gauss",
+ self.assertRaises(TypeError, Record, "Carl Friedrich Gauss",
"Universitaet Helmstedt", 1799, "18231")
def test006_cmp_equal(self):
# Verify two 'equal' records are compared correctly.
- record1 = GGraph.Record("Carl Friedrich Gauss", "Universitaet Helmstedt", 1799, 18231)
- record2 = GGraph.Record("Carl Friedrich Gauss", "Universitaet Helmstedt", 1799, 18231)
+ record1 = Record("Carl Friedrich Gauss", "Universitaet Helmstedt", 1799, 18231)
+ record2 = Record("Carl Friedrich Gauss", "Universitaet Helmstedt", 1799, 18231)
self.assert_(record1 == record2)
def test007_cmp_unequal(self):
# Verify two 'unequal' records are compared correctly.
- record1 = GGraph.Record("Carl Friedrich Gauss", "Universitaet Helmstedt", 1799, 18231)
- record2 = GGraph.Record("Leonhard Euler", "Universitaet Basel", 1726, 38586)
+ record1 = Record("Carl Friedrich Gauss", "Universitaet Helmstedt", 1799, 18231)
+ record2 = Record("Leonhard Euler", "Universitaet Basel", 1726, 38586)
self.assert_(record1 < record2)
def test008_has_institution_yes(self):
# Verify has_institution() method returns True when the conditions are right.
- record = GGraph.Record("Carl Friedrich Gauss", "Universitaet Helmstedt", 1799, 18231)
+ record = Record("Carl Friedrich Gauss", "Universitaet Helmstedt", 1799, 18231)
self.assert_(record.has_institution())
def test009_has_institution_no(self):
# Verify has_institution() method returns False when the conditions are right.
- record = GGraph.Record("Carl Friedrich Gauss", None, 1799, 18231)
+ record = Record("Carl Friedrich Gauss", None, 1799, 18231)
self.assert_(not record.has_institution())
def test010_has_year_yes(self):
# Verify has_year() method returns True when the conditions are right.
- record = GGraph.Record("Carl Friedrich Gauss", "Universitaet Helmstedt", 1799, 18231)
+ record = Record("Carl Friedrich Gauss", "Universitaet Helmstedt", 1799, 18231)
self.assert_(record.has_year())
def test011_has_year_no(self):
# Verify has_year() method returns False when the conditions are right.
- record = GGraph.Record("Carl Friedrich Gauss", "Universitaet Helmstedt", None, 18231)
+ record = Record("Carl Friedrich Gauss", "Universitaet Helmstedt", None, 18231)
self.assert_(not record.has_year())
class TestNodeMethods(unittest.TestCase):
"""
- Unit tests for the GGraph.Node class.
+ Unit tests for the Node class.
"""
def setUp(self):
- self.record = GGraph.Record("Carl Friedrich Gauss", "Universitaet Helmstedt", 1799, 18231)
+ self.record = Record("Carl Friedrich Gauss", "Universitaet Helmstedt", 1799, 18231)
def test001_init(self):
# Test the constructor.
- node = GGraph.Node(self.record, [], [])
+ node = Node(self.record, [], [])
self.assertEquals(node.record, self.record)
self.assertEquals(node.ancestors, [])
self.assertEquals(node.descendants, [])
@@ -83,35 +84,35 @@ class TestNodeMethods(unittest.TestCase):
def test002_init_bad_record(self):
# Test the constructor for a case where the record passed is not a Record
# object.
- self.assertRaises(TypeError, GGraph.Node, 1, [], [])
+ self.assertRaises(TypeError, Node, 1, [], [])
def test003_init_bad_ancestor_list(self):
# Test the constructor for a case where the ancestor list is not a list.
- self.assertRaises(TypeError, GGraph.Node, self.record, 1, [])
+ self.assertRaises(TypeError, Node, self.record, 1, [])
def test003_2_init_bad_descendent_list(self):
# Test the constructor for a case where the descendent list is not a list.
- self.assertRaises(TypeError, GGraph.Node, self.record, [], 1)
+ self.assertRaises(TypeError, Node, self.record, [], 1)
def test004_str_full(self):
# Test __str__() method for Node with complete record.
- node = GGraph.Node(self.record, [], [])
+ node = Node(self.record, [], [])
nodestr = node.__str__()
nodestrexpt = "Carl Friedrich Gauss \\nUniversitaet Helmstedt (1799)"
self.assertEquals(nodestr, nodestrexpt)
def test005_str_no_year(self):
# Test __str__() method for Node containing record without year.
- record = GGraph.Record("Carl Friedrich Gauss", "Universitaet Helmstedt", None, 18231)
- node = GGraph.Node(record, [], [])
+ record = Record("Carl Friedrich Gauss", "Universitaet Helmstedt", None, 18231)
+ node = Node(record, [], [])
nodestr = node.__str__()
nodestrexpt = "Carl Friedrich Gauss \\nUniversitaet Helmstedt"
self.assertEquals(nodestr, nodestrexpt)
def test006_str_no_inst(self):
# Test __str__() method for Node containing record without institution.
- record = GGraph.Record("Carl Friedrich Gauss", None, 1799, 18231)
- node = GGraph.Node(record, [], [])
+ record = Record("Carl Friedrich Gauss", None, 1799, 18231)
+ node = Node(record, [], [])
nodestr = node.__str__()
nodestrexpt = "Carl Friedrich Gauss \\n(1799)"
self.assertEquals(nodestr, nodestrexpt)
@@ -119,44 +120,44 @@ class TestNodeMethods(unittest.TestCase):
def test007_str_no_inst_no_id(self):
# Test __str__() method for Node containing record without institution
# or year.
- record = GGraph.Record("Carl Friedrich Gauss", None, None, 18231)
- node = GGraph.Node(record, [], [])
+ record = Record("Carl Friedrich Gauss", None, None, 18231)
+ node = Node(record, [], [])
nodestr = node.__str__()
nodestrexpt = "Carl Friedrich Gauss"
self.assertEquals(nodestr, nodestrexpt)
def test008_cmp_equal(self):
# Test comparison method for Nodes with identical records.
- record2 = GGraph.Record("Carl Friedrich Gauss", "Universitaet Helmstedt", 1799, 18231)
- node1 = GGraph.Node(self.record, [], [])
- node2 = GGraph.Node(record2, [], [])
+ record2 = Record("Carl Friedrich Gauss", "Universitaet Helmstedt", 1799, 18231)
+ node1 = Node(self.record, [], [])
+ node2 = Node(record2, [], [])
self.assert_(node1 == node2)
def test009_cmp_unequal(self):
# Test comparison method for Nodes with different records.
- record2 = GGraph.Record("Leonhard Euler", "Universitaet Basel", 1726, 38586)
- node1 = GGraph.Node(self.record, [], [])
- node2 = GGraph.Node(record2, [], [])
+ record2 = Record("Leonhard Euler", "Universitaet Basel", 1726, 38586)
+ node1 = Node(self.record, [], [])
+ node2 = Node(record2, [], [])
self.assert_(node1 < node2)
def test010_add_ancestor(self):
# Test the add_ancestor() method.
- node = GGraph.Node(self.record, [], [])
+ node = Node(self.record, [], [])
node.add_ancestor(5)
self.assertEquals(node.ancestors, [5])
def test011_add_ancestor_bad_type(self):
# Test the add_ancestor() method for a case where the parameter type is incorrect.
- node = GGraph.Node(self.record, [], [])
+ node = Node(self.record, [], [])
self.assertRaises(TypeError, node.add_ancestor, '5')
def test012_get_id(self):
- node = GGraph.Node(self.record, [], [])
+ node = Node(self.record, [], [])
self.assertEquals(node.get_id(), 18231)
def test013_set_id(self):
# Test the set_id() method.
- node = GGraph.Node(self.record, [], [])
+ node = Node(self.record, [], [])
self.assertEquals(node.get_id(), 18231)
node.set_id(15)
self.assertEquals(node.get_id(), 15)
@@ -164,16 +165,16 @@ class TestNodeMethods(unittest.TestCase):
class TestGraphMethods(unittest.TestCase):
"""
- Unit tests for the GGraph.Graph class.
+ Unit tests for the Graph class.
"""
def setUp(self):
- self.record1 = GGraph.Record("Carl Friedrich Gauss", "Universitaet Helmstedt", 1799, 18231)
- self.node1 = GGraph.Node(self.record1, [], [])
- self.graph1 = GGraph.Graph([self.node1])
+ self.record1 = Record("Carl Friedrich Gauss", "Universitaet Helmstedt", 1799, 18231)
+ self.node1 = Node(self.record1, [], [])
+ self.graph1 = Graph([self.node1])
def test001_init_empty(self):
# Test the constructor.
- graph = GGraph.Graph()
+ graph = Graph()
self.assertEquals(graph.heads, None)
def test002_init(self):
@@ -184,7 +185,7 @@ class TestGraphMethods(unittest.TestCase):
def test003_init_bad_heads(self):
# Test the constructor when passed a bad type for the heads parameter.
- self.assertRaises(TypeError, GGraph.Graph, 3)
+ self.assertRaises(TypeError, Graph, 3)
def test004_has_node_true(self):
# Test the has_node() method for a True case.
@@ -210,7 +211,7 @@ class TestGraphMethods(unittest.TestCase):
def test008_get_node_list_empty(self):
# Test the get_node_list() method for an empty graph.
- graph = GGraph.Graph()
+ graph = Graph()
self.assertEquals(graph.get_node_list(), [])
def test009_add_node(self):
@@ -228,7 +229,7 @@ class TestGraphMethods(unittest.TestCase):
def test011_add_node_head(self):
# Test the add_node() method when no heads exist.
- graph = GGraph.Graph()
+ graph = Graph()
self.assertEquals(graph.heads, None)
graph.add_node("Leonhard Euler", "Universitaet Basel", 1726, 38586, [], [])
self.assertEquals(graph.heads, [graph.get_node(38586)])
@@ -236,12 +237,12 @@ class TestGraphMethods(unittest.TestCase):
def test012_add_node_already_present(self):
self.graph1.add_node("Leonhard Euler", "Universitaet Basel", 1726, 38586, [], [])
self.assertEquals([38586, 18231], self.graph1.get_node_list())
- self.assertRaises(GGraph.DuplicateNodeError, self.graph1.add_node, "Leonhard Euler", "Universitaet Basel", 1726, 38586, [], [])
+ self.assertRaises(DuplicateNodeError, self.graph1.add_node, "Leonhard Euler", "Universitaet Basel", 1726, 38586, [], [])
def test013_add_node_object(self):
# Test the add_node_object() method.
- record = GGraph.Record("Leonhard Euler", "Universitaet Basel", 1726, 38586)
- node = GGraph.Node(record, [], [])
+ record = Record("Leonhard Euler", "Universitaet Basel", 1726, 38586)
+ node = Node(record, [], [])
self.graph1.add_node_object(node)
self.assertEquals([38586, 18231], self.graph1.get_node_list())
self.assertEquals(self.graph1.heads, [self.node1])
@@ -262,7 +263,7 @@ class TestGraphMethods(unittest.TestCase):
def test015_generate_dot_file(self):
# Test the generate_dot_file() method.
- graph = GGraph.Graph()
+ graph = Graph()
graph.add_node("Carl Friedrich Gauss", "Universitaet Helmstedt", 1799, 18231, [18230], [])
graph.add_node("Johann Friedrich Pfaff", "Georg-August-Universitaet Goettingen", 1786, 18230, [66476], [])
graph.add_node("Abraham Gotthelf Kaestner", "Universitaet Leipzig", 1739, 66476, [57670], [])
@@ -406,7 +407,7 @@ class TestGeneagrapherMethods(unittest.TestCase):
def test001_init(self):
# Test constructor.
- self.assertEquals(isinstance(self.ggrapher.graph, GGraph.Graph), True)
+ self.assertEquals(isinstance(self.ggrapher.graph, Graph), True)
self.assertEquals(self.ggrapher.leaf_ids, [])
self.assertEquals(self.ggrapher.get_ancestors, False)
self.assertEquals(self.ggrapher.get_descendants, False)
--
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