# Создание своего контекстного менеджера

class CustomManagerContext:
    def __enter__(self):
        print('Start manager context')
        return "This is value as ..."
    
    def __exit__(self, exc_type, exc_val, exc_tb):
        print('End manager context')
        print(exc_type, exc_val, exc_tb, sep=',')
        if isinstance(exc_val, ZeroDivisionError):
            print('Нельзя делить на ноль')
            return True
        # return True # без этой строки может быть исключение

class FileContext:
    def __init__(self, path, mode):
        self.path = path
        self.mode = mode

    def __enter__(self):
        print("Open file")
        self.file = open(self.path, self.mode)
        return self.file

    def __exit__(self, exc_type, exc_val, exc_tb):
        print("Close file")
        self.file.close()


def test_2():
    with FileContext('file.txt', 'r') as file:
        print(file.read())


def test_1():
    # with open('text.txt', 'r') as file:
    #     file.read()
    with CustomManagerContext() as cust:
        print(cust)
        print('hello')
        1/0


def main():
    # test_1()
    test_2()

if __name__ == '__main__':
    main()