Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions students/bec_lukasz/lesson_14_decorators/Readme.md
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.
24 changes: 24 additions & 0 deletions students/bec_lukasz/lesson_14_decorators/access_required.py
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()
14 changes: 14 additions & 0 deletions students/bec_lukasz/lesson_14_decorators/authorization.py
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
13 changes: 13 additions & 0 deletions students/bec_lukasz/lesson_14_decorators/decorator_training.py
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"
Copy link
Contributor

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



def p_decorate(func):
def func_wrapper(name):
Copy link
Contributor

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

return "<p>" + func(name) + "</p>"
Copy link
Contributor

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

return func_wrapper
Copy link
Contributor

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



my_get_text = p_decorate(get_text)

print(my_get_text("John"))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

W292 no newline at end of file

21 changes: 21 additions & 0 deletions students/bec_lukasz/lesson_14_decorators/logged.py
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())
12 changes: 12 additions & 0 deletions students/bec_lukasz/lesson_14_decorators/sort.py
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())