Skip to content

ding.utils.loader.types

ding.utils.loader.types

is_type(type_)

Overview

Create a type loader.

Arguments: - type_ (:obj:type): The type.

to_type(type_)

Overview

Create a type loader.

Arguments: - type_ (:obj:type): The type.

is_callable()

Overview

Create a callable loader.

prop(attr_name)

Overview

Create a attribute loader.

Arguments: - attr_name (:obj:str): The attribute name.

method(method_name)

Overview

Create a method loader.

Arguments: - method_name (:obj:str): The method name.

fcall(*args, **kwargs)

Overview

Create a function loader.

Arguments: - args (:obj:Tuple[Any]): The args. - kwargs (:obj:Dict[str, Any]): The kwargs.

fpartial(*args, **kwargs)

Overview

Create a partial function loader.

Arguments: - args (:obj:Tuple[Any]): The args. - kwargs (:obj:Dict[str, Any]): The kwargs.

Full Source Code

../ding/utils/loader/types.py

1from functools import partial 2 3from .base import Loader, ILoaderClass, _reset_exception 4from .utils import check_only 5 6 7def is_type(type_: type) -> ILoaderClass: 8 """ 9 Overview: 10 Create a type loader. 11 Arguments: 12 - type_ (:obj:`type`): The type. 13 """ 14 15 if isinstance(type_, type): 16 return Loader(type_) 17 else: 18 raise TypeError('Type variable expected but {actual} found.'.format(actual=repr(type(type_).__name__))) 19 20 21def to_type(type_: type) -> ILoaderClass: 22 """ 23 Overview: 24 Create a type loader. 25 Arguments: 26 - type_ (:obj:`type`): The type. 27 """ 28 29 return Loader(lambda v: type_(v)) 30 31 32def is_callable() -> ILoaderClass: 33 """ 34 Overview: 35 Create a callable loader. 36 """ 37 38 return _reset_exception( 39 check_only(prop('__call__')), 40 lambda v, e: TypeError('callable expected but {func} not found'.format(func=repr('__call__'))) 41 ) 42 43 44def prop(attr_name: str) -> ILoaderClass: 45 """ 46 Overview: 47 Create a attribute loader. 48 Arguments: 49 - attr_name (:obj:`str`): The attribute name. 50 """ 51 52 return Loader( 53 ( 54 lambda v: hasattr(v, attr_name), lambda v: getattr(v, attr_name), 55 AttributeError('attribute {name} expected but not found'.format(name=repr(attr_name))) 56 ) 57 ) 58 59 60def method(method_name: str) -> ILoaderClass: 61 """ 62 Overview: 63 Create a method loader. 64 Arguments: 65 - method_name (:obj:`str`): The method name. 66 """ 67 68 return _reset_exception( 69 prop(method_name) >> is_callable(), lambda v, e: 70 TypeError('type {type} not support function {func}'.format(type=repr(type(v).__name__), func=repr('__iter__'))) 71 ) 72 73 74def fcall(*args, **kwargs) -> ILoaderClass: 75 """ 76 Overview: 77 Create a function loader. 78 Arguments: 79 - args (:obj:`Tuple[Any]`): The args. 80 - kwargs (:obj:`Dict[str, Any]`): The kwargs. 81 """ 82 83 return Loader(lambda v: v(*args, **kwargs)) 84 85 86def fpartial(*args, **kwargs) -> ILoaderClass: 87 """ 88 Overview: 89 Create a partial function loader. 90 Arguments: 91 - args (:obj:`Tuple[Any]`): The args. 92 - kwargs (:obj:`Dict[str, Any]`): The kwargs. 93 """ 94 95 return Loader(lambda v: partial(v, *args, **kwargs))