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
class sleep: | |
def __init__(self, secs): | |
self.secs = secs | |
def __call__(self, func): | |
from functools import wraps | |
@wraps(func) | |
def wrapper(*args, **kwargs): | |
from time import sleep |
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
def has_permission(permission): | |
def decorator(func): | |
from functools import wraps | |
@wraps(func) | |
def wrapper(*args, **kwargs): | |
user = kwargs.get('user') | |
assert permission in user.permissions | |
result = 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
@benchmark | |
@has_permission('can_view_dashboard') | |
def view_dashboard(**kwargs): | |
user = kwargs.pop('user') | |
print('\n- DASHBOARD') | |
print('- Merhaba {username}, giriş başarılı.'.format( | |
username=user.name | |
)) |
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
@has_permission('can_view_dashboard') | |
def view_dashboard(**kwargs): | |
user = kwargs.pop('user') | |
print('\n- DASHBOARD') | |
print('- Merhaba {username}, giriş başarılı.'.format( | |
username=user.name | |
)) |
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
def has_permission(permission): | |
def decorator(func): | |
def wrapper(*args, **kwargs): | |
user = kwargs.get('user') | |
assert permission in user.permissions | |
result = func(*args, **kwargs) | |
return result | |
return wrapper |
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, render_template | |
app = Flask(__name__) | |
@app.route('/', methods=['GET']) | |
def home(): | |
return render_template('home.html') |
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
@benchmark | |
def test_response_status_is_200(): | |
from urllib import request | |
response = request.urlopen( | |
request.Request('https://httpbin.org/status/200', | |
method='HEAD') | |
) | |
assert response.status == 200 |
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
def benchmark(func): | |
def wrapper(*args, **kwargs): | |
from time import time | |
time_start = time() | |
result = func(*args, **kwargs) | |
time_finish = time() | |
time_delta = time_finish - time_start | |
print('- [{func_name}] fonksiyonu {seconds} saniye sürdü.'.format( |
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
def decorator(func): | |
def wrapper(*args, **kwargs): | |
print('Fonksiyon çalışacak...') | |
func() | |
return wrapper | |
@decorator | |
def func(): | |
print('Fonksiyon çalıştı.') |
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
# Yöntem 1: | |
@decorator | |
def func(): | |
pass | |
# Yöntem 2: | |
def func(): | |
pass | |
func = decorator(func) |