forked from kumaya/python-programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecorator.py
More file actions
126 lines (107 loc) · 3.38 KB
/
decorator.py
File metadata and controls
126 lines (107 loc) · 3.38 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# def myDecorator(some_func):
# def myOrigDeco(*args, **kwargs):
# print "enter function ", some_func.__name__
# count = 0
# while count <= 5:
# count += 1
# print "***************", count
# try:
# return some_func(*args, **kwargs)
# except Exception as e:
# if count < 5:
# import time
# time.sleep(2)
# continue
# raise e
# return myOrigDeco
# @myDecorator
# def sum(a, b):
# # return a+b
# raise Exception("ValueError")
# val = sum(3,4)
# print val
print "*"*80
from functools import wraps
import time
def retry(retries=0, delay=0):
def myDecorator(some_func):
@wraps(some_func)
def myOrigDeco(*args, **kwargs):
"""Original decorator function"""
# print "enter function ", some_func.__name__
count = 0
while count <= retries:
count += 1
# print "********************************************", count
try:
return some_func(*args, **kwargs)
except Exception as e:
if count <= retries:
print("Exception in " + some_func.__name__ + ". " +
"Retry counter: " + str(count))
time.sleep(delay)
continue
raise e
return myOrigDeco
return myDecorator
@retry(retries=3, delay=2)
def sum(a,b):
""" This method adds two number
@param a: Integer number 01
@param b: Integer number 02
@return a+b
"""
return a+b
val = sum(3,4)
print val, "\n", sum.__name__, "\n", sum.__doc__
print "*"*80
# def decoratorFunctionWithArguments(arg1, arg2, arg3):
# def wrap(f):
# print "Inside wrap()"
# def wrapped_f(*args):
# print "Inside wrapped_f()"
# print "Decorator arguments:", arg1, arg2, arg3
# f(*args)
# print "After f(*args)"
# return wrapped_f
# return wrap
# @decoratorFunctionWithArguments("hello", "world", 42)
# def sayHello(a1, a2, a3, a4):
# print 'sayHello arguments:', a1, a2, a3, a4
# print "After decoration"
# print "Preparing to call sayHello()"
# sayHello("say", "hello", "argument", "list")
# print "after first sayHello() call"
# sayHello("a", "different", "set of", "arguments")
# print "after second sayHello() call"
# Case 1: where we declare call to testFunc in __call__. Here deco obj is defined at call time itself
# class myDeco(object):
# def __init__(self, f):
# print "Inside __init__ of deco"
# self.f = f
# def __call__(self):
# print "Inside __call__ of deco"
# self.f()
# print "Exiting __call__"
# @myDeco
# def testFunc():
# print "Inside testFunc"
# print "Call the testFunc"
# testFunc()
# #case 2:
# print "%"*80
# class myDeco(object):
# def __init__(self, f):
# print "Inside __init__ of deco"
# self.f = f
# f()
# def __call__(self):
# print "Inside __call__ od deco"
# self.f()
# print "Exiting __call__"
# # Case 1: where we declare call to testFunc in __call__. Here deco obj is defined at call time itself
# @myDeco
# def testFunc():
# print "Inside testFunc"
# print "Call the testFunc"
# testFunc()