-
Notifications
You must be signed in to change notification settings - Fork 0
/
contextmanager_call_decorator.py
91 lines (63 loc) · 1.95 KB
/
contextmanager_call_decorator.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import time
# 2018.12.28 edit by jun.hu
def get_last4_mobile_number_sha256(mobile_number):
"""
获取手机号最后四位的入库保存值
规则: 使用前三位手机号 MD5 值作为密钥 + 手机号后四位 作 hmac_sha256
hmac_sha256(md5(mobile_number[:3]), mobile_number[-4:])
:param:
* mobile_number: (string) 手机号
:return:
"""
time.sleep(1)
return mobile_number[:4]
class Timer(object):
def __init__(self, start=None):
self.start = start if start else time.time()
def __enter__(self):
return self
def __exit__(self, exc_type, exc_val, exc_tb):
self.stop = time.time()
self.cost = self.stop - self.start
return exc_type is None
with Timer() as ti:
get_last4_mobile_number_sha256('15312341234')
get_last4_mobile_number_sha256('15312341234')
print(ti.cost)
from contextlib import contextmanager
class Timer1(object):
def __init__(self, start=None):
self.start = start if start else time.time()
@contextmanager
def __call__(self, *args, **kwargs):
print('Timer1')
yield
self.cost = time.time() - self.start
t = Timer1()
with t() as t0:
get_last4_mobile_number_sha256('15312341234')
get_last4_mobile_number_sha256('15312341234')
print(t.cost)
@contextmanager
def timer(start=None):
start = start if start else time.time()
print('timer')
yield
print(time.time() - start)
with timer() as t1:
get_last4_mobile_number_sha256('15312341234')
get_last4_mobile_number_sha256('15312341234')
from functools import wraps
def dec_test(info):
def decorator(func):
@wraps(func)
def wrapper(*args, **kwargs):
print('start')
print('dec args is {}'.format(info))
func(*args, **kwargs)
return wrapper
return decorator
@dec_test('dec_test')
def func_1(a, b):
print('here is func, args is {}, {}'.format(a, b))
func_1(1, 2)