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Сори, не знаех за това. Пускам ъпдейтнато решение, което включва и малко докстрингове.
|
| f | 1 | class ProtectedSection: | f | 1 | class ProtectedSection: |
| n | n | 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 | """ | ||||
| 2 | def __init__(self, log=(), suppress=()): | 12 | def __init__(self, log=(), suppress=()): | ||
| n | n | 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 | """ | ||||
| 3 | self.log = log | 20 | self.log = log | ||
| 4 | self.suppress = suppress | 21 | self.suppress = suppress | ||
| 5 | self.exception = None | 22 | self.exception = None | ||
| n | 6 | n | 23 | ||
| 7 | def __enter__(self): | 24 | def __enter__(self): | ||
| n | n | 25 | """ | ||
| 26 | Enters the context manager, returning itself. | ||||
| 27 | |||||
| 28 | Returns: | ||||
| 29 | ProtectedSection: The context manager instance itself. | ||||
| 30 | """ | ||||
| 8 | return self | 31 | return self | ||
| 9 | 32 | ||||
| t | 10 | def __exit__(self, exc_type, exc_value, traceback): | t | 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 | """ | ||||
| 11 | if exc_type in self.log: | 46 | if exc_type in self.log: | ||
| 12 | self.exception = exc_value | 47 | self.exception = exc_value | ||
| 13 | return True | 48 | return True | ||
| 14 | if exc_type in self.suppress: | 49 | if exc_type in self.suppress: | ||
| 15 | return True | 50 | return True | ||
| 16 | return False | 51 | return False |
| Legends | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
| |||||||||