Предизвикателства > Разбий ни ключалката > Решения > Решението на Йоан Байчев

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

1 точки общо

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

 1class LockPicker_0MI0600328:
 2    DEFAULT_VALUES = {
 3        int: int(),
 4        str: str(),
 5        float: float(),
 6        bool: bool(),
 7        list: list(),
 8        dict: dict(),
 9        set: set(),
10        tuple: tuple(),
11        complex: complex()
12    }
13
14    def __init__(self, lock):
15        self.lock = lock
16        self.args = []
17
18    def _handle_type_error(self, exc):
19        if exc.position is None:
20            return [None] * exc.expected
21
22        args_copy = self.args[:]
23        position = exc.position - 1
24        expected_type = exc.expected
25
26        args_copy[position] = self.DEFAULT_VALUES[expected_type]
27        return args_copy
28
29    def _handle_value_error(self, exc):
30        args_copy = self.args[:]
31        position = exc.position - 1
32        expected_value = exc.expected
33
34        args_copy[position] = expected_value
35        return args_copy
36
37    def unlock(self):
38        while True:
39            try:
40                if self.lock.pick(*self.args):
41                    break
42            except TypeError as exc:
43                self.args = self._handle_type_error(exc)
44            except ValueError as exc:
45                self.args = self._handle_value_error(exc)

Резултат от контролното:
17/25 верни отговора.
14 точки.
.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK

Дискусия
Йоан Байчев
13.11.2024 01:34

Имах два файла challenge.py и качих грешния първо
История

f1class LockPicker_0MI0600328:f1class LockPicker_0MI0600328:
2    DEFAULT_VALUES = {2    DEFAULT_VALUES = {
3        int: int(),3        int: int(),
4        str: str(),4        str: str(),
5        float: float(),5        float: float(),
6        bool: bool(),6        bool: bool(),
7        list: list(),7        list: list(),
8        dict: dict(),8        dict: dict(),
9        set: set(),9        set: set(),
10        tuple: tuple(),10        tuple: tuple(),
11        complex: complex()11        complex: complex()
12    }12    }
1313
14    def __init__(self, lock):14    def __init__(self, lock):
15        self.lock = lock15        self.lock = lock
16        self.args = []16        self.args = []
1717
18    def _handle_type_error(self, exc):18    def _handle_type_error(self, exc):
19        if exc.position is None:19        if exc.position is None:
20            return [None] * exc.expected20            return [None] * exc.expected
2121
22        args_copy = self.args[:]22        args_copy = self.args[:]
23        position = exc.position - 123        position = exc.position - 1
24        expected_type = exc.expected24        expected_type = exc.expected
2525
t26        args_copy[position] = self.DEFAULT_VALUES[expected_type] if expected_type in self.DEFAULT_VALUES else Nonet26        args_copy[position] = self.DEFAULT_VALUES[expected_type]
27        return args_copy27        return args_copy
2828
29    def _handle_value_error(self, exc):29    def _handle_value_error(self, exc):
30        args_copy = self.args[:]30        args_copy = self.args[:]
31        position = exc.position - 131        position = exc.position - 1
32        expected_value = exc.expected32        expected_value = exc.expected
3333
34        args_copy[position] = expected_value34        args_copy[position] = expected_value
35        return args_copy35        return args_copy
3636
37    def unlock(self):37    def unlock(self):
38        while True:38        while True:
39            try:39            try:
40                if self.lock.pick(*self.args):40                if self.lock.pick(*self.args):
41                    break41                    break
42            except TypeError as exc:42            except TypeError as exc:
43                self.args = self._handle_type_error(exc)43                self.args = self._handle_type_error(exc)
44            except ValueError as exc:44            except ValueError as exc:
45                self.args = self._handle_value_error(exc)45                self.args = self._handle_value_error(exc)
4646
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

f1class LockPicker_0MI0600328:f1class LockPicker_0MI0600328:
2    DEFAULT_VALUES = {2    DEFAULT_VALUES = {
3        int: int(),3        int: int(),
4        str: str(),4        str: str(),
5        float: float(),5        float: float(),
6        bool: bool(),6        bool: bool(),
7        list: list(),7        list: list(),
8        dict: dict(),8        dict: dict(),
9        set: set(),9        set: set(),
10        tuple: tuple(),10        tuple: tuple(),
11        complex: complex()11        complex: complex()
12    }12    }
1313
14    def __init__(self, lock):14    def __init__(self, lock):
15        self.lock = lock15        self.lock = lock
16        self.args = []16        self.args = []
1717
18    def _handle_type_error(self, exc):18    def _handle_type_error(self, exc):
19        if exc.position is None:19        if exc.position is None:
20            return [None] * exc.expected20            return [None] * exc.expected
2121
22        args_copy = self.args[:]22        args_copy = self.args[:]
23        position = exc.position - 123        position = exc.position - 1
24        expected_type = exc.expected24        expected_type = exc.expected
2525
t26        args_copy[position] = self.DEFAULT_VALUES.get(expected_type, None)t26        args_copy[position] = self.DEFAULT_VALUES[expected_type] if expected_type in self.DEFAULT_VALUES else None
27        return args_copy27        return args_copy
2828
29    def _handle_value_error(self, exc):29    def _handle_value_error(self, exc):
30        args_copy = self.args[:]30        args_copy = self.args[:]
31        position = exc.position - 131        position = exc.position - 1
32        expected_value = exc.expected32        expected_value = exc.expected
3333
34        args_copy[position] = expected_value34        args_copy[position] = expected_value
35        return args_copy35        return args_copy
3636
37    def unlock(self):37    def unlock(self):
38        while True:38        while True:
39            try:39            try:
40                if self.lock.pick(*self.args):40                if self.lock.pick(*self.args):
41                    break41                    break
42            except TypeError as exc:42            except TypeError as exc:
43                self.args = self._handle_type_error(exc)43                self.args = self._handle_type_error(exc)
44            except ValueError as exc:44            except ValueError as exc:
45                self.args = self._handle_value_error(exc)45                self.args = self._handle_value_error(exc)
4646
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

f1class LockPicker_0MI0600328:f1class LockPicker_0MI0600328:
2    DEFAULT_VALUES = {2    DEFAULT_VALUES = {
n3        int: 0,n3        int: int(),
4        str: "",4        str: str(),
5        float: 0.0,5        float: float(),
6        bool: False,6        bool: bool(),
7        list: [],7        list: list(),
8        dict: {},8        dict: dict(),
9        set: set(),9        set: set(),
n10        tuple: (),n10        tuple: tuple(),
11        complex: complex(0, 0)11        complex: complex()
12    }12    }
1313
14    def __init__(self, lock):14    def __init__(self, lock):
15        self.lock = lock15        self.lock = lock
n16        self.attempted_args = set()n16        self.args = []
1717
n18    def _handle_type_error(self, exc, args):n18    def _handle_type_error(self, exc):
19        if exc.position is None:19        if exc.position is None:
n20            expected_count = exc.expectedn
21            return [None] * expected_count20            return [None] * exc.expected
2221
nn22        args_copy = self.args[:]
23        position = exc.position - 123        position = exc.position - 1
24        expected_type = exc.expected24        expected_type = exc.expected
n25        n
26        if (position, expected_type) in self.attempted_args:
27            return args
28        
29        args[position] = self.DEFAULT_VALUES.get(expected_type, None)
30        self.attempted_args.add((position, expected_type))
31        return args
3225
nn26        args_copy[position] = self.DEFAULT_VALUES.get(expected_type, None)
27        return args_copy
28 
33    def _handle_value_error(self, ex, args):29    def _handle_value_error(self, exc):
30        args_copy = self.args[:]
34        position = ex.position - 131        position = exc.position - 1
35        expected_value = ex.expected32        expected_value = exc.expected
33 
36        args[position] = expected_value34        args_copy[position] = expected_value
37        return args35        return args_copy
3836
39    def unlock(self):37    def unlock(self):
n40        args = []n
41 
42        while True:38        while True:
43            try:39            try:
n44                if self.lock.pick(*args):n40                if self.lock.pick(*self.args):
45                    break41                    break
t46            except Exception as ex:t42            except TypeError as exc:
47                if isinstance(ex, TypeError):
48                    args = self._handle_type_error(ex, args)43                self.args = self._handle_type_error(exc)
49                elif isinstance(ex, ValueError):44            except ValueError as exc:
50                    args = self._handle_value_error(ex, args)45                self.args = self._handle_value_error(exc)
5146
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

n1import unittestn1class LockPicker_0MI0600328:
2    DEFAULT_VALUES = {
3        int: 0,
4        str: "",
5        float: 0.0,
6        bool: False,
7        list: [],
8        dict: {},
9        set: set(),
10        tuple: (),
11        complex: complex(0, 0)
12    }
213
n3def optimize_exception_check(attr_name):n14    def __init__(self, lock):
4    def decorator(func):15        self.lock = lock
5        def wrapper(self, exc_type):16        self.attempted_args = set()
6            cache = getattr(self, attr_name)
7            if exc_type in cache:
8                return True
917
n10            result = func(self, exc_type)  # True or Falsen18    def _handle_type_error(self, exc, args):
11            if result:19        if exc.position is None:
12                cache.add(exc_type)20            expected_count = exc.expected
13            return result21            return [None] * expected_count
14        return wrapper
15    return decorator
1622
nn23        position = exc.position - 1
24        expected_type = exc.expected
25        
26        if (position, expected_type) in self.attempted_args:
27            return args
28        
29        args[position] = self.DEFAULT_VALUES.get(expected_type, None)
30        self.attempted_args.add((position, expected_type))
31        return args
1732
n18class ProtectedSection:n33    def _handle_value_error(self, ex, args):
19    __counter = 034        position = ex.position - 1
35        expected_value = ex.expected
36        args[position] = expected_value
37        return args
2038
n21    def __init__(self, log=(), suppress=()):n39    def unlock(self):
22        if log is not None:40        args = []
23            for exc in log:
24                if not issubclass(exc, Exception):
25                    raise TypeError("Not a valid exception type")
2641
n27        if suppress is not None:n42        while True:
28            for exc in suppress:43            try:
29                if not issubclass(exc, Exception):44                if self.lock.pick(*args):
30                    raise TypeError("Not a valid exception type")45                    break
46            except Exception as ex:
47                if isinstance(ex, TypeError):
48                    args = self._handle_type_error(ex, args)
49                elif isinstance(ex, ValueError):
50                    args = self._handle_value_error(ex, args)
3151
t32        self._current_exception = None  # Saves the specific or last "updated" exception in case of reuse of an already created instancet
33        self.log_exceptions = set(log)
34        self.suppress_exceptions = set(suppress)
35        self._checked_exceptions_logs = set()  # Optimization to avoid checking one exception n times
36        self._checked_exceptions_suppress = set()  # Optimization to avoid checking one exception n times
37        self.exceptions_by_session = {}  # Dictionary to store exceptions with their session ID
38 
39    @optimize_exception_check("_checked_exceptions_logs")
40    def is_exception_logged(self, exc_type):
41        return exc_type in self.log_exceptions
42 
43    @optimize_exception_check("_checked_exceptions_suppress")
44    def is_exception_suppressed(self, exc_type):
45        return exc_type in self.suppress_exceptions
46 
47    def __enter__(self):
48        self._current_exception = None
49        return self
50 
51    def __exit__(self, exc_type, exc_value, traceback):
52        if exc_type:
53            if self.is_exception_logged(exc_type):
54                self.exceptions_by_session[self.__counter] = exc_value
55                self.__counter += 1
56                self._current_exception = exc_value
57                return True
58 
59            if self.is_exception_suppressed(exc_type):
60                self._current_exception = None
61                self.__counter += 1
62                return True
63 
64        return False
65 
66    @property
67    def exception(self):
68        return self._current_exception
69 
70    @property
71    def exceptions(self):
72        return self.exceptions_by_session
73 
74    def get_exception_by_session(self, session_id):
75        return self.exceptions_by_session.get(session_id, None)
76 
77 
78class TestProtectedSection(unittest.TestCase):
79 
80    def test_log_exception(self):
81        with ProtectedSection(log=(ZeroDivisionError,)) as err:
82            x = 1 / 0
83        self.assertIsInstance(err.exception, ZeroDivisionError)
84        self.assertEqual(str(err.exception), "division by zero")
85 
86    def test_suppress_exception(self):
87        with ProtectedSection(suppress=(ZeroDivisionError,)) as err:
88            x = 1 / 0
89        self.assertIsNone(err.exception)
90 
91    def test_log_and_suppress_priority(self):
92        with ProtectedSection(log=(ZeroDivisionError,), suppress=(TypeError, ZeroDivisionError)) as err:
93            x = 1 / 0
94        self.assertIsInstance(err.exception, ZeroDivisionError)
95        self.assertEqual(str(err.exception), "division by zero")
96 
97    def test_log_and_suppress_priority_with_multiple_exceptions(self):
98        with ProtectedSection(log=(ZeroDivisionError, IndexError), suppress=(TypeError, ZeroDivisionError)) as err:
99            x = 1 / 0
100        self.assertIsInstance(err.exception, ZeroDivisionError)
101 
102    def test_no_exception(self):
103        with ProtectedSection(log=(), suppress=(TypeError,)) as err:
104            x = 1 + 1
105        self.assertIsNone(err.exception)
106 
107    def test_unhandled_exception(self):
108        with self.assertRaises(ValueError):
109            with ProtectedSection(log=(ZeroDivisionError,), suppress=(TypeError,)) as err:
110                raise ValueError("err")
111        self.assertIsNone(err.exception)
112 
113    def test_empty_log_and_suppress(self):
114        with self.assertRaises(ZeroDivisionError):
115            with ProtectedSection() as err:
116                x = 1 / 0
117        self.assertIsNone(err.exception)
118 
119    def test_reuse_protected_section(self):
120        protected_section = ProtectedSection(log=(ZeroDivisionError,))
121 
122        with protected_section as err:
123            x = 1 / 0
124        self.assertIsInstance(err.exception, ZeroDivisionError)
125 
126        with protected_section as err:
127            x = 1 + 1
128        self.assertIsNone(err.exception)
129 
130        with self.assertRaises(TypeError):
131            with protected_section as err:
132                x = "text" + 5
133        self.assertIsNone(err.exception)
134 
135        with protected_section as err:
136            x = 1 / 0
137        self.assertIsInstance(err.exception, ZeroDivisionError)
138 
139    def test_suppress_then_log_exception(self):
140        with ProtectedSection(log=(IndexError,), suppress=(ZeroDivisionError,)) as err:
141            y = [][2332]
142        self.assertIsInstance(err.exception, IndexError)
143 
144    def test_suppress_then_no_exception(self):
145        protected_section = ProtectedSection(suppress=(ZeroDivisionError,))
146 
147        with protected_section as err:
148            x = 1 / 0
149        self.assertIsNone(err.exception)
150 
151        with protected_section as err:
152            x = 1 + 1
153        self.assertIsNone(err.exception)
154 
155    def test_complex_combination(self):
156        protected_section = ProtectedSection(log=(ZeroDivisionError, IndexError), suppress=(TypeError, ValueError))
157 
158        with protected_section as err:
159            x = 1 / 0
160        self.assertIsInstance(err.exception, ZeroDivisionError)
161        self.assertEqual(str(err.exception), "division by zero")
162 
163        with protected_section as err:
164            x = "text" + 5
165        self.assertIsNone(err.exception)
166 
167        with protected_section as err:
168            y = [][2332]
169        self.assertIsInstance(err.exception, IndexError)
170 
171        with protected_section as err:
172            raise ValueError("err")
173        self.assertIsNone(err.exception)
174 
175        with self.assertRaises(KeyError):
176            with protected_section as err:
177                raise KeyError("key err")
178        self.assertIsNone(err.exception)
179 
180        with protected_section as err:
181            x = 1 + 1
182        self.assertIsNone(err.exception)
183 
184        with protected_section as err:
185            x = "text" + 5
186        self.assertIsNone(err.exception)
187 
188        with protected_section as err:
189            x = 1 / 0
190        self.assertIsInstance(err.exception, ZeroDivisionError)
191 
192        with protected_section as err:
193            raise ValueError("another error")
194        self.assertIsNone(err.exception)
195 
196        with protected_section as err:
197            z = [][999]
198        self.assertIsInstance(err.exception, IndexError)
199 
200        with protected_section as err:
201            x = "string" + 5
202        self.assertIsNone(err.exception)
203 
204        with protected_section as err:
205            x = 1 / 0
206        self.assertIsInstance(err.exception, ZeroDivisionError)
207 
208        with protected_section as err:
209            result = 42 * 42
210        self.assertIsNone(err.exception)
211 
212    def test_complex_combination_with_empty_cases(self):
213        protected_section = ProtectedSection(log=(ZeroDivisionError, IndexError), suppress=(TypeError, ValueError))
214 
215        with protected_section as err:
216            x = 1 / 0
217        self.assertIsInstance(err.exception, ZeroDivisionError)
218        self.assertEqual(str(err.exception), "division by zero")
219 
220        with protected_section as err:
221            x = "text" + 5
222        self.assertIsNone(err.exception)
223 
224        with protected_section as err:
225            y = [][2332]
226        self.assertIsInstance(err.exception, IndexError)
227 
228        with protected_section as err:
229            raise ValueError("err")
230        self.assertIsNone(err.exception)
231 
232        with self.assertRaises(KeyError):
233            with protected_section as err:
234                raise KeyError("key error")
235        self.assertIsNone(err.exception)
236 
237        with protected_section as err:
238            x = 1 + 1
239        self.assertIsNone(err.exception)
240 
241        with protected_section as err:
242            x = "text" + 5
243        self.assertIsNone(err.exception)
244 
245        with protected_section as err:
246            x = 1 / 0
247        self.assertIsInstance(err.exception, ZeroDivisionError)
248 
249        with protected_section as err:
250            raise ValueError("another error")
251        self.assertIsNone(err.exception)
252 
253        with protected_section as err:
254            z = [][999]
255        self.assertIsInstance(err.exception, IndexError)
256 
257        with protected_section as err:
258            x = "string" + 5
259        self.assertIsNone(err.exception)
260 
261        with protected_section as err:
262            x = 1 / 0
263        self.assertIsInstance(err.exception, ZeroDivisionError)
264 
265        with protected_section as err:
266            result = 42 * 42
267        self.assertIsNone(err.exception)
268 
269        protected_section_empty_log = ProtectedSection(log=(), suppress=(TypeError,))
270 
271        with protected_section_empty_log as err:
272            x = "text" + 5
273        self.assertIsNone(err.exception)
274 
275        with self.assertRaises(ZeroDivisionError):
276            with protected_section_empty_log as err:
277                x = 1 / 0
278        self.assertIsNone(err.exception)
279 
280        protected_section_empty_suppress = ProtectedSection(log=(ZeroDivisionError,), suppress=())
281 
282        with protected_section_empty_suppress as err:
283            x = 1 / 0
284        self.assertIsInstance(err.exception, ZeroDivisionError)
285 
286        with self.assertRaises(TypeError):
287            with protected_section_empty_suppress as err:
288                x = "text" + 5
289        self.assertIsNone(err.exception)
290 
291        protected_section_empty_both = ProtectedSection()
292 
293        with self.assertRaises(ZeroDivisionError):
294            with protected_section_empty_both as err:
295                x = 1 / 0
296        self.assertIsNone(err.exception)
297 
298        with self.assertRaises(TypeError):
299            with protected_section_empty_both as err:
300                x = "text" + 5
301        self.assertIsNone(err.exception)
302 
303        with protected_section_empty_both as err:
304            x = 1 + 1
305        self.assertIsNone(err.exception)
306 
307    # Viktor ?
308    def test_reuse_protected_section_with_reset_exception(self):
309        protected_section = ProtectedSection(log=(ValueError,))
310 
311        with protected_section as cm1:
312            raise ValueError('Text 1')
313        self.assertIsInstance(cm1.exception, ValueError)
314        self.assertEqual(str(cm1.exception), 'Text 1')
315 
316        with protected_section as cm2:
317            raise ValueError('Text 2')
318        self.assertIsInstance(cm2.exception, ValueError)
319        self.assertEqual(str(cm2.exception), 'Text 2')
320 
321        self.assertEqual(str(cm1.exception), 'Text 2')
322 
323 
324if __name__ == "__main__":
325    unittest.main()
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

