Предизвикателства > Безгрешен блок > Решения > Решението на Павел Петков

Резултати
1 точки от тестове
0 точки от учител

1 точки общо

2 успешни теста
0 неуспешни теста
Код

 1class ProtectedSection:
 2    def __init__(self, log=(), suppress=()):
 3        self.log = log
 4        self.suppress = suppress
 5        self.exception = None
 6
 7    def __enter__(self):
 8        self.exception = None  # reset state
 9        return self
10
11    def __exit__(self, exc_type, exc_value, traceback):
12        if exc_type is None:
13            return False
14
15        if exc_type in self.log:
16            self.exception = exc_value
17            return True
18        if exc_type in self.suppress:
19            return True
20        return False

..
----------------------------------------------------------------------
Ran 2 tests in 0.000s

OK

Дискусия
История

f1class ProtectedSection:f1class ProtectedSection:
2    def __init__(self, log=(), suppress=()):2    def __init__(self, log=(), suppress=()):
3        self.log = log3        self.log = log
4        self.suppress = suppress4        self.suppress = suppress
5        self.exception = None5        self.exception = None
66
7    def __enter__(self):7    def __enter__(self):
nn8        self.exception = None  # reset state
8        return self9        return self
910
10    def __exit__(self, exc_type, exc_value, traceback):11    def __exit__(self, exc_type, exc_value, traceback):
11        if exc_type is None:12        if exc_type is None:
12            return False13            return False
1314
14        if exc_type in self.log:15        if exc_type in self.log:
15            self.exception = exc_value16            self.exception = exc_value
16            return True17            return True
17        if exc_type in self.suppress:18        if exc_type in self.suppress:
t18            self.exception = Nonet
19            return True19            return True
20        return False20        return False
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op