-
Notifications
You must be signed in to change notification settings - Fork 72
Add final lesson tasks #705
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| ### Lesson 14 - Decorators | ||
| #### introduction | ||
|
|
||
| - [A guide to Python's function decorators](https://www.thecodeship.com/patterns/guide-to-python-function-decorators/) | ||
| - [Easy introduction into decorators](https://www.python-course.eu/python3_decorators.php) | ||
|
|
||
| #### practice projects | ||
|
|
||
| 1. Write `@sort` decorator that when applied to a function that returns a list, sorts this list, so we can do this: | ||
| ``` | ||
| @sort | ||
| def data_feeder(): | ||
| return [4,2,1,3] | ||
|
|
||
| data_feeder() == [1,2,3,4] # <- this is True | ||
| ``` | ||
|
|
||
| 2. Write `@access_required` decorator that when applied to a function executes subjected function only if `has_access` function from `authorization.py` returns `True`. So we can do this: | ||
| ``` | ||
| @access_required | ||
| def restricted_print(*args, **kwargs): | ||
| print(*args, **kwargs) | ||
|
|
||
| restricted_print('1 - visible') | ||
| restricted_print('2 - invisible') | ||
| restricted_print('3 - invisible') | ||
| restricted_print('4 - visible') | ||
| ``` | ||
|
|
||
| 3. Write `@logged` decorator using `logger_wrapper` from lesson 6. Apply it to several functions to demonstrate that it works. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| from lesson_14.authorization import has_access | ||
|
|
||
|
|
||
| def access_required(func): | ||
| def func_wrapper(args): | ||
| if has_access(): | ||
| return func(args) | ||
| return func_wrapper | ||
|
|
||
|
|
||
| @access_required | ||
| def restricted_print(*args, **kwargs): | ||
| print(*args, **kwargs) | ||
|
|
||
|
|
||
| def main(): | ||
| restricted_print('1 - visible') | ||
| restricted_print('2 - invisible') | ||
| restricted_print('3 - invisible') | ||
| restricted_print('4 - visible') | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| main() |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| access_count = -1 | ||
|
|
||
|
|
||
| def has_access(): | ||
| ''' | ||
| let's pretend this function calls some authorization services | ||
| returns True if access is granted and False if not | ||
| access is granted every 3rd time | ||
| ''' | ||
| global access_count | ||
| access_count += 1 | ||
| if access_count >= 3: | ||
| access_count = 0 | ||
| return access_count == 0 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| def get_text(name): | ||
| return "lorem ipsum, " + name + " dolor sit amet" | ||
|
|
||
|
|
||
| def p_decorate(func): | ||
| def func_wrapper(name): | ||
|
||
| return "<p>" + func(name) + "</p>" | ||
|
||
| return func_wrapper | ||
|
||
|
|
||
|
|
||
| my_get_text = p_decorate(get_text) | ||
|
|
||
| print(my_get_text("John")) | ||
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| from lesson_06.logger_wrapper import logger_wrapper | ||
|
|
||
|
|
||
| def logged(func): | ||
| def func_wrapper(*args, **kwargs): | ||
| return logger_wrapper(func, *args, **kwargs) | ||
| return func_wrapper | ||
|
|
||
|
|
||
| @logged | ||
| def first_function(*args, **kwargs): | ||
| return str(args), str(kwargs) | ||
|
|
||
|
|
||
| @logged | ||
| def second_function(): | ||
| return None | ||
|
|
||
|
|
||
| print(first_function(1, 2, osiem=8)) | ||
| print(second_function()) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| def sort(func): | ||
| def list_sort(): | ||
| return sorted(func()) | ||
| return list_sort | ||
|
|
||
|
|
||
| @sort | ||
| def data_feeder(): | ||
| return [4, 2, 1, 3] | ||
|
|
||
|
|
||
| print(data_feeder()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
E111 indentation is not a multiple of four