ding.interaction.base.common¶
ding.interaction.base.common
¶
ControllableContext
¶
Overview
Basic context-supported class structure
Example: - Common usage
>>> c = MyControllableContext() # One of the superclasses if ControllableContext
>>> c.start()
>>> try:
>>> pass # do anything you like
>>> finally:
>>> c.close()
- Use with keyword (the same as code above)
>>> c = MyControllableContext() # One of the superclasses if ControllableContext
>>> with c as cc: # cc is c, have the same id
>>> pass # do anything you like
ControllableService
¶
Bases: ControllableContext
Overview
Controllable service with context support, usually has concurrent feature.
Example: - A common usage
>>> c = MyControllableService() # One of its superclasses is ControllableService
>>> c.start()
>>> try:
>>> pass # do anything you like
>>> finally:
>>> c.shutdown() # shutdown the service
>>> c.join() # wait until service is down
- Use with keyword (the same as code above)
>>> c = MyControllableService() # One of its superclasses is ControllableService
>>> with c as cc: # cc is c, have the same id
>>> pass # do anything you like
random_token(length=None)
¶
Overview
Generate random hex token
Arguments:
- length (:obj:Optional[int]): Length of the random token (None means 64)
Returns:
- token (:obj:str): Generated random token
Example:
>>> random_token() # '4eAbd5218e3d0da5e7AAFcBF48Ea0Df2dadED1bdDF0B8724FdE1569AA78F24A7'
>>> random_token(24) # 'Cd1CdD98caAb8602ac6501aC'
translate_dict_func(d)
¶
Overview
Transform dict with funcs to function generating dict.
Arguments:
- d (:obj:Mapping[str, Callable[..., Any]]): Dict with funcs
Returns:
- func (:obj:Callable[..., Dict[str, Any]]): Function generating dict
Example:
>>> f1 = lambda x, y: x + y
>>> f2 = lambda x, y: x - y
>>> f3 = lambda x, y: x * y
>>> fx = translate_dict_func({'a': f1, 'b': f2, 'c': f3})
>>> fx(2, 3) # {'a': 5, 'b': -1, 'c': 6}
>>> fx(5, 11) # ('a': 16, 'b': -6, 'c': 55}
default_func(return_value=None)
¶
Overview
Transform optional function (maybe None) to function with default value
Argument:
- return_value (:obj:): Return value of the default function
Returns:
- decorator (:obj:Callable[[Callable[..., Any]], Callable[..., Any]]): A decorator function that can decorator optional function to real function (must be not None)
Example:
>>> f1 = None
>>> f2 = lambda x, y: x + y
>>> ff1 = default_func()(f1)
>>> ft1 = default_func(0)(f1)
>>> ff2 = default_func()(f2)
>>> ff1(2, 3) # None
>>> ft1(2, 3) # 0
>>> ff2(2, 3) # 5
Full Source Code
../ding/interaction/base/common.py