I want to create a shortcut function for creating a question object in my django tests.
I don't like to type something like new_q(timedelta(minutes=1))
all the time and I want to write that timedelta only once and be able to pass it's arguments in a nice way to the new_q
function.
Is there any pythonic one line solution to this in which I don't have to capture the kwargs to a variable and then create a timedelta object in a new line?
from datetime import timedelta
from django.utils import timezone
def new_q(question_text = '', offset=timedelta(**kwargs)):
time = timezone.now() + offset
return Question.objects.create(question_text=question_text, pub_date=time)
usage:
test1 = new_q(minutes=1)
test2 = new_q('yo?', seconds=1)
test2 = new_q(question_text = 'maw?', hours=11)
test2 = new_q(question_text = 'maw?')
test2 = new_q('yo?')
def new_q(question_text = '', **kwargs):
time = timezone.now() + timedelta(**kwargs)
...
This is still pretty concise: it mentions kwargs
twice, but there's no offset
or anything else extra.