-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecorator_test.py
More file actions
118 lines (72 loc) · 2.12 KB
/
decorator_test.py
File metadata and controls
118 lines (72 loc) · 2.12 KB
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
92
# -*- coding:utf8 -*-
def check_is_admin(f):
def wrapper(*args, **kwargs):
if kwargs.get('username') != 'admin':
raise Exception("This user is not allowed to get food")
return f(*args, **kwargs)
return wrapper
class Storage(object):
@check_is_admin
def get_food(self, username, food):
return 'appled'
@check_is_admin
def put_food(self, username, food):
return '111'
s = Storage()
#s.get_food('hello', 'apple')
#print s.get_food('admin', 'admin')
def bread(func):
def wrapper():
print "AAAA"
func()
print "BBBB"
return wrapper
def sandwich(food="--ham--"):
print food
import functools
import inspect
def is_admin(func):
@functools.wraps(func)
def wrapper(*args, **kargs):
func_args = inspect.getcallargs(func, *args, **kargs)
if func_args.get('username') != 'admin':
raise Exception('ssss')
return func(*args, **kargs)
return wrapper
import signal
import time
def timeout(seconds, error_massge = 'Function call timed out...'):
def decorated(func):
def _handle_timeout(sigum, frame):
raise Exception(error_massge)
def wrapper(*args, **kwargs):
signal.signal(signal.SIGALRM, _handle_timeout)
signal.alarm(seconds)
try:
result = func(*args, **kwargs)
finally:
signal.alarm(0)
return result
return functools.wraps(func)(wrapper)
return decorated
@timeout(2, 'Function slow; aborted')
def slow_function():
time.sleep(5)
#slow_function()
import os
#from threading import Thread
from multiprocessing import Process as Thread
def get_data(num):
print ('sum to {0} with pid {1}'.format(num,os.getpid()))
def main():
data = [i for i in range(10)]
threads = []
for num in data:
thread = Thread(target = get_data, args=(num,))
thread.start()
threads.append(thread)
for thread in threads:
thread.join()
if __name__ == '__main__':
#main()
print map(lambda x:x*x, [i for i in range(10)])