|
| 1 | +"""Additional utils for sweep.""" |
| 2 | + |
| 3 | +# Copyright (C) 2023 Intel Corporation |
| 4 | +# SPDX-License-Identifier: Apache-2.0 |
| 5 | + |
| 6 | + |
| 7 | +import functools |
| 8 | +import io |
| 9 | +import logging |
| 10 | +import sys |
| 11 | +from enum import Enum |
| 12 | +from typing import Any, Callable |
| 13 | + |
| 14 | +logger = logging.getLogger(__name__) |
| 15 | + |
| 16 | + |
| 17 | +def redirect_output(func: Callable) -> Callable[..., dict[str, Any]]: |
| 18 | + """Decorator to redirect output of the function. |
| 19 | +
|
| 20 | + Args: |
| 21 | + func (function): Hides output of this function. |
| 22 | +
|
| 23 | + Raises: |
| 24 | + Exception: Incase the execution of function fails, it raises an exception. |
| 25 | +
|
| 26 | + Returns: |
| 27 | + object of the called function |
| 28 | + """ |
| 29 | + |
| 30 | + @functools.wraps(func) |
| 31 | + def wrapper(*args, **kwargs) -> dict[str, Any]: |
| 32 | + std_out = sys.stdout |
| 33 | + sys.stdout = buf = io.StringIO() |
| 34 | + try: |
| 35 | + value = func(*args, **kwargs) |
| 36 | + logger.info(buf.getvalue()) |
| 37 | + logger.info(value) |
| 38 | + except Exception as exp: |
| 39 | + logger.exception( |
| 40 | + "Error occurred while computing benchmark %s. Buffer: %s." "\n Method %s, args %s, kwargs %s", |
| 41 | + exp, |
| 42 | + buf.getvalue(), |
| 43 | + func, |
| 44 | + args, |
| 45 | + kwargs, |
| 46 | + ) |
| 47 | + value = {} |
| 48 | + sys.stdout = std_out |
| 49 | + return value |
| 50 | + |
| 51 | + return wrapper |
| 52 | + |
| 53 | + |
| 54 | +class Status(str, Enum): |
| 55 | + """Status of the benchmarking run.""" |
| 56 | + |
| 57 | + SUCCESS = "success" |
| 58 | + FAILED = "failed" |
| 59 | + |
| 60 | + |
| 61 | +class Result: |
| 62 | + def __init__(self, value: Any, status=Status.SUCCESS): |
| 63 | + self.value = value |
| 64 | + self.status = status |
| 65 | + |
| 66 | + def __bool__(self): |
| 67 | + return self.status == Status.SUCCESS |
| 68 | + |
| 69 | + |
| 70 | +def exception_wrapper(func: Callable) -> Callable[..., Result]: |
| 71 | + """Wrapper method to handle exceptions. |
| 72 | +
|
| 73 | + Args: |
| 74 | + func (function): Function to be wrapped. |
| 75 | +
|
| 76 | + Raises: |
| 77 | + Exception: Incase the execution of function fails, it raises an exception. |
| 78 | +
|
| 79 | + Example: |
| 80 | + >>> @exception_wrapper |
| 81 | + ... def func(): |
| 82 | + ... raise Exception("Exception occurred") |
| 83 | + >>> func() |
| 84 | + Exception: Exception occurred |
| 85 | +
|
| 86 | + Returns: |
| 87 | + object of the called function |
| 88 | + """ |
| 89 | + |
| 90 | + @functools.wraps(func) |
| 91 | + def wrapper(*args, **kwargs) -> Result: |
| 92 | + try: |
| 93 | + value = Result(value=func(*args, **kwargs)) |
| 94 | + except Exception as exp: |
| 95 | + logger.exception( |
| 96 | + "Error occurred while computing benchmark %s. Method %s, args %s, kwargs %s", |
| 97 | + exp, |
| 98 | + func, |
| 99 | + args, |
| 100 | + kwargs, |
| 101 | + ) |
| 102 | + value = Result(False, Status.FAILED) |
| 103 | + return value |
| 104 | + |
| 105 | + return wrapper |
0 commit comments