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

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

1 точки общо

1 успешни теста
1 неуспешни теста
Код
Скрий всички коментари

 1class ProtectedSection:
 2    """
 3    A context manager for handling exceptions in two ways.
 4    The ways are logging and suppression.
 5    """
 6    def __init__(self, log=(), suppress=()):
 7        """Initialize the ProtectedSection which can accept logging and suppression parameters"""
 8        self._log = tuple(log)

Не е нужно да ги правиш кортежи. Те вече ще са. Няма да тестваме с невалиден инпут, но ако това е нещо, което искаш да ползваш извън курса - ок.

9 self._suppress = tuple(suppress) 10 self._exception = None 11 12 @property 13 def exception(self):

Мисля, че спокойно можеш да използваш публичен атрибут и да нямаш пропърти, но решението си е твое. И така е ок.

14 """Return last logged exception""" 15 return self._exception 16 17 def __enter__(self): 18 """Enter context manager and return self attribute""" 19 return self 20 21 def __exit__(self, exc_type, exc_val, exc_tb): 22 """Exit context manager and handle exceptions""" 23 if exc_type and isinstance(exc_val, self._log): 24 # if exc_type and issubclass(exc_type, self._log): 25 self._exception = exc_val 26 return True 27 elif exc_type and isinstance(exc_val, self._suppress): 28 return True 29 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.000s

FAILED (failures=1)

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

f1class ProtectedSection:f1class ProtectedSection:
2    """2    """
3    A context manager for handling exceptions in two ways.3    A context manager for handling exceptions in two ways.
4    The ways are logging and suppression.4    The ways are logging and suppression.
5    """5    """
6    def __init__(self, log=(), suppress=()):6    def __init__(self, log=(), suppress=()):
7        """Initialize the ProtectedSection which can accept logging and suppression parameters"""7        """Initialize the ProtectedSection which can accept logging and suppression parameters"""
8        self._log = tuple(log)8        self._log = tuple(log)
9        self._suppress = tuple(suppress)9        self._suppress = tuple(suppress)
10        self._exception = None10        self._exception = None
1111
12    @property12    @property
13    def exception(self):13    def exception(self):
14        """Return last logged exception"""14        """Return last logged exception"""
15        return self._exception15        return self._exception
1616
17    def __enter__(self):17    def __enter__(self):
t18        """Enters context manager and return self attribute"""t18        """Enter context manager and return self attribute"""
19        return self19        return self
2020
21    def __exit__(self, exc_type, exc_val, exc_tb):21    def __exit__(self, exc_type, exc_val, exc_tb):
22        """Exit context manager and handle exceptions"""22        """Exit context manager and handle exceptions"""
23        if exc_type and isinstance(exc_val, self._log):23        if exc_type and isinstance(exc_val, self._log):
24            #  if exc_type and issubclass(exc_type, self._log):24            #  if exc_type and issubclass(exc_type, self._log):
25            self._exception = exc_val25            self._exception = exc_val
26            return True26            return True
27        elif exc_type and isinstance(exc_val, self._suppress):27        elif exc_type and isinstance(exc_val, self._suppress):
28            return True28            return True
29        return False29        return False
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op