Chapter folders renamed

This commit is contained in:
Karan Solanki
2021-08-13 11:44:51 +05:30
parent d9f3f5b159
commit 1eb709f83a
211 changed files with 0 additions and 0 deletions
+13
View File
@@ -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()
+18
View File
@@ -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()
+19
View File
@@ -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)
+16
View File
@@ -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))
+18
View File
@@ -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))
+47
View File
@@ -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")
+20
View File
@@ -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())
+3
View File
@@ -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