Skip to content

Commit 24615f1

Browse files
Added the practice code files
1 parent 7147ac3 commit 24615f1

5 files changed

Lines changed: 67 additions & 0 deletions

File tree

Input_output_logger.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import functools
2+
3+
def log_input_output(func):
4+
@functools.wraps(func)
5+
def wrapper(*args, **kwargs):
6+
print(f"Input : args={args}, kwargs={kwargs}")
7+
result = func(*args, **kwargs)
8+
9+
print(f"Output: {result}")
10+
return result
11+
return wrapper
12+
@log_input_output
13+
def multiply(x, y):
14+
return x * y
15+
16+
result = multiply(2, 3)
17+
print(result)

decorator_chaining_example.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
def spanish(func):
2+
def wrapper():
3+
return "<spanish>" + func() + "<spanish>"
4+
return wrapper
5+
6+
def german(func):
7+
def wrapper():
8+
return "<german>" + func() + "<german>"
9+
return wrapper
10+
11+
@spanish
12+
@german
13+
def greet():
14+
return "Hello, Good Afternoon!"
15+
16+
print(greet())

extend_existing_func.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
def addone(func):
2+
def wrapper(x):
3+
return func(x) + 1
4+
return wrapper
5+
def subfour(func):
6+
def wrapper(x):
7+
return func(x) - 4
8+
return wrapper
9+
10+
@addone
11+
def multiply(y):
12+
return y * 2
13+
14+
@subfour
15+
def divide(a):
16+
return a / 10
17+
18+
print(multiply(2))
19+
print(divide(100))

resource_context_manager.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class contextManager():
2+
def __init__(self):
3+
print('init method called')
4+
5+
def __enter__(self):
6+
print('enter method called')
7+
return self
8+
9+
def __exit__(self, exc_type, exc_value, exc_traceback):
10+
print('exit method called')
11+
12+
13+
with contextManager() as manager:
14+
print('with statement block')

testfile.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
I love python

0 commit comments

Comments
 (0)