Skip to content

Commit

Permalink
add end and allow arguments to full_stop()
Browse files Browse the repository at this point in the history
  • Loading branch information
Ken Kundert authored and Ken Kundert committed May 26, 2020
1 parent 31fb7fe commit 20107aa
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
13 changes: 10 additions & 3 deletions inform/inform.py
Original file line number Diff line number Diff line change
Expand Up @@ -771,6 +771,7 @@ def conjoin(iterable, conj=' and ', sep=', ', end='', fmt=None):
Is added to the end of the returned string.
fmt (string):
A format string used to convert each item in *iterable* to a string.
If not given, str() is used.
Return the items of the *iterable* joined into a string, where *conj* is
used to join the last two items in the list, and *sep* is used to join the
Expand Down Expand Up @@ -983,7 +984,7 @@ def __format__(self, formatter):


# full_stop {{{2
def full_stop(sentence):
def full_stop(sentence, end='.', allow='.?!'):
"""Add period to end of string if it is needed.
A full stop (a period) is added if there is no terminating punctuation at the
Expand All @@ -1002,10 +1003,16 @@ def full_stop(sentence):
>>> full_stop('Is the file is out of date?')
'Is the file is out of date?'
You can override the allowed endings and desired ending.
>>> cases = '1, 3 9, 12.'.split()
>>> print(*[full_stop(c, end=',', allow=',.') for c in cases])
1, 3, 9, 12.
"""
sentence = str(sentence).rstrip()
try:
return sentence if sentence[-1] in '.?!' else sentence + '.'
return sentence if sentence[-1] in allow else sentence + end
except:
# this occurs when sentence is empty string
return sentence
Expand Down Expand Up @@ -1233,7 +1240,7 @@ def escape(self):

# _draw {{{3
def _draw(self, index):
if not self.informant:
if not self.informant: # pragma: no cover
return
stream_info = self.informer.get_stream_info(self.informant)
if self.prefix:
Expand Down
4 changes: 4 additions & 0 deletions tests/test_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -516,13 +516,16 @@ def test_plural():
assert '{:~@ cart|s}'.format(plural(0, invert='~', num='@', slash='|')) == '0 cart'
assert '{:~@ cart|s}'.format(plural(1, invert='~', num='@', slash='|')) == '1 carts'
assert '{:~@ cart|s}'.format(plural(2, invert='~', num='@', slash='|')) == '2 cart'
assert plural(2, invert='~', num='@', slash='|').format('~@ cart|s') == '2 cart'

def test_full_stop():
assert full_stop('hey now') == 'hey now.'
assert full_stop('hey now.') == 'hey now.'
assert full_stop('hey now?') == 'hey now?'
assert full_stop('hey now!') == 'hey now!'
assert full_stop('') == ''
cases = '1, 2, 3, 5 7, 11 13 17.'.split()
assert ' '.join(full_stop(c, end=',', allow=',.') for c in cases) == '1, 2, 3, 5, 7, 11, 13, 17.'

def test_os_error():
try:
Expand Down Expand Up @@ -1368,6 +1371,7 @@ class Orwell(Info):
assert george.peace == 'war'
assert george.truth == 'lies'
assert george.happiness is None
assert george.render(template='peace={peace}, truth={truth}') == 'peace=war, truth=lies'

def test_oblong():
from inform import render_bar
Expand Down

0 comments on commit 20107aa

Please sign in to comment.