Added nItems.

This commit is contained in:
Jeremy Fincher 2003-09-03 09:40:26 +00:00
parent 5857a17f88
commit 4c980aeb3e
2 changed files with 12 additions and 0 deletions

View File

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

View File

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