Skip to content

Commit

Permalink
Fix test as to whether culprit was specified.
Browse files Browse the repository at this point in the history
Added end argument to conjoin.
Added more conjoin fmt examples and tests.
  • Loading branch information
Ken Kundert authored and Ken Kundert committed May 26, 2020
1 parent b0c6888 commit bc28bc3
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 13 deletions.
46 changes: 42 additions & 4 deletions inform/inform.py
Original file line number Diff line number Diff line change
Expand Up @@ -756,7 +756,7 @@ def os_error(err):

# conjoin {{{2
# Like string join method, but supports conjunction
def conjoin(iterable, conj=' and ', sep=', ', fmt=None):
def conjoin(iterable, conj=' and ', sep=', ', end='', fmt=None):
"""Conjunction join.
Args:
Expand All @@ -767,6 +767,8 @@ def conjoin(iterable, conj=' and ', sep=', ', fmt=None):
The separator used between the next to last and last values.
sep (string):
The separator to use when joining the strings in *iterable*.
end (string):
Is added to the end of the returned string.
fmt (string):
A format string used to convert each item in *iterable* to a string.
Expand All @@ -776,7 +778,7 @@ def conjoin(iterable, conj=' and ', sep=', ', fmt=None):
Examples:
>>> from inform import conjoin, display
>>> from inform import conjoin, display, Info
>>> display(conjoin([], ' or '))
<BLANKLINE>
Expand All @@ -792,14 +794,50 @@ def conjoin(iterable, conj=' and ', sep=', ', fmt=None):
>>> display(conjoin([10.1, 32.5, 16.9], fmt='${:0.2f}'))
$10.10, $32.50 and $16.90
>>> characters = dict(
... bob = 'bob@btca.com',
... ted = 'ted@btca.com',
... carol = 'carol@btca.com',
... alice = 'alice@btca.com',
... )
>>> display(conjoin(characters.items(), fmt='{0[0]:>7} : <{0[1]}>', conj='\n', sep='\n'))
bob : <bob@btca.com>
ted : <ted@btca.com>
carol : <carol@btca.com>
alice : <alice@btca.com>
>>> characters = [
... dict(name='bob', email='bob@btca.com'),
... dict(name='ted', email='ted@btca.com'),
... dict(name='carol', email='carol@btca.com'),
... dict(name='alice', email='alice@btca.com'),
... ]
>>> display(conjoin(characters, fmt="{0[name]:>7} : <{0[email]}>", conj=', or\n', sep=',\n', end='.'))
bob : <bob@btca.com>,
ted : <ted@btca.com>,
carol : <carol@btca.com>, or
alice : <alice@btca.com>.
>>> characters = [
... Info(name='bob', email='bob@btca.com'),
... Info(name='ted', email='ted@btca.com'),
... Info(name='carol', email='carol@btca.com'),
... Info(name='alice', email='alice@btca.com'),
... ]
>>> display(conjoin(characters, fmt='{0.name:>7} : <{0.email}>', conj='; &\n', sep=';\n', end='.'))
bob : <bob@btca.com>;
ted : <ted@btca.com>;
carol : <carol@btca.com>; &
alice : <alice@btca.com>.
"""
if fmt:
lst = [fmt.format(m) for m in iterable]
else:
lst = [str(m) for m in iterable]
if conj and len(lst) > 1:
lst = lst[0:-2] + [lst[-2] + conj + lst[-1]]
return sep.join(lst)
return sep.join(lst) + end

# did_you_mean {{{2
def did_you_mean(invalid_str, valid_strs):
Expand Down Expand Up @@ -2051,7 +2089,7 @@ def _render_message(args, kwargs):
# _render_culprit {{{2
def _render_culprit(self, kwargs):
culprit = kwargs.get('culprit')
if culprit:
if culprit is not None:
if is_collection(culprit):
return self.culprit_sep.join(str(c) for c in culprit if c is not None)
return str(culprit)
Expand Down
69 changes: 60 additions & 9 deletions tests/test_utilities.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# encoding: utf8

