From 15b0977ab62417bdd168e2bdd55aee8b64626d3d Mon Sep 17 00:00:00 2001 From: Tyrel Souza Date: Thu, 30 Oct 2014 14:27:36 -0400 Subject: [PATCH] flatten --- README.md | 6 +++--- coverage_report.txt | 6 +++--- glue.py | 12 ++++++++++-- tests.py | 4 ++++ 4 files changed, 20 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 240729a..064ac93 100644 --- a/README.md +++ b/README.md @@ -17,8 +17,8 @@ You should probably just use [str.join](https://docs.python.org/2/library/stdtyp ``` Name Stmts Miss Cover Missing ------------------------------------- -glue 6 0 100% -tests 17 0 100% +glue 13 0 100% +tests 20 0 100% ------------------------------------- -TOTAL 23 0 100% +TOTAL 33 0 100% ``` diff --git a/coverage_report.txt b/coverage_report.txt index b526ebe..8fe495a 100644 --- a/coverage_report.txt +++ b/coverage_report.txt @@ -1,6 +1,6 @@ Name Stmts Miss Cover Missing ------------------------------------- -glue 6 0 100% -tests 17 0 100% +glue 13 0 100% +tests 20 0 100% ------------------------------------- -TOTAL 23 0 100% +TOTAL 33 0 100% diff --git a/glue.py b/glue.py index 1f75332..8c82bea 100644 --- a/glue.py +++ b/glue.py @@ -1,7 +1,15 @@ -from collections import OrderedDict +import collections def glue(*args, **kwargs): + def flatten(l): + for el in l: + if isinstance(el, collections.Iterable) and not isinstance(el, basestring): + for sub in flatten(el): + yield sub + else: + yield el what_to_glue = list(args) - kwargs = OrderedDict(sorted(kwargs.items(), key=lambda t: t[0])) # I dont care what order you submit them in, they will be alphabetical + kwargs = collections.OrderedDict(sorted(kwargs.items(), key=lambda t: t[0])) # I dont care what order you submit them in, they will be alphabetical what_to_glue.extend([v for k,v in kwargs.iteritems()]) # Ignore all your keys, because you're doing this wrong anyway + what_to_glue = flatten(what_to_glue) return " ".join(what_to_glue) diff --git a/tests.py b/tests.py index b21338c..f2df67c 100644 --- a/tests.py +++ b/tests.py @@ -19,5 +19,9 @@ class GlueTests(unittest.TestCase): assert output == "bozo clown" assert "what" not in output + def test_flaten(self): + output = glue("why", ["would", ["you", "run"], "these"], "tests") + assert output == "why would you run these tests" + if __name__ == '__main__': unittest.main()