[geneagrapher] 208/226: Change CacheGrabber to provide information for verbose grabbing.

Doug Torrance dtorrance-guest at moszumanska.debian.org
Sat Jul 11 17:11:12 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 7fc33e62212125a851c1b2d7d9c4e970a0d74f44
Author: David Alber <alber.david at gmail.com>
Date:   Fri Dec 30 18:06:56 2011 -0800

    Change CacheGrabber to provide information for verbose grabbing.
    
    This change introduces a way for a grabber class to return a string
    to print when verbose output is requested. Currently, all of the
    information for verbose runs comes from the Geneagrapher class, but
    this change modifies CacheGrabber and Geneagrapher to allow for
    grabber-specific information to be printed.
---
 src/geneagrapher/cache_grabber.py                  |   5 +++--
 src/geneagrapher/geneagrapher.py                   |  15 ++++++++++++---
 tests/geneagrapher/test_cache_grabber.py           |  20 ++++++++++++--------
 tests/geneagrapher/test_geneagrapher_methods.py    |  21 ++++++++++++++++++++-
 .../geneagrapher_verbose_cache_grabber_test        | Bin 0 -> 12288 bytes
 5 files changed, 47 insertions(+), 14 deletions(-)

diff --git a/src/geneagrapher/cache_grabber.py b/src/geneagrapher/cache_grabber.py
index 78be0e2..612f73d 100644
--- a/src/geneagrapher/cache_grabber.py
+++ b/src/geneagrapher/cache_grabber.py
@@ -33,13 +33,14 @@ class CacheGrabber():
         if self.is_cached(id_str):
             d = self.cache[id_str]
             return [d['name'], d['institution'], d['year'], d['advisors'],
-                    d['descendants']]
+                    d['descendants'], 'cache hit']
         else:
             [name, institution, year, advisors,
              descendants] = self.grabber.get_record(id)
             self.load_into_cache(id, name, institution, year, advisors,
                                  descendants)
