import functools


def qutip_callback(func, **kwargs):
    """Convert `func` into the correct form of a QuTiP time-dependent control

    QuTiP requires that "callback" functions that are used to express
    time-dependent controls take a parameter `t` and `args`. This function
    takes a function `func` that takes `t` as its first parameter and an
    arbitrary number of other parameters. The given `kwargs` set values for
    these other parameters. Parameters not contained in `kwargs` are set
    *at runtime* from the `args` dict.
    """

    partial_func = functools.partial(func, **kwargs)

    def callback(t, args):
        if args is None:
            args = {}
        return partial_func(t, **args)

    return callback