remove blank-to-comma assumption
This commit is contained in:
parent
30c9499564
commit
2e3ce55fce
2 changed files with 11 additions and 13 deletions
|
|
@ -57,21 +57,24 @@ class TestDedent(TestCase):
|
||||||
class TestListToString(TestCase):
|
class TestListToString(TestCase):
|
||||||
"""
|
"""
|
||||||
Default function header from utils.py:
|
Default function header from utils.py:
|
||||||
list_to_string(inlist, endsep="and", addquote=False)
|
list_to_string(inlist, endsep=", and", addquote=False)
|
||||||
|
|
||||||
Examples:
|
Examples:
|
||||||
no endsep:
|
with default endsep:
|
||||||
|
[1,2,3] -> '1, 2, and 3'
|
||||||
|
with endsep==',':
|
||||||
[1,2,3] -> '1, 2, 3'
|
[1,2,3] -> '1, 2, 3'
|
||||||
with endsep=='and':
|
with endsep=='and':
|
||||||
[1,2,3] -> '1, 2 and 3'
|
[1,2,3] -> '1, 2 and 3'
|
||||||
with addquote and endsep
|
with addquote and endsep="and"
|
||||||
[1,2,3] -> '"1", "2" and "3"'
|
[1,2,3] -> '"1", "2" and "3"'
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def test_list_to_string(self):
|
def test_list_to_string(self):
|
||||||
self.assertEqual("1, 2, 3", utils.list_to_string([1, 2, 3], endsep=""))
|
|
||||||
self.assertEqual('"1", "2", "3"', utils.list_to_string([1, 2, 3], endsep="", addquote=True))
|
|
||||||
self.assertEqual("1, 2, and 3", utils.list_to_string([1, 2, 3]))
|
self.assertEqual("1, 2, and 3", utils.list_to_string([1, 2, 3]))
|
||||||
|
self.assertEqual("1, 2, 3", utils.list_to_string([1, 2, 3], endsep=","))
|
||||||
|
self.assertEqual("1, 2 and 3", utils.list_to_string([1, 2, 3], endsep="and"))
|
||||||
|
self.assertEqual('"1", "2", "3"', utils.list_to_string([1, 2, 3], endsep=",", addquote=True))
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
'"1", "2" and "3"', utils.list_to_string([1, 2, 3], endsep="and", addquote=True)
|
'"1", "2" and "3"', utils.list_to_string([1, 2, 3], endsep="and", addquote=True)
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -372,8 +372,7 @@ def iter_to_str(iterable, endsep=", and", addquote=False):
|
||||||
iterable (any): Usually an iterable to print. Each element must be possible to
|
iterable (any): Usually an iterable to print. Each element must be possible to
|
||||||
present with a string. Note that if this is a generator, it will be
|
present with a string. Note that if this is a generator, it will be
|
||||||
consumed by this operation.
|
consumed by this operation.
|
||||||
endsep (str, optional): If set, the last item separator will
|
endsep (str, optional): The last item separator will be replaced with this value.
|
||||||
be replaced with this value.
|
|
||||||
addquote (bool, optional): This will surround all outgoing
|
addquote (bool, optional): This will surround all outgoing
|
||||||
values with double quotes.
|
values with double quotes.
|
||||||
|
|
||||||
|
|
@ -381,13 +380,12 @@ def iter_to_str(iterable, endsep=", and", addquote=False):
|
||||||
str: The list represented as a string.
|
str: The list represented as a string.
|
||||||
|
|
||||||
Notes:
|
Notes:
|
||||||
Default is to use 'Oxford comma', like 1, 2, 3, and 4. To remove, give
|
Default is to use 'Oxford comma', like 1, 2, 3, and 4.
|
||||||
`endsep` as just `and`.
|
|
||||||
|
|
||||||
Examples:
|
Examples:
|
||||||
|
|
||||||
```python
|
```python
|
||||||
>>> list_to_string([1,2,3], endsep='')
|
>>> list_to_string([1,2,3], endsep=',')
|
||||||
'1, 2, 3'
|
'1, 2, 3'
|
||||||
>>> list_to_string([1,2,3], ensdep='and')
|
>>> list_to_string([1,2,3], ensdep='and')
|
||||||
'1, 2 and 3'
|
'1, 2 and 3'
|
||||||
|
|
@ -412,9 +410,6 @@ def iter_to_str(iterable, endsep=", and", addquote=False):
|
||||||
elif endsep:
|
elif endsep:
|
||||||
# normal space-separated end separator
|
# normal space-separated end separator
|
||||||
endsep = " " + str(endsep).strip()
|
endsep = " " + str(endsep).strip()
|
||||||
else:
|
|
||||||
# no separator given - use comma
|
|
||||||
endsep = ","
|
|
||||||
|
|
||||||
if len_iter == 1:
|
if len_iter == 1:
|
||||||
return str(iterable[0])
|
return str(iterable[0])
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue