This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
rng = np.random.RandomState(0) | |
# Create a lot of numbers | |
nums = rng.random(10000000) | |
# Decorate np.sort with our time_it transformer | |
timed_sort = time_it(np.sort) | |
# Perform the sort with our time_it functionality | |
timed_sort(nums) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
def time_it(fun): | |
def wrapper(*args, **kwargs): | |
start = time.time() | |
res = fun(*args, **kwargs) | |
end = time.time() | |
print(f'Function took {end-start}s') | |
return res |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def call_three_times(fun): | |
def wrapper(*args, **kwargs): | |
fun(*args, **kwargs) | |
fun(*args, **kwargs) | |
res = fun(*args, **kwargs) | |
return res | |
return wrapper |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>> walkout('Dionisia') | |
Function `walkout` called | |
Bye Dionisia | |
Function `walkout` finished |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def debug_transformer(fun): | |
# Allow wrapper to receive arbitrary args | |
def wrapper(*args, **kwargs): | |
print(f'Function `{fun.__name__}` called') | |
# And pass it to the original function | |
res = fun(*args, **kwargs) | |
print(f'Function `{fun.__name__}` finished') | |
return res | |
return wrapper |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
TypeError: wrapper() takes 0 positional arguments but 1 was given |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@debug_transformer | |
def walkout(name): | |
print(f'Bye {name}') | |
walkout('Felicia') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>> bob = get_bob() | |
Function `get_bob` called | |
Function `get_bob` finished | |
>>> print(bob) | |
Bob |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def debug_transformer(fun): | |
def wrapper(): | |
print(f'Function `{fun.__name__}` called') | |
res = fun() # get reference to original return value | |
print(f'Function `{fun.__name__}` finished') | |
return res | |
return wrapper |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Function `get_bob` called | |
Function `get_bob` finished | |
None |