def test_normal_use(self): records = load_file('csv', ',', ['title','last','first','year']) self.assertEqual(records, [{'year': '2008', 'first': 'Robert', 'last': 'Martin', 'title': 'Clean Code'}, {'year': '2008', 'first': 'James', 'last': 'Shore', 'title': 'The Art of Agile Development'}] )
def test_too_many_columns(self): # too many columns specified for data # same as above, except it ignores the extra specified columns records = load_file('csv', ',', ['title','last','first', 'year', 'bogus']) self.assertEqual(records, [{'year': '2008', 'first': 'Robert', 'last': 'Martin', 'title': 'Clean Code'}, {'year': '2008', 'first': 'James', 'last': 'Shore', 'title': 'The Art of Agile Development'}] )
def test_filter(self): # a filter that matches one record, but not both records = load_file('csv', ',', ['title','last','first','year'], 'James') self.assertEqual(records, [ {'year': '2008', 'first': 'James', 'last': 'Shore', 'title': 'The Art of Agile Development'}] ) # a filter that will match no records records = load_file('csv', ',', ['title','last','first','year'], 'Beeblebrox') self.assertEqual(records, [] ) # a filter that will match all records records = load_file('csv', ',', ['title','last','first','year'], '2008') self.assertEqual(records, [{'year': '2008', 'first': 'Robert', 'last': 'Martin', 'title': 'Clean Code'}, {'year': '2008', 'first': 'James', 'last': 'Shore', 'title': 'The Art of Agile Development'}] )
def test_too_few_columns(self): # Too few columns specified for data. # currently, it silently loads the columns specified, and # ignores the extras. # this is likely a bad behavior, just keeping it simple. records = load_file('csv', ',', ['title','last','first']) self.assertEqual(records, [{'first': 'Robert', 'last': 'Martin', 'title': 'Clean Code'}, {'first': 'James', 'last': 'Shore', 'title': 'The Art of Agile Development'}] )
'looks for the argument as a ' + \ 'substring of any of the fields', 'type': str }, '--year': { 'help': 'sort the books by year, ascending ' + \ 'instead of default sort', 'action': 'store_true' }, '--reverse': { 'help': 'reverse sort', 'action': 'store_true' } } description = "Show a list of books, alphabetical ascending by author's " + \ "last name" parser = argparse.ArgumentParser(description) for argument, options in arguments.iteritems(): parser.add_argument(argument, **options) args = parser.parse_args() sort_key = 'pubdate' if args.year else 'last' reverse_sort = True if args.reverse else False records = [] for file in files: records += load_file(*file, filter=args.filter) sort_records(records, sort_key, reverse_sort) print_records(records)