-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathswitch.py
More file actions
29 lines (22 loc) · 828 Bytes
/
switch.py
File metadata and controls
29 lines (22 loc) · 828 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
from typing import Any, MutableSequence, Optional, Union, Type
class Switch:
_value: Any = None
_suppress: MutableSequence[Type[Exception]] = None
def __init__(self, value: Any, *,
suppress: Optional[Union[Type[Exception], MutableSequence[Type[Exception]]]] = None):
self._value = value
if not isinstance(suppress, MutableSequence):
self._suppress = [suppress]
else:
self._suppress = suppress
@property
def value(self) -> Any:
return self._value
def __call__(self, value: Any, *args):
return self._value in (value,) + args
def __enter__(self):
return self
def __exit__(self, exc_type, exc_val, exc_tb):
if exc_type and exc_type in self._suppress:
return True
return False