<commit_before>from inspect import getcallargs
from functools import wraps

from covenant.util import toggled_decorator_func
from covenant.exceptions import (PreconditionViolationError,
                                 PostconditionViolationError)


@toggled_decorator_func
def pre(condition):
    """Enforce a precondition on the decorated function.

    The `condition` must be a callable that receives the same keyword arguments
    as the function it's being applied to.

    """
    def _pre(func):
        original_func = getattr(func, '_original_func', func)

        @wraps(func)
        def wrapped_func(*args, **kwargs):
            callargs = getcallargs(original_func, *args, **kwargs)

            try:
                result = condition(**callargs)
            except Exception as e:
                # TODO: Better error message including exception
                raise PreconditionViolationError("Precondition check failed.")

            if not result:
                raise PreconditionViolationError("Precondition check failed.")

            return func(*args, **kwargs)

        wrapped_func._original_func = original_func
        return wrapped_func
    return _pre


@toggled_decorator_func
def post(condition):
    """Enforce a postcondition on the decorated function.

    The `condition` must be a callable that receives the return value of the
    function it's being applied to as its first parameter, and the keyword
    arguments of the function it's applied to as its remaining parameters.

    """
    def _post(func):
        original_func = getattr(func, '_original_func', func)

        @wraps(func)
        def wrapped_func(*args, **kwargs):
            callargs = getcallargs(original_func, *args, **kwargs)

            value = func(*args, **kwargs)

            try:
                result = condition(value, **callargs)
            except Exception as e:
                # TODO: Better error message including exception
                raise PostconditionViolationError("Postcondition check failed.")

            if not result:
                raise PostconditionViolationError("Postcondition check failed.")

            return value

        wrapped_func._original_func = original_func
        return wrapped_func
    return _post

__all__ = ["pre", "post"]<commit_msg>Simplified post and pre decorators using the decorator module<commit_after>from inspect import getcallargs
from functools import wraps
from decorator import decorator

from covenant.util import toggled_decorator_func
from covenant.exceptions import (PreconditionViolationError,
                                 PostconditionViolationError)


@toggled_decorator_func
def pre(condition):
    """Enforce a precondition on the decorated function.

    The `condition` must be a callable that receives the same keyword arguments
    as the function it's being applied to.

    """
    @decorator
    def _pre(func, *args, **kwargs):
        callargs = getcallargs(func, *args, **kwargs)

        try:
            result = condition(**callargs)
        except Exception as e:
            # TODO: Better error message including exception
            raise PreconditionViolationError("Precondition check failed.")

        if not result:
            raise PreconditionViolationError("Precondition check failed.")

        return func(*args, **kwargs)
    return _pre


@toggled_decorator_func
def post(condition):
    """Enforce a postcondition on the decorated function.

    The `condition` must be a callable that receives the return value of the
    function it's being applied to as its first parameter, and the keyword
    arguments of the function it's applied to as its remaining parameters.

    """
    @decorator
    def _post(func, *args, **kwargs):
        callargs = getcallargs(func, *args, **kwargs)

        value = func(*args, **kwargs)

        try:
            result = condition(value, **callargs)
        except Exception as e:
            # TODO: Better error message including exception
            raise PostconditionViolationError("Postcondition check failed.")

        if not result:
            raise PostconditionViolationError("Postcondition check failed.")

        return value
    return _post

__all__ = ["pre", "post"]
{'fraction_non_alphanumeric': 0.06998590887740723, 'fraction_numerical': 0.0, 'mean_word_length': 2.6620808254514188, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6849615', 'n_tokens_mistral': 1074, 'n_tokens_neox': 1035, 'n_words': 377}