because I reminded of this thread earlier and I don't want it to DIE
here's something I learned about python only recently that I love
the * and ** operators are super cool
there's the most common use for them, in function definitions:
def print_garbage(*args, **kwargs):
for positional_arg in args:
print(positional_arg)
for keyword, val in kwargs.items():
print('Keyword: {0}\nValue: {1}'.format(keyword, val))(to be clear: the asterisks are important, not the words. it could be *dog and **cat for all python cares)now a call like
print_garbage(12, 14, sixteen=16) will print:
12
14
Keyword: sixteen
Value: 16which, just so that everyone knows, is an unimaginably cool way to allow any number of arguments
but!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! it also works the other way around! which is the thing that I recently learned!
so
print_garbage(*[12, 14], **{'sixteen': 16}) will have the same output
that particular feature is probably not the most commonly useful feature in the world
however, it means that you can choose what keyword arguments to define, dynamically!
that's especially useful with django, whose
Model.objects.filter() functions can have QUITE A LOT of possible keyword arguments
so, rather than this
huge, disgusting, vomit-inducing mess of a function, you can have
this!!!!!!!!(note: the second code will not actually work 100% of the way, I haven't tested it, but I know for certain that, for example, "defect" should actually be "defects__name" but I literally JUST wrote that to show off this awesome feature, and in order for it to work exactly like it is I'm gonna need to edit the form that submits this stuff and blah blah blah)
now that I think about it, it would be a lot better to have several different possible filter parts....... like. rather than "by=date&for=10/12/2015" and "by=plant&for=A" I would just have "date=10/12/2015" or "plant=A"
that would not only look a lot cleaner in the URL but it would also allow multiple fields to be filtered by, like "date=10/12/2015&plant=A".............................
..................
man. I put so much work into that terrible function and form only to leave it behind now. sigh