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

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

1 точки общо

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

 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        return self
 9
10    def __exit__(self, exc_type, exc_val, exc_tb):
11        if exc_type in self.log:
12            self.exception = exc_val
13            return self
14        elif exc_type in self.suppress:
15            return self
16        return False

.F
======================================================================
FAIL: test_special_cases (test.TestSolution.test_special_cases)
Test special cases to show you that you missed something.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 128, in test_special_cases
self.assertIsNone(cm.exception)
AssertionError: ZeroDivisionError('division by zero') is not None

----------------------------------------------------------------------
Ran 2 tests in 0.001s

FAILED (failures=1)

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

f1class ProtectedSection:f1class ProtectedSection:
n2    def __init__(self, log = None, suppress = None):n2    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):
8        return self8        return self
99
10    def __exit__(self, exc_type, exc_val, exc_tb):10    def __exit__(self, exc_type, exc_val, exc_tb):
n11        if self.log is not None and exc_type in self.log:n11        if exc_type in self.log:
12            self.exception = exc_val12            self.exception = exc_val
13            return self13            return self
n14        elif self.suppress is not None and exc_type in self.suppress:n14        elif exc_type in self.suppress:
15            return self15            return self
16        return False16        return False
tt17 
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op