Created
September 10, 2021 18:07
-
-
Save hodunov/a25d6fbe7aec55a46398b0f1bd39f956 to your computer and use it in GitHub Desktop.
lesson 19.py
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
# map | |
import operator | |
from functools import reduce | |
item = [1, 2, 3, 4] | |
squared = [] | |
for i in item: | |
squared.append(i ** 2) | |
def square_func(i): | |
return i ** 2 | |
# print(sum([1, 2, 3, 4])) | |
# print(list(map(operator.add, [1, 2, 3, 4], [1, 2, 3, 4]))) | |
# result = list(map(square_func, item)) | |
# print(result) | |
def x_plus_y(x, y): | |
return x + y | |
volume = reduce( | |
lambda x, y: x + y, | |
[1, 2, 3, 4] | |
) | |
# print(volume) | |
# print(f"{volume} - sum of digints") | |
# print("{volume} - sum of digints".format(volume=volume)) | |
# | |
areas = [3.123456, 3.123456, 3.123456, 3.123456, 3.123456, 3.123456] | |
# result = list(map(round, areas, range(1, 5))) | |
# print(result) | |
# CRUD | |
# create, read, update, delete | |
# zip | |
first_string = ['a', 'b', 'c', 'd'] | |
second_string = [1, 2, 3] | |
zip_value = list(zip(first_string, second_string)) | |
# print(zip_value) | |
# print(list(map(lambda x, y: (x, y), first_string, second_string))) | |
def bread(func): | |
def wrapper(): | |
print("BREAD") | |
func() | |
print("BREAD") | |
return wrapper | |
def vegetables(func): | |
def wrapper(): | |
print("SALAD") | |
func() | |
print("TOMATO") | |
return wrapper | |
@bread | |
@vegetables | |
def sandwich(food='cheese'): | |
print(food) | |
# sandwich = bread(vegetables(sandwich)) | |
# sandwich() | |
class MyCustomError(Exception): | |
def __init__(self, message): | |
self.message = message | |
def __str__(self): | |
return self.message | |
print(MyCustomError("MY ERROR TEXT")) | |
def error_catcher(error=None): | |
def decorator(func): | |
def wrapper(*args, **kwargs): | |
try: | |
func(*args, **kwargs) | |
except error as e: | |
with open('errors.txt', 'a') as file: | |
file.write( | |
f"While running {func.__name__} with arg {args} kwargs {kwargs}" | |
f"got an error {e}\n" | |
) | |
return wrapper | |
return decorator | |
@error_catcher(error=ZeroDivisionError) | |
def zero(): | |
1/0 | |
# zero() | |
@error_catcher(error=ValueError) | |
def converter(type_to_convert=int, value=None): | |
return type_to_convert(value) | |
converter(value='abc') | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment