-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdecorators.py
More file actions
169 lines (127 loc) · 4.17 KB
/
decorators.py
File metadata and controls
169 lines (127 loc) · 4.17 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#############################################################
# Program : 6
#############################################################
# (1)
# def add_all(a1, a2, a3, a4):
# return a1+a2+a3+a4
# (1)
# vals = (1, 5, 7, 9)
# print(add_all(*vals))
# (2)
# vals = {'a1': 1, 'a2': 5, 'a3': 7, 'a4': 9}
# print(add_all(a1=vals['a1'], a2=vals['a2'], a3=vals['a3'], a4=vals['a4'],))
# (3)
def add_all(*args): # accept any number of single arguments
return(sum(args))
# print(add_all(1, 3, 7, 9))
def pretty_print(**kwargs): # accept any number of named arguments
for k, v in kwargs.items():
print(f'For {k} we have {v}.')
pretty_print(username='vasu123', access_level='admin')
#############################################################
# Program : 5
#############################################################
# import functools
# user = {'username': 'vasu123', 'access_level': 'user'}
# def user_has_permission(access_level):
# def user_has_permission(func):
# @functools.wraps(func)
# def secure_func(panel):
# if user.get('access_level') == access_level:
# return func(panel)
# return secure_func
# return user_has_permission
# @user_has_permission('admin')
# def my_function(panel):
# """
# Allowsus to retrieve the password for the admin panel.
# """
# return f'Password for {panel} panel is 1234.'
# print(my_function.__name__)
# print(my_function('movies'))
#############################################################
# Program : 4
#############################################################
# import functools
# user = {'username': 'vasu123', 'access_level': 'admin'}
# def user_has_permission(func):
# @functools.wraps(func)
# def secure_func(panel):
# if user.get('access_level') == 'admin':
# return func(panel)
# return secure_func
# @user_has_permission
# def my_function(panel):
# """
# Allowsus to retrieve the password for the admin panel.
# """
# return f'Password for {panel} panel is 1234.'
# print(my_function.__name__)
# print(my_function('movies'))
#############################################################
# Program : 3
#############################################################
# import functools
# user = {'username': 'vasu123', 'access_level': 'admin'}
# def user_has_permission(func):
# @functools.wraps(func)
# def secure_func():
# """
# Hey what's going on.
# """
# if user.get('access_level') == 'admin':
# return func()
# raise RuntimeError
# return secure_func
# @user_has_permission
# def my_function():
# """
# Allowsus to retrieve the password for the admin panel.
# """
# return 'Password for admin panel is 1234.'
# @user_has_permission
# def another():
# pass
# print(my_function())
# print(my_function.__name__)
# print(another.__name__)
#############################################################
# Program : 2
#############################################################
# user = {'username': 'vasu123', 'access_level': 'admin'}
# def user_has_permission(func):
# def secure_func():
# """
# Hey
# """
# if user.get('access_level') == 'admin':
# return func()
# raise RuntimeError
# return secure_func
# @user_has_permission
# def my_function():
# """
# Allowsus to retrieve the password for the admin panel.
# """
# return 'Password for admin panel is 1234.'
# @user_has_permission
# def another():
# pass
# print(my_function())
# print(my_function.__name__)
# # print(my_function.__name__)
# print(my_function.__doc__)
#############################################################
# Program : 1
#############################################################
# user = {'username': 'vasu123', 'access_level': 'admin'}
# def user_has_permission(func):
# def secure_func():
# if user.get('access_level') == 'admin':
# return func()
# raise RuntimeError
# return secure_func
# def my_function():
# return 'Password for admin panel is 1234.'
# my_secure_function= user_has_permission(my_function)
# print(my_secure_function())