This file contains hidden or 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 functools | |
def do_ten_times(func): | |
"""Repeats func ten times""" | |
@functools.wraps(func) | |
def wrapper(*args, **kwargs): | |
for i in range(10): | |
func(*args, **kwargs) |
This file contains hidden or 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 functools | |
def do_ten_times(func): | |
"""Repeats func ten times""" | |
@functools.wraps(func) | |
def wrapper(*args, **kwargs): | |
for i in range(10): | |
func(*args, **kwargs) |
This file contains hidden or 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 functools | |
import time | |
import random | |
def timer(func): | |
"""Returns the time it takes for some function to execute""" | |
@functools.wraps(func) | |
def wrapper(*args, **kwargs): |
This file contains hidden or 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 random | |
PLUGINS = dict() | |
def register(func): | |
PLUGINS[func.__name__] = func | |
return func | |
This file contains hidden or 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
from flask import Flask, g, request, redirect, url_for | |
import functools | |
app = Flask(__name__) | |
def login_required(func): | |
"""Make sure user is logged in before proceeding""" | |
@functools.wraps(func) | |
def wrapper_login_required(*args, **kwargs): | |
if g.user is None: | |
return redirect(url_for("login", next=request.url)) |
This file contains hidden or 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 math | |
class Vector: | |
def __init__(self, x, y): | |
self.__x = x | |
self.__y = y | |
def __str__(self): | |
return f"<{self.__x}, {self.__y}>" |
This file contains hidden or 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 functools | |
import time | |
import random | |
def do_ten_times(func): | |
"""Repeats func ten times""" | |
@functools.wraps(func) | |
def wrapper(*args, **kwargs): |
This file contains hidden or 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
wasting_operations = timer(wasting_operations) | |
wasting_operations = do_ten_times(wasting_operations) |
This file contains hidden or 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 functools | |
def repeats(num_reps=2): | |
def decorator(func): | |
@functools.wraps(func) | |
def wrapper(*args, **kwargs): | |
for _ in range(num_reps): | |
value = func(*args, **kwargs) | |
return value |
This file contains hidden or 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 functools | |
def count_calls(func): | |
@functools.wraps(func) | |
def wrapper(*args, **kwargs): | |
wrapper.num_calls += 1 | |
return func(*args, **kwargs) | |
wrapper.num_calls = 0 |