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

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

1 точки общо

1 успешни теста
1 неуспешни теста
Код (traceback -> exc_traceback + docstrings)

 1class ProtectedSection:
 2    """
 3    A context manager for handling exceptions with optional logging and suppression.
 4    
 5    Parameters:
 6    log (tuple): A tuple of exception types to be logged.
 7    suppress (tuple): A tuple of exception types to be suppressed.
 8    
 9    Attributes:
10    exception (Exception): Stores the last logged exception if any.
11    """
12    def __init__(self, log=(), suppress=()):
13        """
14        Initializes the ProtectedSection with specified logging and suppression behavior.
15        
16        Args:
17            log (tuple): Exception types to be logged.
18            suppress (tuple): Exception types to be suppressed.
19        """
20        self.log = log
21        self.suppress = suppress
22        self.exception = None
23
24    def __enter__(self):
25        """
26        Enters the context manager, returning itself.
27        
28        Returns:
29            ProtectedSection: The context manager instance itself.
30        """
31        return self
32
33    def __exit__(self, exc_type, exc_value, exc_traceback):
34        """
35        Handles exceptions raised within the context. Logs or suppresses them
36        based on the provided parameters.
37        
38        Args:
39            exc_type (type): The type of the raised exception, if any.
40            exc_value (Exception): The instance of the raised exception, if any.
41            traceback_obj (traceback): The traceback object associated with the exception.
42        
43        Returns:
44            bool: True if the exception should be suppressed, False otherwise.
45        """
46        if exc_type in self.log:
47            self.exception = exc_value
48            return True
49        if exc_type in self.suppress:
50            return True
51        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)

Дискусия
Никола Георгиев
08.11.2024 14:58

Сори, не знаех за това. Пускам ъпдейтнато решение, което включва и малко докстрингове.
История

f1class ProtectedSection:f1class ProtectedSection:
nn2    """
3    A context manager for handling exceptions with optional logging and suppression.
4    
5    Parameters:
6    log (tuple): A tuple of exception types to be logged.
7    suppress (tuple): A tuple of exception types to be suppressed.
8    
9    Attributes:
10    exception (Exception): Stores the last logged exception if any.
11    """
2    def __init__(self, log=(), suppress=()):12    def __init__(self, log=(), suppress=()):
nn13        """
14        Initializes the ProtectedSection with specified logging and suppression behavior.
15        
16        Args:
17            log (tuple): Exception types to be logged.
18            suppress (tuple): Exception types to be suppressed.
19        """
3        self.log = log20        self.log = log
4        self.suppress = suppress21        self.suppress = suppress
5        self.exception = None22        self.exception = None
n6    n23 
7    def __enter__(self):24    def __enter__(self):
nn25        """
26        Enters the context manager, returning itself.
27        
28        Returns:
29            ProtectedSection: The context manager instance itself.
30        """
8        return self31        return self
932
t10    def __exit__(self, exc_type, exc_value, traceback):t33    def __exit__(self, exc_type, exc_value, exc_traceback):
34        """
35        Handles exceptions raised within the context. Logs or suppresses them
36        based on the provided parameters.
37        
38        Args:
39            exc_type (type): The type of the raised exception, if any.
40            exc_value (Exception): The instance of the raised exception, if any.
41            traceback_obj (traceback): The traceback object associated with the exception.
42        
43        Returns:
44            bool: True if the exception should be suppressed, False otherwise.
45        """
11        if exc_type in self.log:46        if exc_type in self.log:
12            self.exception = exc_value47            self.exception = exc_value
13            return True48            return True
14        if exc_type in self.suppress:49        if exc_type in self.suppress:
15            return True50            return True
16        return False51        return False
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op