-
Notifications
You must be signed in to change notification settings - Fork 0
/
extensions.py
45 lines (28 loc) · 918 Bytes
/
extensions.py
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
import functools
import sys
from settings import LOGS_ENABLED
FILLER = '\n' + '-' * 64 + '\n'
class Beautify:
def __init__(self, func):
functools.update_wrapper(self, func)
self.func = func
self.calls = 0
def __call__(self, *args, **kwargs):
self.calls += 1
# Header
if LOGS_ENABLED:
header = f'{self.func.__name__}'
if self.calls > 1:
header = header + f'({self.calls})'
sys.stdout.write(FILLER)
sys.stdout.write(f'|{header:^62}|')
sys.stdout.write(FILLER)
result = self.func(*args, **kwargs)
# Footer
if LOGS_ENABLED:
sys.stdout.write('DONE')
sys.stdout.write('\n')
return result
def __get__(self, obj, objtype):
"""Implement 'get' descriptor"""
return functools.partial(self.__call__, obj)