Skip to content

Commit d4c24b7

Browse files
authored
Adding first sample (#3)
* Adding first sample * fixing .gitignore
1 parent c4c5efc commit d4c24b7

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,4 +191,7 @@ cython_debug/
191191
# exclude from AI features like autocomplete and code analysis. Recommended for sensitive data
192192
# refer to https://docs.cursor.com/context/ignore-files
193193
.cursorignore
194-
.cursorindexingignore
194+
.cursorindexingignore
195+
196+
# PyCharm
197+
.idea

src/learning/decorating.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"""
2+
Decorators receive a function to be executed when it is called.
3+
However, some other action can be done before or after its execution.
4+
The idea is to encapsulate some behaviour that should happen on top of
5+
some operation and can be reused everywhere it is needed.
6+
"""
7+
from functools import wraps
8+
9+
def log_function(func):
10+
@wraps(func) # this ensures docstring, function name, arguments list, etc. are all copied
11+
# to the wrapped function - instead of being replaced with wrapper's info
12+
def wrapper(*args, **kwargs):
13+
print("Entering function {}".format(func.__name__))
14+
result = func(*args, **kwargs)
15+
print("Exiting function {}".format(func.__name__))
16+
return result
17+
return wrapper
18+
19+
@log_function
20+
def add(a, b):
21+
return a + b
22+
23+
@log_function
24+
def subtract(a, b):
25+
return a - b
26+
27+
@log_function
28+
def multiply(a, b):
29+
return a * b
30+
31+
@log_function
32+
def divide(a, b):
33+
return a / b
34+
35+
print("Adding two numbers: " + str(add(1, 2)))
36+
print("Subtracting two numbers: " + str(subtract(8, 3)))
37+
print("Multiplying two numbers: " + str(multiply(5, 6)))
38+
print("Dividing two numbers: " + str(divide(45, 5)))

0 commit comments

Comments
 (0)