Chapter folders renamed
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
# decorator1.py
|
||||
|
||||
def add_hello(myfunc):
|
||||
def _add_hello():
|
||||
print("inside add hello")
|
||||
myfunc()
|
||||
return _add_hello
|
||||
|
||||
@add_hello
|
||||
def hello_world():
|
||||
print("hello world")
|
||||
|
||||
hello_world()
|
||||
@@ -0,0 +1,18 @@
|
||||
# decorator1.py
|
||||
from datetime import datetime
|
||||
|
||||
def add_timestamps(myfunc):
|
||||
def _add_timestamps():
|
||||
print(datetime.now())
|
||||
myfunc()
|
||||
print(datetime.now())
|
||||
return _add_timestamps
|
||||
|
||||
@add_timestamps
|
||||
def hello_world():
|
||||
print("hello world")
|
||||
|
||||
hello_world()
|
||||
|
||||
#hello = add_timestamps(hello_world)
|
||||
#hello()
|
||||
@@ -0,0 +1,19 @@
|
||||
# decorator2.py
|
||||
from datetime import datetime
|
||||
from functools import wraps
|
||||
|
||||
def add_timestamps(myfunc):
|
||||
@wraps(myfunc)
|
||||
def _add_timestamps():
|
||||
print(datetime.now())
|
||||
myfunc()
|
||||
print(datetime.now())
|
||||
return _add_timestamps
|
||||
|
||||
@add_timestamps
|
||||
def hello_world():
|
||||
print("hello world")
|
||||
|
||||
hello_world()
|
||||
help(hello_world)
|
||||
print(hello_world)
|
||||
@@ -0,0 +1,16 @@
|
||||
# decorator3.py
|
||||
from functools import wraps
|
||||
|
||||
def power(func):
|
||||
@wraps(func)
|
||||
def inner_calc(*args, **kwargs):
|
||||
print("Decorating power")
|
||||
n = func(*args, **kwargs)
|
||||
return n
|
||||
return inner_calc
|
||||
|
||||
@power
|
||||
def power_base2(n):
|
||||
return 2**n
|
||||
|
||||
print(power_base2(3))
|
||||
@@ -0,0 +1,18 @@
|
||||
# decorator4.py
|
||||
from functools import wraps
|
||||
|
||||
def power_calc(base):
|
||||
def inner_decorator(func):
|
||||
@wraps(func)
|
||||
def inner_calc(*args, **kwargs):
|
||||
exponent = func(*args, **kwargs)
|
||||
return base**exponent
|
||||
return inner_calc
|
||||
return inner_decorator
|
||||
|
||||
@power_calc(base=3)
|
||||
def power_n(n):
|
||||
return n
|
||||
|
||||
print(power_n(2))
|
||||
print(power_n(4))
|
||||
@@ -0,0 +1,47 @@
|
||||
# decorator5.py
|
||||
from datetime import datetime
|
||||
from functools import wraps
|
||||
|
||||
def add_timestamp(func):
|
||||
@wraps(func)
|
||||
def inner_func(*args, **kwargs):
|
||||
res = "{}: {}\n".format(datetime.now(), func(*args, **kwargs))
|
||||
return res
|
||||
return inner_func
|
||||
|
||||
def file(func):
|
||||
@wraps(func)
|
||||
def inner_func(*args, **kwargs):
|
||||
res = func(*args, **kwargs)
|
||||
with open("log.txt", 'a') as file:
|
||||
file.write(res)
|
||||
return res
|
||||
return inner_func
|
||||
|
||||
def console(func):
|
||||
@wraps(func)
|
||||
def inner_func(*args, **kwargs):
|
||||
res = func(*args, **kwargs)
|
||||
print(res)
|
||||
return res
|
||||
return inner_func
|
||||
|
||||
@file
|
||||
@add_timestamp
|
||||
def log(msg):
|
||||
return msg
|
||||
|
||||
@file
|
||||
@console
|
||||
@add_timestamp
|
||||
def log1(msg):
|
||||
return msg
|
||||
|
||||
@console
|
||||
@add_timestamp
|
||||
def log2(msg):
|
||||
return msg
|
||||
|
||||
log("This is a test message for file only")
|
||||
log1("This is a test message for both file and console")
|
||||
log2("This message is for console only")
|
||||
@@ -0,0 +1,20 @@
|
||||
from functools import wraps
|
||||
|
||||
def makebold(fn):
|
||||
@wraps(fn)
|
||||
def wrapped(*args, **kwargs):
|
||||
return "<b>" + fn(*args, **kwargs) + "</b>"
|
||||
return wrapped
|
||||
|
||||
def makeitalic(fn):
|
||||
@wraps(fn)
|
||||
def wrapped(*args, **kwargs):
|
||||
return "<i>" + fn(*args, **kwargs) + "</i>"
|
||||
return wrapped
|
||||
|
||||
@makebold
|
||||
@makeitalic
|
||||
def say():
|
||||
return 'Hello'
|
||||
|
||||
print(say())
|
||||
@@ -0,0 +1,3 @@
|
||||
2021-03-13 12:53:00.545882: This is a test message for both file and console
|
||||
2021-03-13 12:53:21.075098: This is a test message for file only
|
||||
2021-03-13 12:53:21.075517: This is a test message for both file and console
|
||||
Reference in New Issue
Block a user