File: utils.py

package info (click to toggle)
mpire 2.10.2-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,064 kB
  • sloc: python: 5,473; makefile: 209; javascript: 182
file content (26 lines) | stat: -rw-r--r-- 770 bytes parent folder | download
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
from typing import Callable


class ConditionalDecorator:

    def __init__(self, decorator: Callable, condition: bool) -> None:
        """
        Decorator which takes a decorator and a condition as input. Only when the condition is met the decorator is used

        :param decorator: Decorator
        :param condition: Condition (boolean)
        """
        self.decorator = decorator
        self.condition = condition

    def __call__(self, func) -> Callable:
        """
        Enables the conditional decorator

        :param func: Function to decorated
        :return: Decorated function if condition is met, otherwise just the function
        """
        if self.condition:
            return self.decorator(func)
        else:
            return func