Created
April 11, 2017 02:30
-
-
Save jwz-ecust/687a6902802a1a854cd8cab696298237 to your computer and use it in GitHub Desktop.
decorator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class zjw(): | |
def __init__(self): | |
self.name = 1 | |
@classmethod | |
def classmethod(cls, arg): | |
print dir(cls) | |
print cls | |
print dir(arg) | |
print arg | |
def cbb(self, a): | |
print a | |
# fuck = zjw() | |
# # zjw.classmethod(fuck, 1) | |
# fuck.classmethod(10) | |
# | |
# zjw.cbb(fuck, 190) | |
import time | |
from functools import wraps | |
def log(func): | |
@wraps(func) | |
def wrapper(): | |
print "funciton is running" | |
# print func.__name__ | |
ts = time.time() | |
result = func() | |
te = time.time() | |
print "function = {}\n return = {}\n tiem = {:.10f}".\ | |
format(func.__name__, result, te - ts) | |
return wrapper | |
@log | |
def sum_non_para_non_para(): | |
x = 1231241412 | |
y = 12309317498 | |
return x * y | |
# sum_non_para_non_para() | |
# print sum_non_para_non_para.__name__ | |
def log2(name): | |
def decora(func): | |
@wraps(func) | |
def wrapper(): | |
print "name : %s" % name | |
print "function is running" | |
ts = time.time() | |
result = func() | |
te = time.time() | |
print "function = {}\n return = {}\n tiem = {:.10f}".\ | |
format(func.__name__, result, te - ts) | |
return wrapper | |
return decora | |
@log2("zhangjiawei") | |
def sum_non_para_para(): | |
x = 1231241412 | |
y = 12309317498 | |
return x * y | |
# sum_non_para_para() | |
def log3(name): | |
def decora(func): | |
@wraps(func) | |
def wrapper(*key): | |
# print "name : ", name | |
# print "funciton is running" | |
ts = time.time() | |
result = func(*key) | |
te = time.time() | |
print "function = {}\n return = {}\n tiem = {:.10f}".\ | |
format(func.__name__, result, te - ts) | |
return wrapper | |
return decora | |
@log3("chenbeibei") | |
def sum_para_para(a, b): | |
return a + b | |
# sum_para_para(10101, 12312) | |
def div(cla): | |
def decara(func): | |
@wraps(func) | |
def wrapper(*arg, **kwargs): | |
return "<div class= %s > %s </div>" % (cla, func()) | |
return wrapper | |
return decara | |
def h1(cla): | |
def decara(func): | |
@wraps(func) | |
def wrapper(*arg, **kwargs): | |
return "<h1 class= %s > %s </h1>" % (cla, func()) | |
return wrapper | |
return decara | |
@div("divclass") | |
@h1("h1class") | |
def hello(): | |
return "hello world" | |
# print hello() | |
class myapp(): | |
def __init__(self): | |
self.func_map = {} | |
def register(self, name): | |
def func_wapper(func): | |
self.func_map[name] = func | |
return func | |
return func_wapper | |
def call_method(self, name=None): | |
func = self.func_map.get(name, None) | |
if func is None: | |
raise Exception("No function registered against - ") + str(name) | |
return func() | |
app = myapp() | |
@app.register("/") | |
def main_page_func(): | |
return "This is the main page." | |
@app.register("/next_page") | |
def next_page_func(): | |
return "This is the next page." | |
print app.call_method("/") | |
print app.call_method("/next_page") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment