Revision 92ea69bf
b/lib/utils/text.py | ||
---|---|---|
486 | 486 |
raise errors.ProgrammerError("Shell argument '%s' contains" |
487 | 487 |
" invalid characters" % word) |
488 | 488 |
return template % args |
489 |
|
|
490 |
|
|
491 |
def FormatOrdinal(value): |
|
492 |
"""Formats a number as an ordinal in the English language. |
|
493 |
|
|
494 |
E.g. the number 1 becomes "1st", 22 becomes "22nd". |
|
495 |
|
|
496 |
@type value: integer |
|
497 |
@param value: Number |
|
498 |
@rtype: string |
|
499 |
|
|
500 |
""" |
|
501 |
tens = value % 10 |
|
502 |
|
|
503 |
if value > 10 and value < 20: |
|
504 |
suffix = "th" |
|
505 |
elif tens == 1: |
|
506 |
suffix = "st" |
|
507 |
elif tens == 2: |
|
508 |
suffix = "nd" |
|
509 |
elif tens == 3: |
|
510 |
suffix = "rd" |
|
511 |
else: |
|
512 |
suffix = "th" |
|
513 |
|
|
514 |
return "%s%s" % (value, suffix) |
b/test/ganeti.utils.text_unittest.py | ||
---|---|---|
438 | 438 |
self.assertEqual(utils.BuildShellCmd("ls %s", "ab"), "ls ab") |
439 | 439 |
|
440 | 440 |
|
441 |
class TestOrdinal(unittest.TestCase): |
|
442 |
def test(self): |
|
443 |
checks = { |
|
444 |
0: "0th", 1: "1st", 2: "2nd", 3: "3rd", 4: "4th", 5: "5th", 6: "6th", |
|
445 |
7: "7th", 8: "8th", 9: "9th", 10: "10th", 11: "11th", 12: "12th", |
|
446 |
13: "13th", 14: "14th", 15: "15th", 16: "16th", 17: "17th", |
|
447 |
18: "18th", 19: "19th", 20: "20th", 21: "21st", 25: "25th", 30: "30th", |
|
448 |
32: "32nd", 40: "40th", 50: "50th", 55: "55th", 60: "60th", 62: "62nd", |
|
449 |
70: "70th", 80: "80th", 83: "83rd", 90: "90th", 91: "91st", |
|
450 |
582: "582nd", 999: "999th", |
|
451 |
} |
|
452 |
|
|
453 |
for value, ordinal in checks.items(): |
|
454 |
self.assertEqual(utils.FormatOrdinal(value), ordinal) |
|
455 |
|
|
456 |
|
|
441 | 457 |
if __name__ == "__main__": |
442 | 458 |
testutils.GanetiTestProgram() |
Also available in: Unified diff