File size: 1,461 Bytes
d8d14f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import functools
import logging


def math_eval(func1, func2):
    """Math evaluation decorator.

    Args:
        func1 (_type_): _description_
        func2 (_type_): _description_

    Example:
    >>> @math_eval(ground_truth, generated_func)
    >>> def test_func(x):
    >>>     return x
    >>> result1, result2 = test_func(5)
    >>> print(f"Result from ground_truth: {result1}")
    >>> print(f"Result from generated_func: {result2}")

    """

    def decorator(func):
        @functools.wraps(func)
        def wrapper(*args, **kwargs):
            try:
                result1 = func1(*args, **kwargs)
            except Exception as e:
                logging.error(f"Error in func1: {e}")
                result1 = None

            try:
                result2 = func2(*args, **kwargs)
            except Exception as e:
                logging.error(f"Error in func2: {e}")
                result2 = None

            if result1 != result2:
                logging.warning(
                    f"Outputs do not match: {result1} != {result2}"
                )

            return result1, result2

        return wrapper

    return decorator


# def ground_truth(x):
#     return x * 2

# def generated_func(x):
#     return x - 10

# @math_eval(ground_truth, generated_func)
# def test_func(x):
#     return x

# result1, result2 = test_func(5)
# print(f"Result from ground_truth: {result1}")
# print(f"Result from generated_func: {result2}")