added over.core.text.lexical_join
This commit is contained in:
parent
93e8c12547
commit
043ad86808
1 changed files with 30 additions and 0 deletions
30
core/text.py
30
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:
|
class ProgressBar:
|
||||||
'''
|
'''
|
||||||
An animated progress bar.
|
An animated progress bar.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue