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

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

1 точки общо

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

 1class ProtectedSection:
 2    """Handle specific exceptions in a controlled manner."""
 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 in self.log:
15            self.exception = exc_val
16            return True
17        elif exc_type in self.suppress:
18            return True
19        return False

..
----------------------------------------------------------------------
Ran 2 tests in 0.000s

OK

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

f1class ProtectedSection:f1class ProtectedSection:
2    """Handle specific exceptions in a controlled manner."""2    """Handle specific exceptions in a controlled manner."""
3    3    
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
8        8        
9    def __enter__(self):9    def __enter__(self):
10        self.exception = None10        self.exception = None
11        return self11        return self
12    12    
13    def __exit__(self, exc_type, exc_val, exc_tb):13    def __exit__(self, exc_type, exc_val, exc_tb):
14        if exc_type in self.log:14        if exc_type in self.log:
15            self.exception = exc_val15            self.exception = exc_val
16            return True16            return True
17        elif exc_type in self.suppress:17        elif exc_type in self.suppress:
18            return True18            return True
19        return False19        return False
t20    t
21    
22protected_section = ProtectedSection(log=(ValueError,))
23 
24with protected_section as cm1:
25    raise ValueError('Text 1')
26print(cm1.exception)
27 
28with protected_section as cm2:
29    raise ValueError('Text 2')
30print(cm2.exception)
31 
32print(cm1.exception)
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

n1 n
2class ProtectedSection:1class ProtectedSection:
3    """Handle specific exceptions in a controlled manner."""2    """Handle specific exceptions in a controlled manner."""
4    3    
5    def __init__(self, log=(), suppress=()):4    def __init__(self, log=(), suppress=()):
6        self.log = log5        self.log = log
7        self.suppress = suppress6        self.suppress = suppress
8        self.exception = None7        self.exception = None
9        8        
10    def __enter__(self):9    def __enter__(self):
nn10        self.exception = None
11        return self11        return self
12    12    
13    def __exit__(self, exc_type, exc_val, exc_tb):13    def __exit__(self, exc_type, exc_val, exc_tb):
14        if exc_type in self.log:14        if exc_type in self.log:
15            self.exception = exc_val15            self.exception = exc_val
16            return True16            return True
17        elif exc_type in self.suppress:17        elif exc_type in self.suppress:
18            return True18            return True
19        return False19        return False
tt20    
21    
22protected_section = ProtectedSection(log=(ValueError,))
23 
24with protected_section as cm1:
25    raise ValueError('Text 1')
26print(cm1.exception)
27 
28with protected_section as cm2:
29    raise ValueError('Text 2')
30print(cm2.exception)
31 
32print(cm1.exception)
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

f1f1
2class ProtectedSection:2class ProtectedSection:
3    """Handle specific exceptions in a controlled manner."""3    """Handle specific exceptions in a controlled manner."""
4    4    
t5    def __init__(self, log = (), suppress = ()):t5    def __init__(self, log=(), suppress=()):
6        self.log = log6        self.log = log
7        self.suppress = suppress7        self.suppress = suppress
8        self.exception = None8        self.exception = None
9        9        
10    def __enter__(self):10    def __enter__(self):
11        return self11        return self
12    12    
13    def __exit__(self, exc_type, exc_val, exc_tb):13    def __exit__(self, exc_type, exc_val, exc_tb):
14        if exc_type in self.log:14        if exc_type in self.log:
15            self.exception = exc_val15            self.exception = exc_val
16            return True16            return True
17        elif exc_type in self.suppress:17        elif exc_type in self.suppress:
18            return True18            return True
19        return False19        return False
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op