import functools


def wrap(before=None, after=None, condition=lambda *args, **kwargs: True):
    """
    A helper for creating decorators.

    Runs a "before" function before the decorated function, and an "after"
    function afterwards. The condition check is performed once before
    the decorated function.

    """
    def decorator(func):
        @functools.wraps(func)
        def wrapped(*args, **kwargs):
            yes = condition(*args, **kwargs)
            if yes and before:
                before()
            result = func(*args, **kwargs)
            if yes and after:
                after()
            return result
        return wrapped
    return decorator