utils/str: Rename nth to ordinal and have it return the full string instead of just the ordinal ending.

This commit is contained in:
James Vega 2005-05-09 17:20:43 +00:00
parent 7deadddbe8
commit 7bfc9cbdd3
2 changed files with 19 additions and 8 deletions

View File

@ -308,18 +308,23 @@ def nItems(n, item, between=None):
else: else:
return format('%s %s %s', n, between, item) return format('%s %s %s', n, between, item)
def nth(i): def ordinal(i):
"""Returns i + the ordinal indicator for the number.
Example: ordinal(3) => '3rd'
"""
i = int(i) i = int(i)
if i % 100 in (11,12,13): if i % 100 in (11,12,13):
return 'th' return '%sth' % i
ord = 'th'
test = i % 10 test = i % 10
if test == 1: if test == 1:
return 'st' ord = 'st'
if test == 2: elif test == 2:
return 'nd' ord = 'nd'
if test == 3: elif test == 3:
return 'rd' ord = 'rd'
return 'th' return '%s%s' % (i, ord)
def be(i): def be(i):
"""Returns the form of the verb 'to be' based on the number i.""" """Returns the form of the verb 'to be' based on the number i."""

View File

@ -370,6 +370,12 @@ class StrTest(SupyTestCase):
self.assertEqual(nItems(2, 'tool', 'crazy'), '2 crazy tools') self.assertEqual(nItems(2, 'tool', 'crazy'), '2 crazy tools')
self.assertEqual(nItems(2, 'tool'), '2 tools') self.assertEqual(nItems(2, 'tool'), '2 tools')
def testOrdinal(self):
ordinal = utils.str.ordinal
self.assertEqual(ordinal(3), '3rd')
self.assertEqual(ordinal('3'), '3rd')
self.assertRaises(ValueError, ordinal, 'a')
def testEllipsisify(self): def testEllipsisify(self):
f = utils.str.ellipsisify f = utils.str.ellipsisify
self.assertEqual(f('x'*30, 30), 'x'*30) self.assertEqual(f('x'*30, 30), 'x'*30)