2024-11-08 20:13:54 +00:00
|
|
|
"""
|
|
|
|
Now that we have covered the basics of a function and variables, we will quickly cover decorators.
|
|
|
|
Decorators are extremely useful and are used to wrap existing functions in another function, you can use this
|
|
|
|
for many different things, a good example is permission checks.
|
|
|
|
|
|
|
|
Key Concepts:
|
|
|
|
**wrappers** are used to nest functions in another generalized function, this can be used for various things e.g (logging info)
|
|
|
|
**args** we use the "*args* paramater to capture all positional based arguements, these come before keyword arguements.
|
|
|
|
it sounds more complicated than it is, e.g
|
|
|
|
myfunction(arg1, arg2, arg3)
|
|
|
|
**kwargs** are arguements defined by a key rather than their position. e.g
|
|
|
|
myfunction(keyword1="foo", keyword2="bar")
|
|
|
|
**__name__** is just another magic/dunder method that returns the function's name.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import time
|
2024-11-08 20:47:03 +00:00
|
|
|
# Initial function takes in another function as a arguement
|
|
|
|
def timed(function: any) -> any:
|
|
|
|
|
|
|
|
# The wrapper, takes in the arguements and keyword arguements from the function
|
|
|
|
def wrapper(*args: any, **kwargs: any) -> any:
|
2024-11-08 20:13:54 +00:00
|
|
|
before = time.time()
|
|
|
|
output = function(*args, **kwargs)
|
|
|
|
after = time.time()
|
|
|
|
print(f"{function.__name__} with output of {output} took {after-before}s to execute.")
|
2024-11-08 20:47:03 +00:00
|
|
|
# Ensure the output of the function is passed back
|
|
|
|
return output
|
2024-11-08 20:13:54 +00:00
|
|
|
|
|
|
|
return wrapper
|
|
|
|
|
2024-11-08 20:47:03 +00:00
|
|
|
# Python uses @ to use a wrapper on a function
|
2024-11-08 20:13:54 +00:00
|
|
|
@timed
|
2024-11-08 20:47:03 +00:00
|
|
|
# O(n^2) time complexity due to nested loops
|
|
|
|
def exponential_function(n: int) -> int:
|
2024-11-08 20:13:54 +00:00
|
|
|
output = 0
|
|
|
|
for i in range(n):
|
|
|
|
for j in range(i):
|
|
|
|
output += j
|
|
|
|
return output
|
|
|
|
|
|
|
|
# Test the decorated function
|
2024-11-08 20:47:03 +00:00
|
|
|
|
|
|
|
# This will take some time to compute
|
|
|
|
exponential_function(1000)
|
|
|
|
|
|
|
|
# This will take noticeably more time to compute
|
|
|
|
exponential_function(10000)
|
2024-11-08 20:13:54 +00:00
|
|
|
|