From 725ccd5c7e309c0e9b79f1a573dfc30781e6e247 Mon Sep 17 00:00:00 2001 From: Tyrel Souza Date: Thu, 30 Oct 2014 14:37:21 -0400 Subject: [PATCH] added comments and docstrings --- glue.py | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/glue.py b/glue.py index ad15763..7fe4d03 100644 --- a/glue.py +++ b/glue.py @@ -1,16 +1,31 @@ import collections + def glue(*args, **kwargs): - def flatten(l): + """ + 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. + """ + def _flatten(l): + """ + from: Cristian at http://stackoverflow.com/a/2158532 + """ for el in l: if isinstance(el, collections.Iterable) and not isinstance(el, basestring): - for sub in flatten(el): + for sub in _flatten(el): yield sub else: yield el - what_to_glue = list(args) - 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) - what_to_glue = [str(x) for x in what_to_glue] - return " ".join(what_to_glue) + + # I dont care what order you submit them in, they will be alphabetical + kwargs = collections.OrderedDict( + sorted(kwargs.items(), key=lambda t: t[0])) + + # Ignore all your keys, because you're doing this wrong anyway + what_to_glue = list(args).extend([v for k, v in kwargs.iteritems()]) + + # flatten crap out of this! + what_to_glue = _flatten(what_to_glue) + + # safeguard against nonstrings + return " ".join([str(x) for x in what_to_glue])