Skip to content

Commit 1e1d94a

Browse files
committed
YT Decorators
1 parent 2b0daf4 commit 1e1d94a

6 files changed

Lines changed: 84 additions & 0 deletions

File tree

006_YT/README.md

Whitespace-only changes.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"""
2+
Why we need a decorators?
3+
4+
- One functions one task.
5+
- It reduce the code reusability.
6+
"""
7+
8+
import time
9+
10+
from rich.pretty import pprint
11+
12+
13+
def cooking_work():
14+
start_time = time.time()
15+
16+
pprint("Cooking started ...")
17+
time.sleep(1)
18+
pprint("Cooking finished ...")
19+
20+
end_time = time.time()
21+
22+
pprint(f"Difference: {end_time - start_time}")
23+
24+
25+
cooking_work()
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import time
2+
3+
from rich.pretty import pprint
4+
5+
6+
def time_diff(base_fn):
7+
def enhanced_fn():
8+
start_time = time.time()
9+
result = base_fn()
10+
end_time = time.time()
11+
pprint(f"Difference: {end_time - start_time}")
12+
return result
13+
14+
return enhanced_fn
15+
16+
17+
@time_diff
18+
def cooking_work():
19+
pprint("Cooking started ...")
20+
time.sleep(1)
21+
pprint("Cooking finished ...")
22+
return "Done!"
23+
24+
25+
# cooking_work = time_diff(cooking_work)
26+
# pprint(cooking_work())
27+
28+
pprint(cooking_work())
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"""
2+
Add *args, **kwargs
3+
"""
4+
5+
import time
6+
7+
from rich.pretty import pprint
8+
9+
10+
def time_diff(base_fn):
11+
def enhanced_fn(*args, **kwargs):
12+
start_time = time.time()
13+
result = base_fn(*args, **kwargs)
14+
end_time = time.time()
15+
pprint(f"Difference: {end_time - start_time}")
16+
return result
17+
18+
return enhanced_fn
19+
20+
21+
@time_diff
22+
def cooking_work(what: str):
23+
pprint(f"{what} Cooking started ...")
24+
time.sleep(1)
25+
pprint("Cooking finished ...")
26+
27+
28+
# cooking_work = time_diff(cooking_work)
29+
# pprint(cooking_work())
30+
31+
pprint(cooking_work(what="Rich"))

006_YT/decorators-notes/README.md

Whitespace-only changes.
74.5 KB
Loading

0 commit comments

Comments
 (0)