def test_normal_use(self): records = [ {'a':1, 'b':2, 'c': 3}, {'a':4, 'b':1, 'c': 4} ] sort_records(records, 'b') self.assertEqual(records, [ {'a':4, 'b':1, 'c': 4}, {'a':1, 'b':2, 'c': 3} ])
def test_reverse_order(self): records = [ {'a':1, 'b':2, 'c': 3}, {'a':4, 'b':1, 'c': 4} ] sort_records(records, 'a', True) self.assertEqual(records, [ {'a':4, 'b':1, 'c': 4}, {'a':1, 'b':2, 'c': 3} ])
def test_bad_key(self): records = [ {'a':1, 'b':2, 'c': 3}, {'a':4, 'b':1, 'c': 4} ] with self.assertRaises(KeyError): sort_records(records, 'd', True)
'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)