1# Адекватно решение
2class ProtectedSection:
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 return self
11
12 def __exit__(self, ex_type, ex_value, _):
13 self.exception = ex_value if ex_type in self._log else None
14 return ex_type in (*self._log, *self._suppress)
15
16
17# Неадекватно решение
18ProtectedSection = type('', (), {'__init__': lambda _, log=(), suppress=(): (setattr(_, '_', {___: (lambda __, ___, ____, _____: setattr(_, '___', ____) or _) for ___ in log}),setattr(_, '__', {_: (lambda *_: True) for _ in suppress}), setattr(_, '___', None), None)[1], '__getattr__': lambda _, __: _.___, '__enter__': lambda _: (_, setattr(_, '___', None))[0], '__exit__': lambda _, __, *___: _._.get(__, _.__.get(__, lambda *_: None))(_, __, *___)})
19
20
21# Хитро решение
22class ProtectedSection:
23
24 def __init__(self, log=(), suppress=()):
25 self._log = dict(((x, x) for x in log))
26 self._suppress = log + suppress
27 self.exception = None
28
29 def __enter__(self):
30 self.exception = None
31 return self
32
33 def __exit__(self, ex_type, ex_value, _):
34 try:
35 self.exception = self._log.get(ex_type)(ex_value)
36 finally:
37 return ex_type in self._suppress
38
39
40# Турбо решение
41from contextlib import AbstractContextManager
42from inspect import signature
43from itertools import starmap
44
45
46class exception:
47 def __get__(self, instance, _):
48 if instance.ex_type in getattr(instance, 'log', ()):
49 return instance.ex_value
50
51
52def decorate_with(fun):
53 def decorator(fun_to_decorate):
54 def decorated(*args, **kwargs):
55 _, *args_names = signature(fun_to_decorate).parameters.keys()
56 instance, *arg_vals = args
57 fun(instance, **dict(zip(args_names, arg_vals)))
58 return fun_to_decorate(*args, **kwargs)
59 return decorated
60 return decorator
61
62
63class ProtectedSection(AbstractContextManager):
64
65 exception = exception()
66
67 __setter = lambda self, **kwargs: list(starmap(self.__setattr__, kwargs.items())).clear()
68 __init__ = __setter
69
70 @decorate_with(__setter)
71 def __exit__(self, ex_type, ex_value, _):
72 return ex_type in (*getattr(self, 'log', ()), *getattr(self, 'suppress', ()))
..
----------------------------------------------------------------------
Ran 2 tests in 0.001s
OK
| f | 1 | # Адекватно решение | f | 1 | # Адекватно решение |
| 2 | class ProtectedSection: | 2 | class ProtectedSection: | ||
| 3 | 3 | ||||
| 4 | def __init__(self, log=(), suppress=()): | 4 | def __init__(self, log=(), suppress=()): | ||
| 5 | self._log = log | 5 | self._log = log | ||
| 6 | self._suppress = suppress | 6 | self._suppress = suppress | ||
| 7 | self.exception = None | 7 | self.exception = None | ||
| 8 | 8 | ||||
| 9 | def __enter__(self): | 9 | def __enter__(self): | ||
| 10 | return self | 10 | return self | ||
| 11 | 11 | ||||
| 12 | def __exit__(self, ex_type, ex_value, _): | 12 | def __exit__(self, ex_type, ex_value, _): | ||
| n | 13 | self.exception = ex_type(ex_value) if ex_type in self._log else None | n | 13 | self.exception = ex_value if ex_type in self._log else None |
| 14 | return ex_type in (*self._log, *self._suppress) | 14 | return ex_type in (*self._log, *self._suppress) | ||
| 15 | 15 | ||||
| 16 | 16 | ||||
| 17 | # Неадекватно решение | 17 | # Неадекватно решение | ||
| n | 18 | ProtectedSection = type('', (), {'__init__': lambda _, log=(), suppress=(): (setattr(_, '_', {___: (lambda __, ___, ____, _____: setattr(_, '___', ___(____)) or _) for ___ in log}),setattr(_, '__', {_: (lambda *_: True) for _ in suppress}), setattr(_, '___', None), None)[1], '__getattr__': lambda _, __: _.___, '__enter__': lambda _: (_, setattr(_, '___', None))[0], '__exit__': lambda _, __, *___: _._.get(__, _.__.get(__, lambda *_: None))(_, __, *___)}) | n | 18 | ProtectedSection = type('', (), {'__init__': lambda _, log=(), suppress=(): (setattr(_, '_', {___: (lambda __, ___, ____, _____: setattr(_, '___', ____) or _) for ___ in log}),setattr(_, '__', {_: (lambda *_: True) for _ in suppress}), setattr(_, '___', None), None)[1], '__getattr__': lambda _, __: _.___, '__enter__': lambda _: (_, setattr(_, '___', None))[0], '__exit__': lambda _, __, *___: _._.get(__, _.__.get(__, lambda *_: None))(_, __, *___)}) |
| 19 | 19 | ||||
| 20 | 20 | ||||
| 21 | # Хитро решение | 21 | # Хитро решение | ||
| 22 | class ProtectedSection: | 22 | class ProtectedSection: | ||
| 23 | 23 | ||||
| 24 | def __init__(self, log=(), suppress=()): | 24 | def __init__(self, log=(), suppress=()): | ||
| 25 | self._log = dict(((x, x) for x in log)) | 25 | self._log = dict(((x, x) for x in log)) | ||
| 26 | self._suppress = log + suppress | 26 | self._suppress = log + suppress | ||
| 27 | self.exception = None | 27 | self.exception = None | ||
| 28 | 28 | ||||
| 29 | def __enter__(self): | 29 | def __enter__(self): | ||
| 30 | self.exception = None | 30 | self.exception = None | ||
| 31 | return self | 31 | return self | ||
| 32 | 32 | ||||
| 33 | def __exit__(self, ex_type, ex_value, _): | 33 | def __exit__(self, ex_type, ex_value, _): | ||
| 34 | try: | 34 | try: | ||
| 35 | self.exception = self._log.get(ex_type)(ex_value) | 35 | self.exception = self._log.get(ex_type)(ex_value) | ||
| 36 | finally: | 36 | finally: | ||
| 37 | return ex_type in self._suppress | 37 | return ex_type in self._suppress | ||
| 38 | 38 | ||||
| 39 | 39 | ||||
| 40 | # Турбо решение | 40 | # Турбо решение | ||
| 41 | from contextlib import AbstractContextManager | 41 | from contextlib import AbstractContextManager | ||
| 42 | from inspect import signature | 42 | from inspect import signature | ||
| 43 | from itertools import starmap | 43 | from itertools import starmap | ||
| 44 | 44 | ||||
| 45 | 45 | ||||
| 46 | class exception: | 46 | class exception: | ||
| 47 | def __get__(self, instance, _): | 47 | def __get__(self, instance, _): | ||
| 48 | if instance.ex_type in getattr(instance, 'log', ()): | 48 | if instance.ex_type in getattr(instance, 'log', ()): | ||
| t | 49 | return instance.ex_type(instance.ex_value) | t | 49 | return instance.ex_value |
| 50 | 50 | ||||
| 51 | 51 | ||||
| 52 | def decorate_with(fun): | 52 | def decorate_with(fun): | ||
| 53 | def decorator(fun_to_decorate): | 53 | def decorator(fun_to_decorate): | ||
| 54 | def decorated(*args, **kwargs): | 54 | def decorated(*args, **kwargs): | ||
| 55 | _, *args_names = signature(fun_to_decorate).parameters.keys() | 55 | _, *args_names = signature(fun_to_decorate).parameters.keys() | ||
| 56 | instance, *arg_vals = args | 56 | instance, *arg_vals = args | ||
| 57 | fun(instance, **dict(zip(args_names, arg_vals))) | 57 | fun(instance, **dict(zip(args_names, arg_vals))) | ||
| 58 | return fun_to_decorate(*args, **kwargs) | 58 | return fun_to_decorate(*args, **kwargs) | ||
| 59 | return decorated | 59 | return decorated | ||
| 60 | return decorator | 60 | return decorator | ||
| 61 | 61 | ||||
| 62 | 62 | ||||
| 63 | class ProtectedSection(AbstractContextManager): | 63 | class ProtectedSection(AbstractContextManager): | ||
| 64 | 64 | ||||
| 65 | exception = exception() | 65 | exception = exception() | ||
| 66 | 66 | ||||
| 67 | __setter = lambda self, **kwargs: list(starmap(self.__setattr__, kwargs.items())).clear() | 67 | __setter = lambda self, **kwargs: list(starmap(self.__setattr__, kwargs.items())).clear() | ||
| 68 | __init__ = __setter | 68 | __init__ = __setter | ||
| 69 | 69 | ||||
| 70 | @decorate_with(__setter) | 70 | @decorate_with(__setter) | ||
| 71 | def __exit__(self, ex_type, ex_value, _): | 71 | def __exit__(self, ex_type, ex_value, _): | ||
| 72 | return ex_type in (*getattr(self, 'log', ()), *getattr(self, 'suppress', ())) | 72 | return ex_type in (*getattr(self, 'log', ()), *getattr(self, 'suppress', ())) |
| Legends | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
| |||||||||
| f | 1 | # Адекватно решение | f | 1 | # Адекватно решение |
| 2 | class ProtectedSection: | 2 | class ProtectedSection: | ||
| 3 | 3 | ||||
| 4 | def __init__(self, log=(), suppress=()): | 4 | def __init__(self, log=(), suppress=()): | ||
| 5 | self._log = log | 5 | self._log = log | ||
| 6 | self._suppress = suppress | 6 | self._suppress = suppress | ||
| 7 | self.exception = None | 7 | self.exception = None | ||
| 8 | 8 | ||||
| 9 | def __enter__(self): | 9 | def __enter__(self): | ||
| 10 | return self | 10 | return self | ||
| 11 | 11 | ||||
| 12 | def __exit__(self, ex_type, ex_value, _): | 12 | def __exit__(self, ex_type, ex_value, _): | ||
| n | 13 | if ex_type in self._log: | n | 13 | self.exception = ex_type(ex_value) if ex_type in self._log else None |
| 14 | self.exception = ex_type(ex_value) | ||||
| 15 | return ex_type in (*self._log, *self._suppress) | 14 | return ex_type in (*self._log, *self._suppress) | ||
| 16 | 15 | ||||
| 17 | 16 | ||||
| 18 | # Неадекватно решение | 17 | # Неадекватно решение | ||
| n | 19 | ProtectedSection = type('', (), {'__init__': lambda _, log=(), suppress=(): (setattr(_, '_', {___: (lambda __, ___, ____, _____: setattr(_, '___', ___(____)) or _) for ___ in log}),setattr(_, '__', {_: (lambda *_: True) for _ in suppress}), setattr(_, '___', None), None)[1], '__getattr__': lambda _, __: _.___, '__enter__': lambda _: _, '__exit__': lambda _, __, *___: _._.get(__, _.__.get(__, lambda *_: None))(_, __, *___)}) | n | 18 | ProtectedSection = type('', (), {'__init__': lambda _, log=(), suppress=(): (setattr(_, '_', {___: (lambda __, ___, ____, _____: setattr(_, '___', ___(____)) or _) for ___ in log}),setattr(_, '__', {_: (lambda *_: True) for _ in suppress}), setattr(_, '___', None), None)[1], '__getattr__': lambda _, __: _.___, '__enter__': lambda _: (_, setattr(_, '___', None))[0], '__exit__': lambda _, __, *___: _._.get(__, _.__.get(__, lambda *_: None))(_, __, *___)}) |
| 20 | 19 | ||||
| 21 | 20 | ||||
| 22 | # Хитро решение | 21 | # Хитро решение | ||
| 23 | class ProtectedSection: | 22 | class ProtectedSection: | ||
| 24 | 23 | ||||
| 25 | def __init__(self, log=(), suppress=()): | 24 | def __init__(self, log=(), suppress=()): | ||
| 26 | self._log = dict(((x, x) for x in log)) | 25 | self._log = dict(((x, x) for x in log)) | ||
| 27 | self._suppress = log + suppress | 26 | self._suppress = log + suppress | ||
| 28 | self.exception = None | 27 | self.exception = None | ||
| 29 | 28 | ||||
| 30 | def __enter__(self): | 29 | def __enter__(self): | ||
| t | t | 30 | self.exception = None | ||
| 31 | return self | 31 | return self | ||
| 32 | 32 | ||||
| 33 | def __exit__(self, ex_type, ex_value, _): | 33 | def __exit__(self, ex_type, ex_value, _): | ||
| 34 | try: | 34 | try: | ||
| 35 | self.exception = self._log.get(ex_type)(ex_value) | 35 | self.exception = self._log.get(ex_type)(ex_value) | ||
| 36 | finally: | 36 | finally: | ||
| 37 | return ex_type in self._suppress | 37 | return ex_type in self._suppress | ||
| 38 | 38 | ||||
| 39 | 39 | ||||
| 40 | # Турбо решение | 40 | # Турбо решение | ||
| 41 | from contextlib import AbstractContextManager | 41 | from contextlib import AbstractContextManager | ||
| 42 | from inspect import signature | 42 | from inspect import signature | ||
| 43 | from itertools import starmap | 43 | from itertools import starmap | ||
| 44 | 44 | ||||
| 45 | 45 | ||||
| 46 | class exception: | 46 | class exception: | ||
| 47 | def __get__(self, instance, _): | 47 | def __get__(self, instance, _): | ||
| 48 | if instance.ex_type in getattr(instance, 'log', ()): | 48 | if instance.ex_type in getattr(instance, 'log', ()): | ||
| 49 | return instance.ex_type(instance.ex_value) | 49 | return instance.ex_type(instance.ex_value) | ||
| 50 | 50 | ||||
| 51 | 51 | ||||
| 52 | def decorate_with(fun): | 52 | def decorate_with(fun): | ||
| 53 | def decorator(fun_to_decorate): | 53 | def decorator(fun_to_decorate): | ||
| 54 | def decorated(*args, **kwargs): | 54 | def decorated(*args, **kwargs): | ||
| 55 | _, *args_names = signature(fun_to_decorate).parameters.keys() | 55 | _, *args_names = signature(fun_to_decorate).parameters.keys() | ||
| 56 | instance, *arg_vals = args | 56 | instance, *arg_vals = args | ||
| 57 | fun(instance, **dict(zip(args_names, arg_vals))) | 57 | fun(instance, **dict(zip(args_names, arg_vals))) | ||
| 58 | return fun_to_decorate(*args, **kwargs) | 58 | return fun_to_decorate(*args, **kwargs) | ||
| 59 | return decorated | 59 | return decorated | ||
| 60 | return decorator | 60 | return decorator | ||
| 61 | 61 | ||||
| 62 | 62 | ||||
| 63 | class ProtectedSection(AbstractContextManager): | 63 | class ProtectedSection(AbstractContextManager): | ||
| 64 | 64 | ||||
| 65 | exception = exception() | 65 | exception = exception() | ||
| 66 | 66 | ||||
| 67 | __setter = lambda self, **kwargs: list(starmap(self.__setattr__, kwargs.items())).clear() | 67 | __setter = lambda self, **kwargs: list(starmap(self.__setattr__, kwargs.items())).clear() | ||
| 68 | __init__ = __setter | 68 | __init__ = __setter | ||
| 69 | 69 | ||||
| 70 | @decorate_with(__setter) | 70 | @decorate_with(__setter) | ||
| 71 | def __exit__(self, ex_type, ex_value, _): | 71 | def __exit__(self, ex_type, ex_value, _): | ||
| 72 | return ex_type in (*getattr(self, 'log', ()), *getattr(self, 'suppress', ())) | 72 | return ex_type in (*getattr(self, 'log', ()), *getattr(self, 'suppress', ())) |
| Legends | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
| |||||||||