Added function commaAndify to turn a list of strings into a proper English foo, bar, and baz string.

This commit is contained in:
Jeremy Fincher 2003-08-20 09:24:57 +00:00
parent 15ba96b4eb
commit 01eb6934ef
2 changed files with 26 additions and 6 deletions

View File

@ -259,4 +259,15 @@ def findBinaryInPath(s):
break
return cmdLine
def commaAndify(L):
if len(L) == 0:
return ''
elif len(L) == 1:
return L[0]
elif len(L) == 2:
return '%s and %s' % (L[0], L[1])
else:
L[-1] = 'and %s' % L[-1]
return ', '.join(L)
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:

View File

@ -128,8 +128,17 @@ class UtilsTest(unittest.TestCase):
self.assertEqual(f('fooba/rba/z'), 'foorz')
def testFindBinaryInPath(self):
if os.name == 'posix':
self.assertEqual(None, utils.findBinaryInPath('asdfhjklasdfhjkl'))
self.assertEqual('/bin/sh', utils.findBinaryInPath('sh'))
def testCommaAndify(self):
L = ['foo']
self.assertEqual(utils.commaAndify(L), 'foo')
L.append('bar')
self.assertEqual(utils.commaAndify(L), 'foo and bar')
L.append('baz')
self.assertEqual(utils.commaAndify(L), 'foo, bar, and baz')
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78: