Skip to content

Instantly share code, notes, and snippets.

@kurzweil777
Created June 21, 2020 16:34
Show Gist options
  • Save kurzweil777/955da44480bfc61dce2e6291b0a6d877 to your computer and use it in GitHub Desktop.
Save kurzweil777/955da44480bfc61dce2e6291b0a6d877 to your computer and use it in GitHub Desktop.
Exercise from Py_Course
# Create an @authenticated decorator that only allows the function to run is user1 has 'valid' set to True:
user1 = {
'name': 'Sorna',
'valid': True # changing this will either run or not run the message_friends function.
}
def authenticated(fn):
def check(*args, **kwargs):
if dict(*args).get('valid'):
return fn(*args, **kwargs)
else:
print(f'Authorization for {dict(*args).get("name")} is not valid')
return check
@authenticated
def message_friends(user):
print('message has been sent')
message_friends(user1) # message has been sent
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment