gist decorator in class method with

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
def func_time(log=None, tag=None):
    def deco(old_handler):
        @wraps(old_handler)
        def new_handler(*args, **kwargs):
            if not config.debug:
                return old_handler(*args, **kwargs)
            start = time.time()
            result = old_handler(*args, **kwargs)
            end = time.time()
            msg = "Total time running [%s]: %s seconds" % (
                old_handler.__name__ if tag is None else tag, str(end - start))
            if log:
                log(msg)
            else:
                print(msg)
            return result

        return new_handler

    return deco

decorator in class method

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
class retry(object):

    def __init__(self, times: int):
        self.times = times

    def __call__(self, func):
        def new_handler(*args, **kwargs):
            if self.times == -1:
                while True:
                    try:
                        is_ok = func(*args, **kwargs)
                        if is_ok:
                            break
                    except Exception:
                        pass
            elif self.times > 0:
                for i in range(self.times):
                    try:
                        is_ok = func(*args, **kwargs)
                        if is_ok:
                            break
                    except Exception:
                        pass
        new_handler.__doc__ = func.__doc__
        return new_handler

class Test(object):

    def _decorator(foo):
        def magic(self):
            print("start magic")
            foo(self)
            print("end magic")

        return magic

    @retry(times=3)
    def bar(self):
        print("normal call")
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
class Test(object):

    def retry(times=3):
        """
        times == -1 forever
        :return:
        """

        def deco(func):
            def new_handler(*args, **kwargs):
                retry_time = 1

                if times == -1:
                    while True:
                        try:
                            is_ok = func(*args, **kwargs)
                            if is_ok:
                                break
                            print(f'retry[{retry_time}]:{Duration.get_time(args[2]).to_str()}')
                            retry_time +=1
                        except Exception:
                            pass
                elif times > 0:
                    for i in range(times):
                        try:
                            is_ok = func(*args, **kwargs)
                            if is_ok:
                                break
                            print(f'retry[{retry_time}]:{Duration.get_time(args[2]).to_str()}')
                            retry_time += 1
                        except Exception:
                            pass

            return new_handler

        return deco

    @retry(times=-1)
    def download_img(self, station_num: int, time: float, rewrite=False):
        path = f'afreecatv/{station_num}/{Duration.get_time(time).to_str()}.jpg'
        if not rewrite and os.path.exists(path):
            return True
        param = self.get_thumbnail_param(station_num, time)
        is_ok = util.down_img(self.THUMBNAIL_URL, param, path)
        if not is_ok:
            print(path)
        return is_ok

with

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@contextmanager
def with_func_time(tag, log=None, is_log=False):
    if config.debug or is_log:
        start = time.time()
    try:
        yield
    finally:
        if config.debug or is_log:
            end = time.time()
            msg = "Total time running %s: %s seconds" % (tag, str(end - start))
            if log:
                log(msg)
                return
            print(msg)
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
@contextmanager
def transaction(conn):
    """
    Automatic handle transaction COMMIT/ROLLBACK. You MUST call trans.finish(),
    if you want to COMMIT; Otherwise(not call or exception occurs), ROLLBACK.

    >>> with transaction(conn) as trans:
    >>>     do something...
    >>>     if xxxxx:
    >>>         # if you don't want to commit, you just not call trans.finish().
    >>>         return error_page("xxxxxx")
    >>>     # if you want to commit, you call:
    >>>     trans.finish()

    @param conn: database connection
    """
    trans = Job()
    conn.begin()

    try:
        yield trans
    except:
        conn.rollback()
        raise

    if trans.is_finished():
        conn.commit()
    else:
        conn.rollback()