• Function Captchas

    Dorthu        
    0 Likes0 Commentspython

    >>> def captcha(challenge=None, answer=None):
    ...   def _dec(f):
    ...     @wraps(f)
    ...     def _inner(*args, **kwargs):
    ...       if not challenge in kwargs or not kwargs[challenge] == answer:
    ...         raise ValueError('captcha failed!')
    ...       del kwargs[challenge]
    ...       return f(*args, **kwargs)
    ...     return _inner
    ...   return _dec
    ...
    >>> @captcha(challenge='two_plus_three', answer=5)
    ... def foo(bar):
    ...   print("bar is {}".format(bar))
    ...
    >>> foo('test')
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "<stdin>", line 6, in _inner
    ValueError: captcha failed!
    >>> foo('test', two_plus_three=5)
    bar is test

Comments (0)