added over.core.text.lexical_join

This commit is contained in:
Overwatch 2014-12-10 18:26:08 +01:00
parent 93e8c12547
commit 043ad86808

View file

@ -11,6 +11,36 @@ import time
# --------------------------------------------------
def lexical_join(words, oxford=False):
'''
Joins an iterable of words or sentence fragments into a lexical list:
>>> lexical_join(['this', 'that', 'one of them too'])
'this, that and one of them too'
>>> lexical_join(['this', 'that', 'one of them too'], oxford=True)
'this, that, and one of them too'
>>> lexical_join(['this', 'that'])
'this and that'
>>> lexical_join(['this'])
'this'
'''
l = len(words)
if l == 0:
return ''
elif l == 1:
return words[0]
elif l == 2:
return '%s and %s' %(str(words[0]), str(words[1]))
else:
return '%s%s and %s' %(', '.join(str(w) for w in words[:-1]), ',' if oxford_comma else '', str(words[-1]))
# --------------------------------------------------
class ProgressBar:
'''
An animated progress bar.