diff --git a/core/text.py b/core/text.py index c543010..a357b37 100644 --- a/core/text.py +++ b/core/text.py @@ -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.