Added keyword option And to commaAndify to allow specifying a word other than 'and' as the last separator.

This commit is contained in:
Jeremy Fincher 2003-09-12 08:55:30 +00:00
parent a169b64740
commit 0aa448bfc7
2 changed files with 6 additions and 3 deletions

View File

@ -241,7 +241,7 @@ def findBinaryInPath(s):
break break
return cmdLine return cmdLine
def commaAndify(seq): def commaAndify(seq, And='and'):
"""Given a a sequence, returns an english clause for that sequence. """Given a a sequence, returns an english clause for that sequence.
I.e., given [1, 2, 3], returns '1, 2, and 3' I.e., given [1, 2, 3], returns '1, 2, and 3'
@ -252,9 +252,9 @@ def commaAndify(seq):
elif len(L) == 1: elif len(L) == 1:
return L[0] return L[0]
elif len(L) == 2: elif len(L) == 2:
return '%s and %s' % (L[0], L[1]) return '%s %s %s' % (L[0], And, L[1])
else: else:
L[-1] = 'and %s' % L[-1] L[-1] = '%s %s' % (And, L[-1])
return ', '.join(L) return ', '.join(L)
_unCommaTheRe = re.compile(r'(.*),\s*(the)$', re.I) _unCommaTheRe = re.compile(r'(.*),\s*(the)$', re.I)

View File

@ -144,14 +144,17 @@ class UtilsTest(unittest.TestCase):
L = ['foo'] L = ['foo']
original = L[:] original = L[:]
self.assertEqual(utils.commaAndify(L), 'foo') self.assertEqual(utils.commaAndify(L), 'foo')
self.assertEqual(utils.commaAndify(L, 'or'), 'foo')
self.assertEqual(L, original) self.assertEqual(L, original)
L.append('bar') L.append('bar')
original = L[:] original = L[:]
self.assertEqual(utils.commaAndify(L), 'foo and bar') self.assertEqual(utils.commaAndify(L), 'foo and bar')
self.assertEqual(utils.commaAndify(L, 'or'), 'foo or bar')
self.assertEqual(L, original) self.assertEqual(L, original)
L.append('baz') L.append('baz')
original = L[:] original = L[:]
self.assertEqual(utils.commaAndify(L), 'foo, bar, and baz') self.assertEqual(utils.commaAndify(L), 'foo, bar, and baz')
self.assertEqual(utils.commaAndify(L, 'or'), 'foo, bar, or baz')
self.assertEqual(L, original) self.assertEqual(L, original)
self.failUnless(utils.commaAndify(sets.Set(L))) self.failUnless(utils.commaAndify(sets.Set(L)))