add custom separator
This commit is contained in:
parent
6c3238730a
commit
55d8eb8254
12
glue.py
12
glue.py
@ -4,7 +4,9 @@ import collections
|
||||
def glue(*args, **kwargs):
|
||||
"""
|
||||
Glue takes in any number of arguments, named or not, and any depth list
|
||||
It will flatten them all and return them joined with spaces.
|
||||
It will flatten them all and return them joined with kwargs['separator']
|
||||
(This is the only kwarg that will be used, anything else will be
|
||||
included)
|
||||
"""
|
||||
|
||||
def _flatten(l):
|
||||
@ -19,6 +21,10 @@ def glue(*args, **kwargs):
|
||||
else:
|
||||
yield el
|
||||
|
||||
separator = ' ' # default
|
||||
if 'separator' in kwargs:
|
||||
separator = kwargs.pop('separator')
|
||||
|
||||
# I dont care what order you submit them in, they will be alphabetical
|
||||
kwargs = collections.OrderedDict(
|
||||
sorted(kwargs.items(), key=lambda t: t[0]))
|
||||
@ -30,5 +36,7 @@ def glue(*args, **kwargs):
|
||||
# flatten crap out of this!
|
||||
what_to_glue = _flatten(what_to_glue)
|
||||
|
||||
#set separator
|
||||
|
||||
# safeguard against nonstrings
|
||||
return " ".join([str(x) for x in what_to_glue])
|
||||
return separator.join(str(x) for x in what_to_glue)
|
||||
|
4
tests.py
4
tests.py
@ -8,6 +8,10 @@ class GlueTests(unittest.TestCase):
|
||||
output = glue("why", "would", "you", "run", "these", "tests")
|
||||
assert output == "why would you run these tests"
|
||||
|
||||
def test_tuple_custom_separator(self):
|
||||
output = glue("why", "would", "you", separator=",")
|
||||
assert output == "why,would,you"
|
||||
|
||||
def test_tuple_and_named(self):
|
||||
output = glue("why", "would", "you", "run", "these", "tests", who="bozo")
|
||||
assert output == "why would you run these tests bozo"
|
||||
|
Loading…
Reference in New Issue
Block a user