t1import unittestt1import unittest
22
3def optimize_exception_check(attr_name):3def optimize_exception_check(attr_name):
4    def decorator(func):4    def decorator(func):
5        def wrapper(self, exc_type):5        def wrapper(self, exc_type):
6            cache = getattr(self, attr_name)6            cache = getattr(self, attr_name)
7            if exc_type in cache:7            if exc_type in cache:
8                return True8                return True
99
10            result = func(self, exc_type)  # True or False10            result = func(self, exc_type)  # True or False
11            if result:11            if result:
12                cache.add(exc_type)12                cache.add(exc_type)
13            return result13            return result
14        return wrapper14        return wrapper
15    return decorator15    return decorator
1616
1717
18class ProtectedSection:18class ProtectedSection:
19    __counter = 019    __counter = 0
2020
21    def __init__(self, log=(), suppress=()):21    def __init__(self, log=(), suppress=()):
22        if log is not None:22        if log is not None:
23            for exc in log:23            for exc in log:
24                if not issubclass(exc, Exception):24                if not issubclass(exc, Exception):
25                    raise TypeError("Not a valid exception type")25                    raise TypeError("Not a valid exception type")
2626
27        if suppress is not None:27        if suppress is not None:
28            for exc in suppress:28            for exc in suppress:
29                if not issubclass(exc, Exception):29                if not issubclass(exc, Exception):
30                    raise TypeError("Not a valid exception type")30                    raise TypeError("Not a valid exception type")
3131
32        self._current_exception = None  # Saves the specific or last "updated" exception in case of reuse of an already created instance32        self._current_exception = None  # Saves the specific or last "updated" exception in case of reuse of an already created instance
33        self.log_exceptions = set(log)33        self.log_exceptions = set(log)
34        self.suppress_exceptions = set(suppress)34        self.suppress_exceptions = set(suppress)
35        self._checked_exceptions_logs = set()  # Optimization to avoid checking one exception n times35        self._checked_exceptions_logs = set()  # Optimization to avoid checking one exception n times
36        self._checked_exceptions_suppress = set()  # Optimization to avoid checking one exception n times36        self._checked_exceptions_suppress = set()  # Optimization to avoid checking one exception n times
37        self.exceptions_by_session = {}  # Dictionary to store exceptions with their session ID37        self.exceptions_by_session = {}  # Dictionary to store exceptions with their session ID
3838
39    @optimize_exception_check("_checked_exceptions_logs")39    @optimize_exception_check("_checked_exceptions_logs")
40    def is_exception_logged(self, exc_type):40    def is_exception_logged(self, exc_type):
41        return exc_type in self.log_exceptions41        return exc_type in self.log_exceptions
4242
43    @optimize_exception_check("_checked_exceptions_suppress")43    @optimize_exception_check("_checked_exceptions_suppress")
44    def is_exception_suppressed(self, exc_type):44    def is_exception_suppressed(self, exc_type):
45        return exc_type in self.suppress_exceptions45        return exc_type in self.suppress_exceptions
4646
47    def __enter__(self):47    def __enter__(self):
48        self._current_exception = None48        self._current_exception = None
49        return self49        return self
5050
51    def __exit__(self, exc_type, exc_value, traceback):51    def __exit__(self, exc_type, exc_value, traceback):
52        if exc_type:52        if exc_type:
53            if self.is_exception_logged(exc_type):53            if self.is_exception_logged(exc_type):
54                self.exceptions_by_session[self.__counter] = exc_value54                self.exceptions_by_session[self.__counter] = exc_value
55                self.__counter += 155                self.__counter += 1
56                self._current_exception = exc_value56                self._current_exception = exc_value
57                return True57                return True
5858
59            if self.is_exception_suppressed(exc_type):59            if self.is_exception_suppressed(exc_type):
60                self._current_exception = None60                self._current_exception = None
61                self.__counter += 161                self.__counter += 1
62                return True62                return True
6363
64        return False64        return False
6565
66    @property66    @property
67    def exception(self):67    def exception(self):
68        return self._current_exception68        return self._current_exception
6969
70    @property70    @property
71    def exceptions(self):71    def exceptions(self):
72        return self.exceptions_by_session72        return self.exceptions_by_session
7373
74    def get_exception_by_session(self, session_id):74    def get_exception_by_session(self, session_id):
75        return self.exceptions_by_session.get(session_id, None)75        return self.exceptions_by_session.get(session_id, None)
7676
7777
78class TestProtectedSection(unittest.TestCase):78class TestProtectedSection(unittest.TestCase):
7979
80    def test_log_exception(self):80    def test_log_exception(self):
81        with ProtectedSection(log=(ZeroDivisionError,)) as err:81        with ProtectedSection(log=(ZeroDivisionError,)) as err:
82            x = 1 / 082            x = 1 / 0
83        self.assertIsInstance(err.exception, ZeroDivisionError)83        self.assertIsInstance(err.exception, ZeroDivisionError)
84        self.assertEqual(str(err.exception), "division by zero")84        self.assertEqual(str(err.exception), "division by zero")
8585
86    def test_suppress_exception(self):86    def test_suppress_exception(self):
87        with ProtectedSection(suppress=(ZeroDivisionError,)) as err:87        with ProtectedSection(suppress=(ZeroDivisionError,)) as err:
88            x = 1 / 088            x = 1 / 0
89        self.assertIsNone(err.exception)89        self.assertIsNone(err.exception)
9090
91    def test_log_and_suppress_priority(self):91    def test_log_and_suppress_priority(self):
92        with ProtectedSection(log=(ZeroDivisionError,), suppress=(TypeError, ZeroDivisionError)) as err:92        with ProtectedSection(log=(ZeroDivisionError,), suppress=(TypeError, ZeroDivisionError)) as err:
93            x = 1 / 093            x = 1 / 0
94        self.assertIsInstance(err.exception, ZeroDivisionError)94        self.assertIsInstance(err.exception, ZeroDivisionError)
95        self.assertEqual(str(err.exception), "division by zero")95        self.assertEqual(str(err.exception), "division by zero")
9696
97    def test_log_and_suppress_priority_with_multiple_exceptions(self):97    def test_log_and_suppress_priority_with_multiple_exceptions(self):
98        with ProtectedSection(log=(ZeroDivisionError, IndexError), suppress=(TypeError, ZeroDivisionError)) as err:98        with ProtectedSection(log=(ZeroDivisionError, IndexError), suppress=(TypeError, ZeroDivisionError)) as err:
99            x = 1 / 099            x = 1 / 0
100        self.assertIsInstance(err.exception, ZeroDivisionError)100        self.assertIsInstance(err.exception, ZeroDivisionError)
101101
102    def test_no_exception(self):102    def test_no_exception(self):
103        with ProtectedSection(log=(), suppress=(TypeError,)) as err:103        with ProtectedSection(log=(), suppress=(TypeError,)) as err:
104            x = 1 + 1104            x = 1 + 1
105        self.assertIsNone(err.exception)105        self.assertIsNone(err.exception)
106106
107    def test_unhandled_exception(self):107    def test_unhandled_exception(self):
108        with self.assertRaises(ValueError):108        with self.assertRaises(ValueError):
109            with ProtectedSection(log=(ZeroDivisionError,), suppress=(TypeError,)) as err:109            with ProtectedSection(log=(ZeroDivisionError,), suppress=(TypeError,)) as err:
110                raise ValueError("err")110                raise ValueError("err")
111        self.assertIsNone(err.exception)111        self.assertIsNone(err.exception)
112112
113    def test_empty_log_and_suppress(self):113    def test_empty_log_and_suppress(self):
114        with self.assertRaises(ZeroDivisionError):114        with self.assertRaises(ZeroDivisionError):
115            with ProtectedSection() as err:115            with ProtectedSection() as err:
116                x = 1 / 0116                x = 1 / 0
117        self.assertIsNone(err.exception)117        self.assertIsNone(err.exception)
118118
119    def test_reuse_protected_section(self):119    def test_reuse_protected_section(self):
120        protected_section = ProtectedSection(log=(ZeroDivisionError,))120        protected_section = ProtectedSection(log=(ZeroDivisionError,))
121121
122        with protected_section as err:122        with protected_section as err:
123            x = 1 / 0123            x = 1 / 0
124        self.assertIsInstance(err.exception, ZeroDivisionError)124        self.assertIsInstance(err.exception, ZeroDivisionError)
125125
126        with protected_section as err:126        with protected_section as err:
127            x = 1 + 1127            x = 1 + 1
128        self.assertIsNone(err.exception)128        self.assertIsNone(err.exception)
129129
130        with self.assertRaises(TypeError):130        with self.assertRaises(TypeError):
131            with protected_section as err:131            with protected_section as err:
132                x = "text" + 5132                x = "text" + 5
133        self.assertIsNone(err.exception)133        self.assertIsNone(err.exception)
134134
135        with protected_section as err:135        with protected_section as err:
136            x = 1 / 0136            x = 1 / 0
137        self.assertIsInstance(err.exception, ZeroDivisionError)137        self.assertIsInstance(err.exception, ZeroDivisionError)
138138
139    def test_suppress_then_log_exception(self):139    def test_suppress_then_log_exception(self):
140        with ProtectedSection(log=(IndexError,), suppress=(ZeroDivisionError,)) as err:140        with ProtectedSection(log=(IndexError,), suppress=(ZeroDivisionError,)) as err:
141            y = [][2332]141            y = [][2332]
142        self.assertIsInstance(err.exception, IndexError)142        self.assertIsInstance(err.exception, IndexError)
143143
144    def test_suppress_then_no_exception(self):144    def test_suppress_then_no_exception(self):
145        protected_section = ProtectedSection(suppress=(ZeroDivisionError,))145        protected_section = ProtectedSection(suppress=(ZeroDivisionError,))
146146
147        with protected_section as err:147        with protected_section as err:
148            x = 1 / 0148            x = 1 / 0
149        self.assertIsNone(err.exception)149        self.assertIsNone(err.exception)
150150
151        with protected_section as err:151        with protected_section as err:
152            x = 1 + 1152            x = 1 + 1
153        self.assertIsNone(err.exception)153        self.assertIsNone(err.exception)
154154
155    def test_complex_combination(self):155    def test_complex_combination(self):
156        protected_section = ProtectedSection(log=(ZeroDivisionError, IndexError), suppress=(TypeError, ValueError))156        protected_section = ProtectedSection(log=(ZeroDivisionError, IndexError), suppress=(TypeError, ValueError))
157157
158        with protected_section as err:158        with protected_section as err:
159            x = 1 / 0159            x = 1 / 0
160        self.assertIsInstance(err.exception, ZeroDivisionError)160        self.assertIsInstance(err.exception, ZeroDivisionError)
161        self.assertEqual(str(err.exception), "division by zero")161        self.assertEqual(str(err.exception), "division by zero")
162162
163        with protected_section as err:163        with protected_section as err:
164            x = "text" + 5164            x = "text" + 5
165        self.assertIsNone(err.exception)165        self.assertIsNone(err.exception)
166166
167        with protected_section as err:167        with protected_section as err:
168            y = [][2332]168            y = [][2332]
169        self.assertIsInstance(err.exception, IndexError)169        self.assertIsInstance(err.exception, IndexError)
170170
171        with protected_section as err:171        with protected_section as err:
172            raise ValueError("err")172            raise ValueError("err")
173        self.assertIsNone(err.exception)173        self.assertIsNone(err.exception)
174174
175        with self.assertRaises(KeyError):175        with self.assertRaises(KeyError):
176            with protected_section as err:176            with protected_section as err:
177                raise KeyError("key err")177                raise KeyError("key err")
178        self.assertIsNone(err.exception)178        self.assertIsNone(err.exception)
179179
180        with protected_section as err:180        with protected_section as err:
181            x = 1 + 1181            x = 1 + 1
182        self.assertIsNone(err.exception)182        self.assertIsNone(err.exception)
183183
184        with protected_section as err:184        with protected_section as err:
185            x = "text" + 5185            x = "text" + 5
186        self.assertIsNone(err.exception)186        self.assertIsNone(err.exception)
187187
188        with protected_section as err:188        with protected_section as err:
189            x = 1 / 0189            x = 1 / 0
190        self.assertIsInstance(err.exception, ZeroDivisionError)190        self.assertIsInstance(err.exception, ZeroDivisionError)
191191
192        with protected_section as err:192        with protected_section as err:
193            raise ValueError("another error")193            raise ValueError("another error")
194        self.assertIsNone(err.exception)194        self.assertIsNone(err.exception)
195195
196        with protected_section as err:196        with protected_section as err:
197            z = [][999]197            z = [][999]
198        self.assertIsInstance(err.exception, IndexError)198        self.assertIsInstance(err.exception, IndexError)
199199
200        with protected_section as err:200        with protected_section as err:
201            x = "string" + 5201            x = "string" + 5
202        self.assertIsNone(err.exception)202        self.assertIsNone(err.exception)
203203
204        with protected_section as err:204        with protected_section as err:
205            x = 1 / 0205            x = 1 / 0
206        self.assertIsInstance(err.exception, ZeroDivisionError)206        self.assertIsInstance(err.exception, ZeroDivisionError)
207207
208        with protected_section as err:208        with protected_section as err:
209            result = 42 * 42209            result = 42 * 42
210        self.assertIsNone(err.exception)210        self.assertIsNone(err.exception)
211211
212    def test_complex_combination_with_empty_cases(self):212    def test_complex_combination_with_empty_cases(self):
213        protected_section = ProtectedSection(log=(ZeroDivisionError, IndexError), suppress=(TypeError, ValueError))213        protected_section = ProtectedSection(log=(ZeroDivisionError, IndexError), suppress=(TypeError, ValueError))
214214
215        with protected_section as err:215        with protected_section as err:
216            x = 1 / 0216            x = 1 / 0
217        self.assertIsInstance(err.exception, ZeroDivisionError)217        self.assertIsInstance(err.exception, ZeroDivisionError)
218        self.assertEqual(str(err.exception), "division by zero")218        self.assertEqual(str(err.exception), "division by zero")
219219
220        with protected_section as err:220        with protected_section as err:
221            x = "text" + 5221            x = "text" + 5
222        self.assertIsNone(err.exception)222        self.assertIsNone(err.exception)
223223
224        with protected_section as err:224        with protected_section as err:
225            y = [][2332]225            y = [][2332]
226        self.assertIsInstance(err.exception, IndexError)226        self.assertIsInstance(err.exception, IndexError)
227227
228        with protected_section as err:228        with protected_section as err:
229            raise ValueError("err")229            raise ValueError("err")
230        self.assertIsNone(err.exception)230        self.assertIsNone(err.exception)
231231
232        with self.assertRaises(KeyError):232        with self.assertRaises(KeyError):
233            with protected_section as err:233            with protected_section as err:
234                raise KeyError("key error")234                raise KeyError("key error")
235        self.assertIsNone(err.exception)235        self.assertIsNone(err.exception)
236236
237        with protected_section as err:237        with protected_section as err:
238            x = 1 + 1238            x = 1 + 1
239        self.assertIsNone(err.exception)239        self.assertIsNone(err.exception)
240240
241        with protected_section as err:241        with protected_section as err:
242            x = "text" + 5242            x = "text" + 5
243        self.assertIsNone(err.exception)243        self.assertIsNone(err.exception)
244244
245        with protected_section as err:245        with protected_section as err:
246            x = 1 / 0246            x = 1 / 0
247        self.assertIsInstance(err.exception, ZeroDivisionError)247        self.assertIsInstance(err.exception, ZeroDivisionError)
248248
249        with protected_section as err:249        with protected_section as err:
250            raise ValueError("another error")250            raise ValueError("another error")
251        self.assertIsNone(err.exception)251        self.assertIsNone(err.exception)
252252
253        with protected_section as err:253        with protected_section as err:
254            z = [][999]254            z = [][999]
255        self.assertIsInstance(err.exception, IndexError)255        self.assertIsInstance(err.exception, IndexError)
256256
257        with protected_section as err:257        with protected_section as err:
258            x = "string" + 5258            x = "string" + 5
259        self.assertIsNone(err.exception)259        self.assertIsNone(err.exception)
260260
261        with protected_section as err:261        with protected_section as err:
262            x = 1 / 0262            x = 1 / 0
263        self.assertIsInstance(err.exception, ZeroDivisionError)263        self.assertIsInstance(err.exception, ZeroDivisionError)
264264
265        with protected_section as err:265        with protected_section as err:
266            result = 42 * 42266            result = 42 * 42
267        self.assertIsNone(err.exception)267        self.assertIsNone(err.exception)
268268
269        protected_section_empty_log = ProtectedSection(log=(), suppress=(TypeError,))269        protected_section_empty_log = ProtectedSection(log=(), suppress=(TypeError,))
270270
271        with protected_section_empty_log as err:271        with protected_section_empty_log as err:
272            x = "text" + 5272            x = "text" + 5
273        self.assertIsNone(err.exception)273        self.assertIsNone(err.exception)
274274
275        with self.assertRaises(ZeroDivisionError):275        with self.assertRaises(ZeroDivisionError):
276            with protected_section_empty_log as err:276            with protected_section_empty_log as err:
277                x = 1 / 0277                x = 1 / 0
278        self.assertIsNone(err.exception)278        self.assertIsNone(err.exception)
279279
280        protected_section_empty_suppress = ProtectedSection(log=(ZeroDivisionError,), suppress=())280        protected_section_empty_suppress = ProtectedSection(log=(ZeroDivisionError,), suppress=())
281281
282        with protected_section_empty_suppress as err:282        with protected_section_empty_suppress as err:
283            x = 1 / 0283            x = 1 / 0
284        self.assertIsInstance(err.exception, ZeroDivisionError)284        self.assertIsInstance(err.exception, ZeroDivisionError)
285285
286        with self.assertRaises(TypeError):286        with self.assertRaises(TypeError):
287            with protected_section_empty_suppress as err:287            with protected_section_empty_suppress as err:
288                x = "text" + 5288                x = "text" + 5
289        self.assertIsNone(err.exception)289        self.assertIsNone(err.exception)
290290
291        protected_section_empty_both = ProtectedSection()291        protected_section_empty_both = ProtectedSection()
292292
293        with self.assertRaises(ZeroDivisionError):293        with self.assertRaises(ZeroDivisionError):
294            with protected_section_empty_both as err:294            with protected_section_empty_both as err:
295                x = 1 / 0295                x = 1 / 0
296        self.assertIsNone(err.exception)296        self.assertIsNone(err.exception)
297297
298        with self.assertRaises(TypeError):298        with self.assertRaises(TypeError):
299            with protected_section_empty_both as err:299            with protected_section_empty_both as err:
300                x = "text" + 5300                x = "text" + 5
301        self.assertIsNone(err.exception)301        self.assertIsNone(err.exception)
302302
303        with protected_section_empty_both as err:303        with protected_section_empty_both as err:
304            x = 1 + 1304            x = 1 + 1
305        self.assertIsNone(err.exception)305        self.assertIsNone(err.exception)
306306
307    # Viktor ?307    # Viktor ?
308    def test_reuse_protected_section_with_reset_exception(self):308    def test_reuse_protected_section_with_reset_exception(self):
309        protected_section = ProtectedSection(log=(ValueError,))309        protected_section = ProtectedSection(log=(ValueError,))
310310
311        with protected_section as cm1:311        with protected_section as cm1:
312            raise ValueError('Text 1')312            raise ValueError('Text 1')
313        self.assertIsInstance(cm1.exception, ValueError)313        self.assertIsInstance(cm1.exception, ValueError)
314        self.assertEqual(str(cm1.exception), 'Text 1')314        self.assertEqual(str(cm1.exception), 'Text 1')
315315
316        with protected_section as cm2:316        with protected_section as cm2:
317            raise ValueError('Text 2')317            raise ValueError('Text 2')
318        self.assertIsInstance(cm2.exception, ValueError)318        self.assertIsInstance(cm2.exception, ValueError)
319        self.assertEqual(str(cm2.exception), 'Text 2')319        self.assertEqual(str(cm2.exception), 'Text 2')
320320
321        self.assertEqual(str(cm1.exception), 'Text 2')321        self.assertEqual(str(cm1.exception), 'Text 2')
322322
323323
324if __name__ == "__main__":324if __name__ == "__main__":
325    unittest.main()325    unittest.main()
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op