Last active
October 31, 2015 03:15
-
-
Save patrickyoung/2ceba4e33af888f8333c to your computer and use it in GitHub Desktop.
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
from functools import wraps | |
import logging | |
user_id = "user" | |
people_db = [ | |
{'id': '001', 'name': 'Andrew M. Tatum'}, | |
{'id': '002', 'name': 'Frances G. Marzano'}, | |
{'id': '003', 'name': 'Tammy R. Green'},] | |
def add_tag(tag_name): | |
def add_tag_decorator(func): | |
@wraps(func) | |
def wrapper(*args, **kwargs): | |
return "<{0}>{1}</{0}>".format(tag_name, func(*args, **kwargs)) | |
return wrapper | |
return add_tag_decorator | |
def audit(func): | |
@wraps(func) | |
def wrapper(*args, **kwargs): | |
logging.critical("[{0}] Accessing: {1}".format(user_id, func.__name__)) | |
return func(*args, **kwargs) | |
return wrapper | |
def secured(func): | |
@wraps(func) | |
def wrapper(*args, **kwargs): | |
if user_id == 'user': | |
return func(*args, **kwargs) | |
else: | |
raise Exception('Not Authorized.') | |
return wrapper | |
@add_tag("p") | |
@audit | |
@secured | |
def find_person_by_id(id): | |
try: | |
return (person for person in people_db if person["id"] == id).next() | |
except StopIteration: | |
return None | |
print "" | |
print "Locates a user: {0}\n".format(find_person_by_id('001')) | |
print "Does not locate a user: {0}\n".format(find_person_by_id('004')) | |
try: | |
user_id = 'not_a_user' | |
print find_person_by_id('001') | |
except Exception: | |
print "Access denied :(" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment