Предизвикателства > Безгрешен блок > Решения > Решението на Ивайло Кънчев

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

1 точки общо

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

 1class ProtectedSection:
 2    """Handle exceptions gracefully within a given section"""
 3
 4    def __init__(self, log = (), suppress = ()):
 5        self.log = log
 6        self.suppress = suppress
 7        self.exception = None
 8
 9    def __enter__(self):
10        self.exception = None
11        return self
12
13    def __exit__(self, exc_type, exc_val, exc_tb):
14        if exc_type is None:
15            return True
16        if isinstance(exc_val, self.log):
17            self.exception = exc_val
18            return True
19        if isinstance(exc_val, self.suppress):
20            return True
21        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 133, in test_special_cases
self.assertIsNone(cm.exception)
AssertionError: ZeroDivisionError('Ако ползваш AI генератори, ще влезеш в черния списък!') is not None

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

FAILED (failures=1)

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

f1class ProtectedSection:f1class ProtectedSection:
2    """Handle exceptions gracefully within a given section"""2    """Handle exceptions gracefully within a given section"""
33
4    def __init__(self, log = (), suppress = ()):4    def __init__(self, log = (), suppress = ()):
5        self.log = log5        self.log = log
6        self.suppress = suppress6        self.suppress = suppress
7        self.exception = None7        self.exception = None
88
9    def __enter__(self):9    def __enter__(self):
nn10        self.exception = None
10        return self11        return self
1112
12    def __exit__(self, exc_type, exc_val, exc_tb):13    def __exit__(self, exc_type, exc_val, exc_tb):
13        if exc_type is None:14        if exc_type is None:
14            return True15            return True
15        if isinstance(exc_val, self.log):16        if isinstance(exc_val, self.log):
16            self.exception = exc_val17            self.exception = exc_val
17            return True18            return True
18        if isinstance(exc_val, self.suppress):19        if isinstance(exc_val, self.suppress):
t19            self.exception = Nonet
20            return True20            return True
21        return False21        return False
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

f1class ProtectedSection:f1class ProtectedSection:
2    """Handle exceptions gracefully within a given section"""2    """Handle exceptions gracefully within a given section"""
33
4    def __init__(self, log = (), suppress = ()):4    def __init__(self, log = (), suppress = ()):
5        self.log = log5        self.log = log
6        self.suppress = suppress6        self.suppress = suppress
7        self.exception = None7        self.exception = None
88
9    def __enter__(self):9    def __enter__(self):
10        return self10        return self
1111
12    def __exit__(self, exc_type, exc_val, exc_tb):12    def __exit__(self, exc_type, exc_val, exc_tb):
13        if exc_type is None:13        if exc_type is None:
14            return True14            return True
15        if isinstance(exc_val, self.log):15        if isinstance(exc_val, self.log):
16            self.exception = exc_val16            self.exception = exc_val
17            return True17            return True
18        if isinstance(exc_val, self.suppress):18        if isinstance(exc_val, self.suppress):
tt19            self.exception = None
19            return True20            return True
20        return False21        return False
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

f1class ProtectedSection:f1class ProtectedSection:
2    """Handle exceptions gracefully within a given section"""2    """Handle exceptions gracefully within a given section"""
33
4    def __init__(self, log = (), suppress = ()):4    def __init__(self, log = (), suppress = ()):
5        self.log = log5        self.log = log
6        self.suppress = suppress6        self.suppress = suppress
7        self.exception = None7        self.exception = None
88
9    def __enter__(self):9    def __enter__(self):
10        return self10        return self
1111
12    def __exit__(self, exc_type, exc_val, exc_tb):12    def __exit__(self, exc_type, exc_val, exc_tb):
13        if exc_type is None:13        if exc_type is None:
14            return True14            return True
15        if isinstance(exc_val, self.log):15        if isinstance(exc_val, self.log):
16            self.exception = exc_val16            self.exception = exc_val
17            return True17            return True
18        if isinstance(exc_val, self.suppress):18        if isinstance(exc_val, self.suppress):
19            return True19            return True
20        return False20        return False
t21 t
22with ProtectedSection(log=(ZeroDivisionError, IndexError), suppress=(TypeError, ZeroDivisionError, Exception)) as err:
23    x = 1 / 0
24 
25print(err.exception)
26# division by zero
27print(type(err.exception))
28# <class 'ZeroDivisionError'>
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

f1class ProtectedSection:f1class ProtectedSection:
n2    """Handle exceptions gracefully within a given context"""n2    """Handle exceptions gracefully within a given section"""
33
4    def __init__(self, log = (), suppress = ()):4    def __init__(self, log = (), suppress = ()):
5        self.log = log5        self.log = log
6        self.suppress = suppress6        self.suppress = suppress
7        self.exception = None7        self.exception = None
88
9    def __enter__(self):9    def __enter__(self):
10        return self10        return self
1111
12    def __exit__(self, exc_type, exc_val, exc_tb):12    def __exit__(self, exc_type, exc_val, exc_tb):
13        if exc_type is None:13        if exc_type is None:
14            return True14            return True
15        if isinstance(exc_val, self.log):15        if isinstance(exc_val, self.log):
16            self.exception = exc_val16            self.exception = exc_val
17            return True17            return True
18        if isinstance(exc_val, self.suppress):18        if isinstance(exc_val, self.suppress):
19            return True19            return True
20        return False20        return False
2121
tt22with ProtectedSection(log=(ZeroDivisionError, IndexError), suppress=(TypeError, ZeroDivisionError, Exception)) as err:
23    x = 1 / 0
24 
25print(err.exception)
26# division by zero
27print(type(err.exception))
28# <class 'ZeroDivisionError'>
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

t1class ProtectedSection:t1class ProtectedSection:
2    """Handle exceptions gracefully within a given context"""2    """Handle exceptions gracefully within a given context"""
33
4    def __init__(self, log = (), suppress = ()):4    def __init__(self, log = (), suppress = ()):
5        self.log = log5        self.log = log
6        self.suppress = suppress6        self.suppress = suppress
7        self.exception = None7        self.exception = None
88
9    def __enter__(self):9    def __enter__(self):
10        return self10        return self
1111
12    def __exit__(self, exc_type, exc_val, exc_tb):12    def __exit__(self, exc_type, exc_val, exc_tb):
13        if exc_type is None:13        if exc_type is None:
14            return True14            return True
15        if isinstance(exc_val, self.log):15        if isinstance(exc_val, self.log):
16            self.exception = exc_val16            self.exception = exc_val
17            return True17            return True
18        if isinstance(exc_val, self.suppress):18        if isinstance(exc_val, self.suppress):
19            return True19            return True
20        return False20        return False
2121
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op