(validation_func)
return __decorator
A decorator that applies given validation_func just before calling
    the decorated func.

    Example:

        @pre_condition(lambda var1, var3: var1 == var3)
        def myfunc(var1, var2=99, var3=1):
            return "I ran"

        myfunc(1, 2, 3)  # raises
        myfunc(1, 2, 1)  # --> "I ran"
        myfunc(1, var3=1)  # --> "I ran"
        myfunc(1, var3=1, var2=2)  # --> "I ran"

        myfunc(1)  # raises  - it's not smart enough to extract default kwargs
A decorator that applies given validation_func just before calling
    the decorated func.
A decorator that applies given validation_func just before calling the decorated func .
def pre_condition(validation_func):
    """A decorator that applies given validation_func just before calling
    the decorated func.

    Example:

        @pre_condition(lambda var1, var3: var1 == var3)
        def myfunc(var1, var2=99, var3=1):
            return "I ran"

        myfunc(1, 2, 3)  # raises
        myfunc(1, 2, 1)  # --> "I ran"
        myfunc(1, var3=1)  # --> "I ran"
        myfunc(1, var3=1, var2=2)  # --> "I ran"

        myfunc(1)  # raises  - it's not smart enough to extract default kwargs
    """
    def __decorator(func):
        @functools.wraps(func)
        def _decorator(*args, **kwargs):
            kws2 = dict(zip(inspect.getargspec(func).args, args))
            kws2.update(kwargs)
            vf = inspect.getargspec(validation_func)
            validation_args = (
                kws2[k] for k in vf.args if k in kws2)
            assert validation_func(*validation_args), (
                "validation_func %s did not return True"
                % validation_func.__name__)
            return func(*args, **kwargs)
        return _decorator
    return __decorator
def pre_condition ( validation_func ) : def __decorator ( func ) : @ functools . wraps ( func ) def _decorator ( * args , * * kwargs ) : kws2 = dict ( zip ( inspect . getargspec ( func ) . args , args ) ) kws2 . update ( kwargs ) vf = inspect . getargspec ( validation_func ) validation_args = ( kws2 [ k ] for k in vf . args if k in kws2 ) assert validation_func ( * validation_args ) , ( "validation_func %s did not return True" % validation_func . __name__ ) return func ( * args , * * kwargs ) return _decorator return __decorator