OXIESEC PANEL
- Current Dir:
/
/
opt
/
alt
/
python311
/
lib
/
python3.11
/
site-packages
/
typish
/
classes
Server IP: 2a02:4780:11:1084:0:327f:3464:10
Upload:
Create Dir:
Name
Size
Modified
Perms
📁
..
-
05/08/2024 06:42:59 PM
rwxr-xr-x
📄
__init__.py
0 bytes
05/08/2024 06:42:59 PM
rw-r--r--
📁
__pycache__
-
05/08/2024 06:42:59 PM
rwxr-xr-x
📄
_cls_dict.py
1.89 KB
05/08/2024 06:42:59 PM
rw-r--r--
📄
_cls_function.py
2.92 KB
05/08/2024 06:42:59 PM
rw-r--r--
📄
_literal.py
2.54 KB
05/08/2024 06:42:59 PM
rw-r--r--
📄
_something.py
4.77 KB
05/08/2024 06:42:59 PM
rw-r--r--
📄
_subscriptable_type.py
1.59 KB
05/08/2024 06:42:59 PM
rw-r--r--
📄
_union_type.py
185 bytes
05/08/2024 06:42:59 PM
rw-r--r--
Editing: _cls_dict.py
Close
from collections import OrderedDict from typing import Optional, Any class ClsDict(OrderedDict): """ ClsDict is a dict that accepts (only) types as keys and will return its values depending on instance checks rather than equality checks. """ def __new__(cls, *args, **kwargs): """ Construct a new instance of ``ClsDict``. :param args: a dict. :param kwargs: any kwargs that ``dict`` accepts. :return: a ``ClsDict``. """ from typish.functions._is_type_annotation import is_type_annotation if len(args) > 1: raise TypeError('TypeDict accepts only one positional argument, ' 'which must be a dict.') if args and not isinstance(args[0], dict): raise TypeError('TypeDict accepts only a dict as positional ' 'argument.') if not all([is_type_annotation(key) for key in args[0]]): raise TypeError('The given dict must only hold types as keys.') return super().__new__(cls, args[0], **kwargs) def __getitem__(self, item: Any) -> Any: """ Return the value of the first encounter of a key for which ``is_instance(item, key)`` holds ``True``. :param item: any item. :return: the value of which the type corresponds with item. """ from typish.functions._get_type import get_type from typish.functions._subclass_of import subclass_of item_type = get_type(item, use_union=True) for key, value in self.items(): if subclass_of(item_type, key): return value raise KeyError('No match for {}'.format(item)) def get(self, item: Any, default: Any = None) -> Optional[Any]: try: return self.__getitem__(item) except KeyError: return default