from inform import (
Color, columns, conjoin, did_you_mean, comment, cull, display, done, error,
Error, fatal, fmt, full_stop, indent, Inform, is_collection, is_iterable,
is_mapping, is_str, join, get_prog_name, get_informer, narrate, os_error,
output, plural, render, terminate, warn, ddd, ppp, sss, vvv, ProgressBar,
Color, Info, columns, conjoin, did_you_mean, comment, cull, display, done,
error, Error, fatal, fmt, full_stop, indent, Inform, is_collection,
is_iterable, is_mapping, is_str, join, get_prog_name, get_informer, narrate,
os_error, output, plural, render, terminate, warn, ddd, ppp, sss, vvv,
ProgressBar,
)
from textwrap import dedent
import sys
Expand All @@ -28,7 +29,7 @@ def test_debug(capsys):
ddd(a, b, c)
captured = capsys.readouterr()
assert captured[0] == dedent("""
DEBUG: test_utilities.py, 28, test_utilities.test_debug():
DEBUG: test_utilities.py, 29, test_utilities.test_debug():
'a'
'b'
'c'
Expand All @@ -37,7 +38,7 @@ def test_debug(capsys):
ddd(a=a, b=b, c=c)
captured = capsys.readouterr()
assert captured[0] == dedent("""
DEBUG: test_utilities.py, 37, test_utilities.test_debug():
DEBUG: test_utilities.py, 38, test_utilities.test_debug():
a = 'a'
b = 'b'
c = 'c'
Expand All @@ -46,21 +47,21 @@ def test_debug(capsys):
ppp(a, b, c)
captured = capsys.readouterr()
assert captured[0] == dedent("""
DEBUG: test_utilities.py, 46, test_utilities.test_debug(): a b c
DEBUG: test_utilities.py, 47, test_utilities.test_debug(): a b c
""").lstrip()

vvv(a, b, c)
captured = capsys.readouterr()
assert captured[0] == dedent("""
DEBUG: test_utilities.py, 52, test_utilities.test_debug():
DEBUG: test_utilities.py, 53, test_utilities.test_debug():
a = 'a'
b = 'b'
c = 'c'
""").lstrip()

sss()
captured = capsys.readouterr()
assert captured[0].split('\n')[0] == "DEBUG: test_utilities.py, 61, test_utilities.test_debug():"
assert captured[0].split('\n')[0] == "DEBUG: test_utilities.py, 62, test_utilities.test_debug():"

def test_indent():
text=dedent('''
Expand Down Expand Up @@ -122,6 +123,56 @@ def test_conjoin():
items = [.14, 6.78, 9]
assert conjoin(items, fmt='${:0.2f}', conj=None) == '$0.14, $6.78, $9.00'

assert conjoin([], ' or ') == ''
assert conjoin(['a'], ' or ') == 'a'

assert conjoin(['a', 'b'], ' or ') == 'a or b'

assert conjoin(['a', 'b', 'c']) == 'a, b and c'

assert conjoin([10.1, 32.5, 16.9], fmt='${:0.2f}') == '$10.10, $32.50 and $16.90'

characters = dict(
bob = 'bob@btca.com',
ted = 'ted@btca.com',
carol = 'carol@btca.com',
alice = 'alice@btca.com',
)
assert conjoin(characters.items(), fmt='{0[0]} : <{0[1]}>', conj='\n', sep='\n') == dedent('''
bob : <bob@btca.com>
ted : <ted@btca.com>
carol : <carol@btca.com>
alice : <alice@btca.com>
''').strip()

characters = [
dict(name='bob', email='bob@btca.com'),
dict(name='ted', email='ted@btca.com'),
dict(name='carol', email='carol@btca.com'),
dict(name='alice', email='alice@btca.com'),
]
assert conjoin(characters, fmt="{0[name]} : <{0[email]}>", conj=' and\n', sep=',\n') == dedent('''
bob : <bob@btca.com>,
ted : <ted@btca.com>,
carol : <carol@btca.com> and
alice : <alice@btca.com>
''').strip()

characters = [
Info(name='bob', email='bob@btca.com'),
Info(name='ted', email='ted@btca.com'),
Info(name='carol', email='carol@btca.com'),
Info(name='alice', email='alice@btca.com'),
]
assert conjoin(characters, fmt='{0.name} : <{0.email}>', conj='; &\n', sep=';\n', end='.') == dedent('''
bob : <bob@btca.com>;
ted : <ted@btca.com>;
carol : <carol@btca.com>; &
alice : <alice@btca.com>.
''').strip()



def test_did_you_mean():
assert did_you_mean('abc', ['bcd']) == 'bcd'
assert did_you_mean('abc', ['cde']) == 'cde'
Expand Down

0 comments on commit bc28bc3

Please sign in to comment.