Best Python code snippet using avocado_python
decorators.py
Source:decorators.py  
...13# Author: Amador Pahim <apahim@redhat.com>14import types15from functools import wraps16from . import exceptions as core_exceptions17def deco_factory(behavior, signal):18    """19    Decorator factory.20    Returns a decorator used to signal the test when specified exception is21    raised.22    :param behavior: expected test result behavior.23    :param signal: delegating exception.24    """25    def signal_on(exceptions=None):26        """27        {0} the test when decorated function produces exception of the28        specified type.29        :param exceptions: Tuple or single exception to be assumed as30                           test {1} [Exception].31        :note: self.error, self.cancel and self.fail remain intact.32        :note: to allow simple usage param 'exceptions' must not be callable.33        """34        func = False35        if exceptions is None:36            exceptions = Exception37        elif isinstance(exceptions, types.FunctionType):38            func = exceptions39            exceptions = Exception40        def decorate(func):41            @wraps(func)42            def wrapper(*args, **kwargs):43                try:44                    return func(*args, **kwargs)45                except core_exceptions.TestBaseException as exc:46                    raise exc47                except exceptions as details:48                    raise signal(repr(details)) from details49            return wrapper50        return decorate(func) if func else decorate51    name = "{behavior}_on".format(behavior=behavior)52    signal_on.__name__ = signal_on.__qualname__ = name53    signal_on.__doc__ = signal_on.__doc__.format(behavior.capitalize(),54                                                 behavior.upper())55    return signal_on56fail_on = deco_factory("fail", core_exceptions.TestFail)57cancel_on = deco_factory("cancel", core_exceptions.TestCancel)58def skip(message=None):59    """60    Decorator to skip a test.61    """62    def decorator(obj):63        def method_decorator(function):64            if not isinstance(function, type):65                @wraps(function)66                def wrapper(*args, **kwargs):  # pylint: disable=W061367                    raise core_exceptions.TestSkipError(message)68                function = wrapper69            function.__skip_test_decorator__ = True70            return function71        def class_decorator(cls):...deco.py
Source:deco.py  
...12    '''13    Decorate a decorator so that it inherits the docstrings14    and stuff from the function it's decorating.15    '''16    def deco_factory(called_func):17        def decorator_wrapper(*args, **kwargs):18            return called_func(*args, **kwargs)19        return update_wrapper(decorator_wrapper, original_func)20    return deco_factory21def countcalls(fn):22    '''Decorator that counts calls made to the function decorated.'''23    def countcalls_wrapper(*args, **kwargs):24        # f = globals()[fn.func_name]25        # f.calls += 126        countcalls_wrapper.calls = getattr(countcalls_wrapper, 'calls', 0) + 127        return fn(*args, **kwargs)28    # fn.calls = 029    return update_wrapper(countcalls_wrapper, fn)30def memo(fn):...log.py
Source:log.py  
1from PyQt5 import QtCore2import time3def deco_factory(prefix, suffix):4    def deco(s):5        return prefix + s + suffix6    return deco7error_deco = deco_factory('\033[31m', '\033[0m')8warning_deco = deco_factory('\033[33m', '\033[0m')9info_deco = deco_factory('\033[1m', '\033[0m')10class Logger(QtCore.QObject):11    sig_output = QtCore.pyqtSignal(str)12    ERROR_LEVEL = 313    WARN_LEVEL  = 214    INFO_LEVEL  = 115    DEBUG_LEVEL = 016    def __init__(self, sender, profiles):17        super(Logger, self).__init__()18        self.sender = sender19        self.profiles = profiles20    def log(self, message, level, ending='\n'):21        if not isinstance(message, str):22            message = str(message)23        for i, profile in enumerate(self.profiles):...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