-            return [name, institution, year, advisors, descendants]
+            return [name, institution, year, advisors, descendants,
+                    'cache miss']
 
     def is_cached(self, id):
         """Return True if an item with the given id is in the cache and has
diff --git a/src/geneagrapher/geneagrapher.py b/src/geneagrapher/geneagrapher.py
index 04d2a26..999bb34 100644
--- a/src/geneagrapher/geneagrapher.py
+++ b/src/geneagrapher/geneagrapher.py
@@ -1,6 +1,7 @@
 from argparse import ArgumentParser
 from collections import deque
 import pkg_resources
+import sys
 from graph import Graph
 from grabber import Grabber
 
@@ -67,9 +68,17 @@ include in graph")
             if not self.graph.has_node(id):
                 # Then this information has not yet been grabbed.
                 if self.verbose:
-                    print "Grabbing record #{}".format(id)
-                [name, institution, year, advisors,
-                 descendants] = grabber.get_record(id)
+                    sys.stdout.write('Grabbing record #{}...'.format(id))
+                record = grabber.get_record(id)
+                if len(record) == 5:
+                    [name, institution, year, advisors,
+                     descendants] = record
+                    grab_msg = ''
+                elif len(record) == 6:
+                    [name, institution, year, advisors,
+                     descendants, grab_msg] = record
+                if self.verbose:
+                    print grab_msg
                 self.graph.add_node(name, institution, year, id, advisors,
                                     descendants, is_seed)
                 if self.get_ancestors and 'ancestor_queue' in kwargs:
diff --git a/tests/geneagrapher/test_cache_grabber.py b/tests/geneagrapher/test_cache_grabber.py
index 25d622b..f4ce55a 100644
--- a/tests/geneagrapher/test_cache_grabber.py
+++ b/tests/geneagrapher/test_cache_grabber.py
@@ -104,14 +104,15 @@ class TestCacheGrabberMethods(unittest.TestCase):
         """Test the get_record method for a good id."""
         with CacheGrabber(record_grabber=LocalDataGrabber) as cache:
             [name, institution, year, advisors,
-             descendents] = cache.get_record(18231)
+             descendants, msg] = cache.get_record(18231)
             self.assertEqual(name, u"Carl Friedrich Gau\xdf")
             self.assertEqual(institution, u"Universit\xe4t Helmstedt")
             self.assertEqual(year, 1799)
             self.assertEqual(advisors, set([18230]))
-            self.assertEqual(descendents, set([18603, 18233, 62547, 29642,
+            self.assertEqual(descendants, set([18603, 18233, 62547, 29642,
                                                55175, 29458, 19953, 18232,
                                                151876]))
+            self.assertEqual(msg, u"cache miss")
             self.assertEqual(len(cache.cache), 1)
 
             # Make the request again and verify the cached version is returned.
@@ -119,14 +120,15 @@ class TestCacheGrabberMethods(unittest.TestCase):
             d['institution'] = u'Rigged for test'
             cache.cache['18231'] = d
             [name, institution, year, advisors,
-             descendents] = cache.get_record(18231)
+             descendants, msg] = cache.get_record(18231)
             self.assertEqual(name, u"Carl Friedrich Gau\xdf")
             self.assertEqual(institution, u"Rigged for test")
             self.assertEqual(year, 1799)
             self.assertEqual(advisors, set([18230]))
-            self.assertEqual(descendents, set([18603, 18233, 62547, 29642,
+            self.assertEqual(descendants, set([18603, 18233, 62547, 29642,
                                                55175, 29458, 19953, 18232,
                                                151876]))
+            self.assertEqual(msg, u"cache hit")
             self.assertEqual(len(cache.cache), 1)
 
         with CacheGrabber(record_grabber=LocalDataGrabber) as cache:
@@ -137,14 +139,15 @@ class TestCacheGrabberMethods(unittest.TestCase):
             d['institution'] = u'Rigged for test'
             cache.cache['18231'] = d
             [name, institution, year, advisors,
-             descendents] = cache.get_record(18231)
+             descendants, msg] = cache.get_record(18231)
             self.assertEqual(name, u"Carl Friedrich Gau\xdf")
             self.assertEqual(institution, u"Rigged for test")
             self.assertEqual(year, 1799)
             self.assertEqual(advisors, set([18230]))
-            self.assertEqual(descendents, set([18603, 18233, 62547, 29642,
+            self.assertEqual(descendants, set([18603, 18233, 62547, 29642,
                                                55175, 29458, 19953, 18232,
                                                151876]))
+            self.assertEqual(msg, u"cache hit")
             self.assertEqual(len(cache.cache), 1)
 
             # Make another request, this time with the cached entry expired,
@@ -152,14 +155,15 @@ class TestCacheGrabberMethods(unittest.TestCase):
             d['timestamp'] = time() - cache.expiration_interval - 1
             cache.cache['18231'] = d
             [name, institution, year, advisors,
-             descendents] = cache.get_record(18231)
+             descendants, msg] = cache.get_record(18231)
             self.assertEqual(name, u"Carl Friedrich Gau\xdf")
             self.assertEqual(institution, u"Universit\xe4t Helmstedt")
             self.assertEqual(year, 1799)
             self.assertEqual(advisors, set([18230]))
-            self.assertEqual(descendents, set([18603, 18233, 62547, 29642,
+            self.assertEqual(descendants, set([18603, 18233, 62547, 29642,
                                                55175, 29458, 19953, 18232,
                                                151876]))
+            self.assertEqual(msg, u"cache miss")
             self.assertEqual(len(cache.cache), 1)
 
     def test_is_in_cache(self):
diff --git a/tests/geneagrapher/test_geneagrapher_methods.py b/tests/geneagrapher/test_geneagrapher_methods.py
index b13dd7c..2267c2e 100644
--- a/tests/geneagrapher/test_geneagrapher_methods.py
+++ b/tests/geneagrapher/test_geneagrapher_methods.py
@@ -3,6 +3,7 @@ import sys
 import unittest
 import StringIO
 from geneagrapher import geneagrapher
+from geneagrapher.cache_grabber import CacheGrabber
 from geneagrapher.graph import Graph
 from local_data_grabber import LocalDataGrabber
 
@@ -95,6 +96,24 @@ geneagrapher: error: too few arguments
         self.assertEqual(record.year, 1672)
         self.assertEqual(record.id, 127946)
 
+    def test_build_graph_only_self_verbose_cache_grabber(self):
+        """Graph building with no ancestors or descendants using the cache
+        grabber to verify its verbose printing."""
+        self.ggrapher.verbose = True
+        self.ggrapher.seed_ids.append(127946)
+
+        # Redirect stdout to capture output.
+        stdout = sys.stdout
+        stdout_intercept = StringIO.StringIO()
+        sys.stdout = stdout_intercept
+        cache_fname = LocalDataGrabber.data_file(
+            'geneagrapher_verbose_cache_grabber_test')
+        self.ggrapher.build_graph(CacheGrabber, filename=cache_fname)
+        sys.stdout = stdout
+
+        self.assertEqual(stdout_intercept.getvalue().decode('utf-8'),
+                         u"Grabbing record #127946...cache hit\n")
+
     def test_build_graph_only_self_verbose(self):
         """Graph building with no ancestors or descendants."""
         self.ggrapher.verbose = True
@@ -122,7 +141,7 @@ geneagrapher: error: too few arguments
         self.assertEqual(record.id, 127946)
 
         self.assertEqual(stdout_intercept.getvalue().decode('utf-8'),
-                         u"Grabbing record #127946\n")
+                         u"Grabbing record #127946...\n")
 
     def test_build_graph_with_ancestors(self):
         """Graph building with ancestors."""
diff --git a/tests/geneagrapher/testdata/geneagrapher_verbose_cache_grabber_test b/tests/geneagrapher/testdata/geneagrapher_verbose_cache_grabber_test
new file mode 100644
index 0000000..12d7f28
Binary files /dev/null and b/tests/geneagrapher/testdata/geneagrapher_verbose_cache_grabber_test differ

-- 
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