import re


def pulse_factory(cls, name=None, **kwargs):
    """Returns a function that creates an instance
    if the given pulse class.

    Keyword arguments are passed to ``cls.__init__()``.

    Args:
        cls (type): Subclass of Pulse of which to create an instance.
        name (optional, str): Name of the resulting pulse. If None,
            will use a snake-case version of the class name,
            e.g. 'GaussianPulse' -> 'gaussian_pulse'. Default: None.

    Returns:
        callable: A function that takes no arguments and returns
        an instance of ``cls``.
    """
    if name is None:
        # turn 'GaussianPulse' into 'gaussian_pulse'
        name = "_".join(re.findall("[a-zA-Z][^A-Z]*", cls.__name__)).lower()
    return lambda: cls(name=name, **kwargs)