diff --git a/students/bec_lukasz/lesson_14_decorators/Readme.md b/students/bec_lukasz/lesson_14_decorators/Readme.md new file mode 100644 index 000000000..03b2040b7 --- /dev/null +++ b/students/bec_lukasz/lesson_14_decorators/Readme.md @@ -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. diff --git a/students/bec_lukasz/lesson_14_decorators/access_required.py b/students/bec_lukasz/lesson_14_decorators/access_required.py new file mode 100644 index 000000000..232314ed5 --- /dev/null +++ b/students/bec_lukasz/lesson_14_decorators/access_required.py @@ -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() diff --git a/students/bec_lukasz/lesson_14_decorators/authorization.py b/students/bec_lukasz/lesson_14_decorators/authorization.py new file mode 100644 index 000000000..cd09dce7b --- /dev/null +++ b/students/bec_lukasz/lesson_14_decorators/authorization.py @@ -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 diff --git a/students/bec_lukasz/lesson_14_decorators/logged.py b/students/bec_lukasz/lesson_14_decorators/logged.py new file mode 100644 index 000000000..729d9ed0b --- /dev/null +++ b/students/bec_lukasz/lesson_14_decorators/logged.py @@ -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()) diff --git a/students/bec_lukasz/lesson_14_decorators/sort.py b/students/bec_lukasz/lesson_14_decorators/sort.py new file mode 100644 index 000000000..1451a848e --- /dev/null +++ b/students/bec_lukasz/lesson_14_decorators/sort.py @@ -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())