[geneagrapher] 172/226: Reformatted code to conform to PEP8.
Doug Torrance
dtorrance-guest at moszumanska.debian.org
Sat Jul 11 17:11:03 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 4e46acaeb406be7f913c91b137b40bca2d9a01ce
Author: David Alber <alber.david at gmail.com>
Date: Thu Nov 24 00:56:55 2011 -0800
Reformatted code to conform to PEP8.
---
geneagrapher/geneagrapher.py | 71 +++++++++++++++++++++++++-------------------
geneagrapher/grabber.py | 20 ++++++++-----
geneagrapher/graph/graph.py | 31 +++++++++++--------
geneagrapher/graph/node.py | 17 +++++++----
geneagrapher/graph/record.py | 26 +++++++++-------
5 files changed, 99 insertions(+), 66 deletions(-)
diff --git a/geneagrapher/geneagrapher.py b/geneagrapher/geneagrapher.py
index de7d863..36d3882 100644
--- a/geneagrapher/geneagrapher.py
+++ b/geneagrapher/geneagrapher.py
@@ -3,6 +3,7 @@ import pkg_resources
from graph import Graph
from grabber import Grabber
+
class Geneagrapher:
"""
A class for building Graphviz "dot" files for math genealogies
@@ -25,22 +26,27 @@ class Geneagrapher:
pkg_env[self.__module__.split('.')[0]][0].version))
self.parser.set_usage("%prog [options] ID [ID...]")
- self.parser.set_description('Create a Graphviz "dot" file for a mathematics genealogy, where ID is a record identifier from the Mathematics Genealogy Project. Multiple IDs may be passed.')
+ self.parser.set_description('Create a Graphviz "dot" file for a \
+mathematics genealogy, where ID is a record identifier from the Mathematics \
+Genealogy Project. Multiple IDs may be passed.')
self.parser.add_option("-f", "--file", dest="filename",
- help="write output to FILE [default: stdout]",
- metavar="FILE", default=None)
+ help="write output to FILE [default: stdout]",
+ metavar="FILE", default=None)
self.parser.add_option("-a", "--with-ancestors", action="store_true",
- dest="get_ancestors", default=False,
- help="retrieve ancestors of IDs and include in graph")
+ dest="get_ancestors", default=False,
+ help="retrieve ancestors of IDs and include in \
+graph")
self.parser.add_option("-d", "--with-descendants", action="store_true",
- dest="get_descendants", default=False,
- help="retrieve descendants of IDs and include in graph")
- self.parser.add_option("--verbose", "-v", action="store_true", dest="verbose",
- default=False, help="list nodes being retrieved")
+ dest="get_descendants", default=False,
+ help="retrieve descendants of IDs and include \
+in graph")
+ self.parser.add_option("--verbose", "-v", action="store_true",
+ dest="verbose", default=False,
+ help="list nodes being retrieved")
(options, args) = self.parser.parse_args()
-
+
if len(args) == 0:
self.parser.error("no record IDs given")
@@ -52,7 +58,8 @@ class Geneagrapher:
def build_graph_portion(self, grab_queue, is_seed, **kwargs):
"""Handle grabbing and storing nodes in the graph. Depending on the
- arguments, this method handles seed nodes, ancestors, or descendants."""
+ arguments, this method handles seed nodes, ancestors, or
+ descendants."""
while len(grab_queue) != 0:
id = grab_queue.pop(0)
if not self.graph.has_node(id):
@@ -60,39 +67,42 @@ class Geneagrapher:
grabber = Grabber(id)
if self.verbose:
print "Grabbing record #{}".format(id)
- [name, institution, year, advisors, descendants] = grabber.extract_node_information()
- self.graph.add_node(name, institution, year, id, advisors, descendants, is_seed)
- if self.get_ancestors and kwargs.has_key('ancestor_grab_queue'):
- kwargs['ancestor_grab_queue'] += advisors
- if self.get_descendants and kwargs.has_key('descendant_grab_queue'):
- kwargs['descendant_grab_queue'] += descendants
-
+ [name, institution, year, advisors,
+ descendants] = grabber.extract_node_information()
+ self.graph.add_node(name, institution, year, id, advisors,
+ descendants, is_seed)
+ if self.get_ancestors and 'ancestor_queue' in kwargs:
+ kwargs['ancestor_queue'] += advisors
+ if self.get_descendants and 'descendant_queue' in kwargs:
+ kwargs['descendant_queue'] += descendants
+
def build_graph(self):
"""
Populate the graph member by grabbing the mathematician
pages and extracting relevant data.
"""
- leaf_grab_queue = list(self.leaf_ids)
- ancestor_grab_queue = []
- descendant_grab_queue = []
+ leaf_queue = list(self.leaf_ids)
+ ancestor_queue = []
+ descendant_queue = []
# Grab "leaf" nodes.
- self.build_graph_portion(leaf_grab_queue, True,
- ancestor_grab_queue=ancestor_grab_queue,
- descendant_grab_queue=descendant_grab_queue)
+ self.build_graph_portion(leaf_queue, True,
+ ancestor_queue=ancestor_queue,
+ descendant_queue=descendant_queue)
# Grab ancestors of leaf nodes.
if self.get_ancestors:
- self.build_graph_portion(ancestor_grab_queue, False,
- ancestor_grab_queue=ancestor_grab_queue)
+ self.build_graph_portion(ancestor_queue, False,
+ ancestor_queue=ancestor_queue)
# Grab descendants of leaf nodes.
if self.get_descendants:
- self.build_graph_portion(descendant_grab_queue, False,
- descendant_grab_queue=descendant_grab_queue)
+ self.build_graph_portion(descendant_queue, False,
+ descendant_queue=descendant_queue)
def generate_dot_file(self):
- dotfile = self.graph.generate_dot_file(self.get_ancestors, self.get_descendants)
+ dotfile = self.graph.generate_dot_file(self.get_ancestors,
+ self.get_descendants)
dotfile = dotfile.encode('utf-8', 'replace')
if self.write_filename is not None:
outfile = open(self.write_filename, "w")
@@ -100,7 +110,8 @@ class Geneagrapher:
outfile.close()
else:
print dotfile,
-
+
+
def ggrapher():
"""Function to run the Geneagrapher. This is the function called when
the ggrapher script is run."""
diff --git a/geneagrapher/grabber.py b/geneagrapher/grabber.py
index d1ffb9b..edfc2dc 100644
--- a/geneagrapher/grabber.py
+++ b/geneagrapher/grabber.py
@@ -2,6 +2,7 @@ import urllib
import re
from BeautifulSoup import BeautifulSoup
+
class Grabber:
"""
Class for grabbing and parsing mathematician information from
@@ -30,12 +31,14 @@ class Grabber:
page = urllib.urlopen(url)
soup = BeautifulSoup(page, convertEntities='html')
page.close()
-
+
self.advisors = []
self.descendants = []
- if soup.firstText().text == u"You have specified an ID that does not exist in the database. Please back up and try again.":
- # Then a bad URL (e.g., a bad record id) was given. Throw an exception.
+ if soup.firstText().text == u"You have specified an ID that does not \
+exist in the database. Please back up and try again.":
+ # Then a bad URL (e.g., a bad record id) was given. Throw an
+ # exception.
msg = "Invalid id {}".format(self.id)
raise ValueError(msg)
@@ -43,12 +46,14 @@ class Grabber:
self.name = soup.find('h2').getText()
# Get institution name (or None, if it there is no institution name).
- self.institution = soup.find('div', style="line-height: 30px; text-align: center; margin-bottom: 1ex").find('span').find('span').text
+ self.institution = soup.find('div', style="line-height: 30px; \
+text-align: center; margin-bottom: 1ex").find('span').find('span').text
if self.institution == u'':
self.institution = None
# Get graduation year, if present.
- inst_year = soup.find('div', style="line-height: 30px; text-align: center; margin-bottom: 1ex").find('span').contents[-1].strip()
+ inst_year = soup.find('div', style="line-height: 30px; text-align: \
+center; margin-bottom: 1ex").find('span').contents[-1].strip()
if inst_year.isdigit():
self.year = int(inst_year)
@@ -61,5 +66,6 @@ class Grabber:
if soup.find('table') is not None:
self.descendants = [self.extract_id(info) for info in
soup.find('table').findAll('a')]
-
- return [self.name, self.institution, self.year, self.advisors, self.descendants]
+
+ return [self.name, self.institution, self.year, self.advisors,
+ self.descendants]
diff --git a/geneagrapher/graph/graph.py b/geneagrapher/graph/graph.py
index ed58c3b..919380e 100644
--- a/geneagrapher/graph/graph.py
+++ b/geneagrapher/graph/graph.py
@@ -1,6 +1,7 @@
from node import Node
from record import Record
+
class DuplicateNodeError(Exception):
def __init__(self, value):
self.value = value
@@ -8,6 +9,7 @@ class DuplicateNodeError(Exception):
def __str__(self):
return self.value
+
class Graph:
"""
Class storing the representation of a genealogy graph.
@@ -15,21 +17,23 @@ class 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'")
+ 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'")
+ raise TypeError("Unexpected parameter type: expected list \
+of Node objects for 'heads'")
if self.heads is not None:
self.nodes = dict([(head.get_id(), head) for head in self.heads])
@@ -40,7 +44,7 @@ class Graph:
"""
Check if the graph contains a node with the given id.
"""
- return self.nodes.has_key(id)
+ return id in self.nodes
def get_node(self, id):
"""
@@ -49,13 +53,14 @@ class Graph:
"""
return self.nodes[id]
- def get_node_list(self): # NOTE: this method is unused
+ 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):
+ 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.
@@ -95,7 +100,7 @@ class Graph:
queue.append(head.get_id())
edges = ""
dotfile = ""
-
+
dotfile += u"""digraph genealogy {
graph [charset="utf-8"];
node [shape=plaintext];
@@ -107,20 +112,20 @@ class Graph:
if not self.has_node(node_id):
# Skip this id if a corresponding node is not present.
continue
- if printed_nodes.has_key(node_id):
+ if node_id in printed_nodes:
# Skip this id because it is already printed.
continue
node = self.get_node(node_id)
printed_nodes[node_id] = node
-
+
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 = u" {} [label=\"{}\"];".format(node_id, node)
dotfile += nodestr
@@ -130,7 +135,7 @@ class Graph:
if self.has_node(advisor):
edgestr = "\n {} -> {};".format(advisor, node_id)
edges += edgestr
-
+
dotfile += "\n"
# Now print the connections between the nodes.
diff --git a/geneagrapher/graph/node.py b/geneagrapher/graph/node.py
index b095c5d..6f9540e 100644
--- a/geneagrapher/graph/node.py
+++ b/geneagrapher/graph/node.py
@@ -1,5 +1,6 @@
from record import Record
+
class Node:
"""
Container class storing a node in the graph.
@@ -7,7 +8,7 @@ class Node:
def __init__(self, record, ancestors, descendants):
"""
Node class constructor.
-
+
Parameters:
record: instance of the Record class
ancestors: list of the record's genealogical ancestors'
@@ -15,18 +16,21 @@ class Node:
descendants: list of this record's genealogical
descendants' IDs
"""
-
+
self.record = record
self.ancestors = ancestors
self.descendants = descendants
# Verify parameter types.
if not isinstance(self.record, Record):
- raise TypeError("Unexpected parameter type: expected Record object for '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'")
+ 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'")
+ raise TypeError("Unexpected parameter type: expected list object \
+for 'descendants'")
def __unicode__(self):
return self.record.__unicode__()
@@ -40,7 +44,8 @@ class Node:
"""
# Verify we were passed an int.
if not isinstance(ancestor, int):
- raise TypeError("Unexpected parameter type: expected int for 'ancestor'")
+ raise TypeError("Unexpected parameter type: expected int for \
+'ancestor'")
self.ancestors.append(ancestor)
def get_id(self):
diff --git a/geneagrapher/graph/record.py b/geneagrapher/graph/record.py
index 929ea5b..048d7b9 100644
--- a/geneagrapher/graph/record.py
+++ b/geneagrapher/graph/record.py
@@ -5,7 +5,7 @@ class Record:
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
@@ -17,16 +17,21 @@ class Record:
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'")
+ 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'")
+ 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'")
+ raise TypeError("Unexpected parameter type: expected integer \
+value for 'id'")
def __cmp__(self, r2):
"""
@@ -37,7 +42,8 @@ class Record:
def __unicode__(self):
if self.has_institution():
if self.has_year():
- return u'{} \\n{} ({})'.format(self.name, self.institution, self.year)
+ return u'{} \\n{} ({})'.format(self.name, self.institution,
+ self.year)
else:
return u'{} \\n{}'.format(self.name, self.institution)
else:
@@ -45,14 +51,14 @@ class Record:
return u'{} \\n({})'.format(self.name, self.year)
else:
return self.name
-
+
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
--
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