From 7bfc9cbdd3b2a480a56e8a674e4e8b956f44a3e0 Mon Sep 17 00:00:00 2001 From: James Vega Date: Mon, 9 May 2005 17:20:43 +0000 Subject: [PATCH] utils/str: Rename nth to ordinal and have it return the full string instead of just the ordinal ending. --- src/utils/str.py | 21 +++++++++++++-------- test/test_utils.py | 6 ++++++ 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/src/utils/str.py b/src/utils/str.py index 9fcf3d122..264aa1dd6 100644 --- a/src/utils/str.py +++ b/src/utils/str.py @@ -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.""" diff --git a/test/test_utils.py b/test/test_utils.py index 8d41d9766..3c4e0835d 100644 --- a/test/test_utils.py +++ b/test/test_utils.py @@ -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)