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

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

0 точки общо

0 успешни теста
2 неуспешни теста
Код
Скрий всички коментари

 1class ProtectedSection:
 2    exception = Exception(None)

Не мисля, че сме говорили за инициализиране на грешки по този начин и с добра причина - няма никаква функционална стойност.
Грешките приемат стринг като опционален аргумент, да инициализираме с None не е особено полезно.

3 4 def __init__(self, log=(), suppress=()): 5 self.log = log 6 self.suppress = suppress 7 8 def __enter__(self): 9 return self 10 11 def __exit__(self, exc_type, exc_val, exc_tb): 12 if exc_type is None: 13 return True 14 elif 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 20 21

FF
======================================================================
FAIL: test_solution (test.TestSolution.test_solution)
Test everything in a single test case. Only 100% gives a point.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 26, in test_solution
self.assertIsNone(cm.exception)
AssertionError: Exception(None) is not None

======================================================================
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.001s

FAILED (failures=2)

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

f1class ProtectedSection:f1class ProtectedSection:
2    exception = Exception(None)2    exception = Exception(None)
3    3    
t4    def __init__(self, log=None, suppress=None):t4    def __init__(self, log=(), suppress=()):

Можеш направо да дефинираш тук стойности по подразбиране - празни кортежи, за да спестиш проверката долу.

5        self.log = log or ()5        self.log = log
6        self.suppress = suppress or ()6        self.suppress = suppress
77
8    def __enter__(self):8    def __enter__(self):
9        return self9        return self
1010
11    def __exit__(self, exc_type, exc_val, exc_tb):11    def __exit__(self, exc_type, exc_val, exc_tb):
12        if exc_type is None:12        if exc_type is None:
13            return True13            return True
14        elif exc_type in self.log:14        elif 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
2020
21    21    
2222
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op