diff --git a/src/utils.py b/src/utils.py index 081fd4ce0..64aac7ea8 100755 --- a/src/utils.py +++ b/src/utils.py @@ -309,6 +309,12 @@ def pluralize(i, s): else: return s + 's' +def nItems(n, item, between=None): + if between is None: + return '%s %s' % (n, pluralize(n, item)) + else: + return '%s %s %s' % (n, between, pluralize(n, item)) + def be(i): """Returns the form of the verb 'to be' based on the number i.""" if i == 1: diff --git a/test/test_utils.py b/test/test_utils.py index 3cc5531c1..678aeefbc 100644 --- a/test/test_utils.py +++ b/test/test_utils.py @@ -170,5 +170,11 @@ class UtilsTest(unittest.TestCase): utils.sortBy(str.lower, L) self.assertEqual(L, ['abc', 'AD', 'z']) + def testNItems(self): + self.assertEqual(utils.nItems(1, 'tool', 'crazy'), '1 crazy tool') + self.assertEqual(utils.nItems(1, 'tool'), '1 tool') + self.assertEqual(utils.nItems(2, 'tool', 'crazy'), '2 crazy tools') + self.assertEqual(utils.nItems(2, 'tool'), '2 tools') + # vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78: