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

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

1 точки общо

1 успешни теста
1 неуспешни теста
Код (@property for protected exception)

 1class ProtectedSection:
 2    """Context manager that suppresses or logs exceptions based on provided exception types."""
 3
 4    def __init__(self, log=(), suppress=()):
 5        """
 6        Initialize the ProtectedSection context manager.
 7
 8        Args:
 9            log (tuple): Tuple of exception types to log. Defaults to an empty tuple.
10            suppress (tuple): Tuple of exception types to suppress. Defaults to an empty tuple.
11        """
12        self._exception = None
13        self._log_exceptions = tuple(
14            filter(lambda exc_type: issubclass(exc_type, Exception), log)
15        )
16        self._suppress_exceptions = tuple(
17            filter(lambda exc_type: issubclass(exc_type, Exception), suppress)
18        )
19
20    def __enter__(self):
21        """Return the context manager instance."""
22        return self
23
24    def __exit__(self, exc_type, exc_value, traceback):
25        """Handle exceptions based on log and suppress settings."""
26        if exc_type in self._log_exceptions:
27            self._exception = exc_value
28            return True
29
30        return exc_type in self._suppress_exceptions
31
32    @property
33    def exception(self):
34        """Return the exception if any."""
35        return self._exception

.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    """Context manager that suppresses or logs exceptions based on provided exception types."""2    """Context manager that suppresses or logs exceptions based on provided exception types."""
33
4    def __init__(self, log=(), suppress=()):4    def __init__(self, log=(), suppress=()):
5        """5        """
6        Initialize the ProtectedSection context manager.6        Initialize the ProtectedSection context manager.
77
8        Args:8        Args:
9            log (tuple): Tuple of exception types to log. Defaults to an empty tuple.9            log (tuple): Tuple of exception types to log. Defaults to an empty tuple.
10            suppress (tuple): Tuple of exception types to suppress. Defaults to an empty tuple.10            suppress (tuple): Tuple of exception types to suppress. Defaults to an empty tuple.
11        """11        """
n12        self.exception = Nonen12        self._exception = None
13        self._log_exceptions = tuple(13        self._log_exceptions = tuple(
14            filter(lambda exc_type: issubclass(exc_type, Exception), log)14            filter(lambda exc_type: issubclass(exc_type, Exception), log)
15        )15        )
16        self._suppress_exceptions = tuple(16        self._suppress_exceptions = tuple(
17            filter(lambda exc_type: issubclass(exc_type, Exception), suppress)17            filter(lambda exc_type: issubclass(exc_type, Exception), suppress)
18        )18        )
1919
20    def __enter__(self):20    def __enter__(self):
21        """Return the context manager instance."""21        """Return the context manager instance."""
22        return self22        return self
2323
24    def __exit__(self, exc_type, exc_value, traceback):24    def __exit__(self, exc_type, exc_value, traceback):
25        """Handle exceptions based on log and suppress settings."""25        """Handle exceptions based on log and suppress settings."""
26        if exc_type in self._log_exceptions:26        if exc_type in self._log_exceptions:
n27            self.exception = exc_valuen27            self._exception = exc_value
28            return True28            return True
2929
30        return exc_type in self._suppress_exceptions30        return exc_type in self._suppress_exceptions
tt31 
32    @property
33    def exception(self):
34        """Return the exception if any."""
35        return self._exception
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op