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:
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)
if i % 100 in (11,12,13):
return 'th'
return '%sth' % i
ord = 'th'
test = i % 10
if test == 1:
return 'st'
if test == 2:
return 'nd'
if test == 3:
return 'rd'
return 'th'
ord = 'st'
elif test == 2:
ord = 'nd'
elif test == 3:
ord = 'rd'
return '%s%s' % (i, ord)
def be(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'), '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):
f = utils.str.ellipsisify
self.assertEqual(f('x'*30, 30), 'x'*30)