commit bb0dcfc7fdf9303251dbe7304005d349f9956fd6 Author: Erik eislo@wesleyan.edu Date: Wed Aug 1 16:40:56 2012 -0400
Submitting for code review. --- stem/descriptor/export.py | 16 ++++++++-------- test/mocking.py | 5 +++++ test/unit/descriptor/export.py | 9 --------- 3 files changed, 13 insertions(+), 17 deletions(-)
diff --git a/stem/descriptor/export.py b/stem/descriptor/export.py index 11b4918..89a5644 100644 --- a/stem/descriptor/export.py +++ b/stem/descriptor/export.py @@ -6,7 +6,7 @@ def export_csv(descriptor, include_fields=(), exclude_fields=()): Takes a single descriptor object, puts it in a list, and passes it to descriptors_csv_exp to build a csv.
- :param object descriptor: single descriptor whose. + :param object descriptor: single descriptor whose attributes will be returned as a string. :param list include_fields: list of attribute fields to include in the csv string. :param list exclude_fields: list of attribute fields to exclude from csv string.
@@ -25,8 +25,7 @@ def export_csvs(descriptors, include_fields=[], exclude_fields=[], header=False) :param list descrs: List of descriptor objects whose attributes will be written. :param list include_fields: list of attribute fields to include in the csv string. :param list exclude_fields: list of attribute fields to exclude from csv string. - :param bool header: whether or not a header is requested; probably won't be - needed outside of csv_file_exp's call of this function. + :param bool header: whether or not a header is requested.
:returns: csv string with one descriptor per line and one attribute per cell. :raises: ValueError if more than one descriptor type (e.g. server_descriptor, @@ -39,7 +38,7 @@ def export_csvs(descriptors, include_fields=[], exclude_fields=[], header=False) first = True
for desc in descriptors: - import sys + #umport sys attr = vars(desc)
# Defining incl_fields and the dwriter object requires having access @@ -90,8 +89,9 @@ def export_csv_file(descriptors, document, include_fields=(), exclude_fields=(), :param list include_fields: list of attribute fields to include in the csv line. :param list exclude_fields: list of attribute fields to exclude from csv line. """ - if not hasattr(document, 'write'): - raise AttributeError("Provided %r object does not have a write method." % document) - else: - document.write(export_csvs(descriptors, include_fields=include_fields, exclude_fields=exclude_fields, header=header))
+ try: + document.write(export_csvs(descriptors, include_fields=include_fields, exclude_fields=exclude_fields, header=header)) + except AttributeError: + print "Provided %r object does not have a write() method." % document + raise diff --git a/test/mocking.py b/test/mocking.py index 071514d..49b8c14 100644 --- a/test/mocking.py +++ b/test/mocking.py @@ -67,6 +67,11 @@ def return_for_args(args_to_return_value, kwarg_type=None, default=None):
:param dict args_to_return_value: mapping of arguments to the value we should provide :param object kwarg_type: type of kwarg mapping to be used in unwraping these arguments. + Though a dictionary would be the obvious choice, mapping expected keyword + arguments and their values in the keys of args_to_return_value would result + in unhashable types. Instead, a named tuple may be used and kwarg_type + provides the type of the named tuple so it may be parsed. + See test/unit/descriptor/export.py for an example. :param functor default: returns the value of this function if the args don't match something that we have, we raise a ValueError by default """
diff --git a/test/unit/descriptor/export.py b/test/unit/descriptor/export.py index d006e85..12d5ed3 100644 --- a/test/unit/descriptor/export.py +++ b/test/unit/descriptor/export.py @@ -82,10 +82,6 @@ class TestExport(unittest.TestCase): """
# Single descriptor - #print "Type descriptor: %s" % type(descriptor) - #print "Type SINGLE_ALL: %s" % type(SINGLE_ALL) - #print SINGLE_ALL - #print "Type fn Call: %s" % type(export.export_csvs([descriptor])) self.assertEquals(SINGLE_ALL + "\r\n", export.export_csvs([descriptor])) self.assertEqual(SINGLE_PART + "\r\n", export.export_csvs([descriptor], include_fields=['address', 'exit_policy'])) @@ -95,14 +91,9 @@ class TestExport(unittest.TestCase): include_fields=['address', 'exit_policy', 'fingerprint'], exclude_fields=['fingerprint']))
# Multiple descriptors - #print "Sample Call: \n %s \n\n" % export.export_csvs([descriptor, descriptor2], header=True) - self.assertEqual(DOUBLE_ALL, export.export_csvs([descriptor, descriptor2])) self.assertEqual(DOUBLE_PART, export.export_csvs([descriptor, descriptor2], include_fields=['address', 'exit_policy'])) - #print export.export_csvs([descriptor, descriptor2], exclude_fields=['onion_key', 'fingerprint']) - #print "\n %s" % descriptor2.__dict__ - self.assertEqual(DOUBLE_PART2, export.export_csvs([descriptor, descriptor2], exclude_fields=['onion_key', 'fingerprint'])) self.assertEqual(DOUBLE_PART, export.export_csvs([descriptor, descriptor2],
tor-commits@lists.torproject.org