neuralib.argp.dispatch

Value dispatch function

author:

Ta-Shun Su

Work with AbstractParser.

>>> from neuralib.argp import AbstractParser
>>> class Test(AbstractParser, DispatchOption):
...     target: str = DispatchOption.argument(
...         '--run'
...     )
...
...     @classmethod
...     def EPILOG(cls): # build parser epilog
...         return f'''... Command (--run)
... {textwrap.indent(Test.parser_command_epilog(), "  ")}
... '''
...
...     def run(self):
...         # dispaatch function call according to self.target
...         self.invoke_command(self.target)
...
...     @DispatchOption.dispatch('A')
...     def run_a(self):
...         '''doc A.'''
...         print('a')
...     @DispatchOption.dispatch('B')
...     def run_b(self):
...         '''doc B'''
...         print('b')
neuralib.argp.dispatch.dispatch(command, alias=None, group=None)[source]

A decorator that mark a function a dispatch target function.

All functions decorated in same dispatch group should have save function signature (at least for non-default parameters). For example:

>>> @dispatch('A')
... def function_a(self, a, b, c=None):
...     pass
... @dispatch('B')
... def function_b(self, a, b, d=None):
...     pass
... def run_function(self):
...     invoke_command(self, 'A', a, b)
Parameters:
  • command (str)

  • alias (str | list[str] | None)

  • group (str | None)

neuralib.argp.dispatch.list_commands(host, group=<object object>)[source]

list all dispatch-decoratored function info in host.

Parameters:
  • host (T | Type[T])

  • group (str | None) – dispatch group.

Returns:

list of DispatchCommand

Return type:

list[DispatchCommand]

neuralib.argp.dispatch.find_command(host, command, group=<object object>)[source]

find dispatch-decoratored function in host according to command.

Parameters:
  • host (T)

  • command (str) – command or command alias

  • group (str | None) – dispatch group

Returns:

found DispatchCommand

Return type:

DispatchCommand | None

neuralib.argp.dispatch.invoke_command(host, command, *args, **kwargs)[source]

invoke a dispatch-decoratored function in default group.

Parameters:
  • host (T)

  • command (str) – command or command alias

  • args – dispatch-decoratored function positional arguments

  • kwargs – dispatch-decoratored function keyword arguments

Returns:

function return

Raises:

DispatchCommandNotFound

Return type:

Any

neuralib.argp.dispatch.invoke_group_command(host, group, command, *args, **kwargs)[source]

invoke a dispatch-decoratored function in certain group.

Parameters:
  • host (T)

  • group (str) – dispatch group

  • command (str) – command or command alias

  • args – dispatch-decoratored function positional arguments

  • kwargs – dispatch-decoratored function keyword arguments

Returns:

function return

Raises:

DispatchCommandNotFound

Return type:

Any

class neuralib.argp.dispatch.DispatchOption[source]
argument

alias of DispatchArgument

list_commands(group=<object object>)[source]

list all dispatch-decoratored function info in host.

Parameters:

group (str | None) – dispatch group.

Returns:

list of DispatchCommand

Return type:

list[DispatchCommand]

find_command(command, group=<object object>)[source]

find dispatch-decoratored function in host according to command.

Parameters:
  • command (str) – command or command alias

  • group (str | None) – dispatch group

Returns:

found DispatchCommand

Return type:

DispatchCommand | None

invoke_command(command, *args, **kwargs)[source]

invoke a dispatch-decoratored function in default group.

Parameters:
  • command (str) – command or command alias

  • args – dispatch-decoratored function positional arguments

  • kwargs – dispatch-decoratored function keyword arguments

Returns:

function return

Raises:

DispatchCommandNotFound

Return type:

Any

invoke_group_command(group, command, *args, **kwargs)[source]

invoke a dispatch-decoratored function in certain group.

Parameters:
  • group (str) – dispatch group

  • command (str) – command or command alias

  • args – dispatch-decoratored function positional arguments

  • kwargs – dispatch-decoratored function keyword arguments

Returns:

function return

Raises:

DispatchCommandNotFound

Return type:

Any

classmethod dispatch(command, alias=None, group=None)[source]
Parameters:
  • command (str)

  • alias (str | list[str] | None)

  • group (str | None)

classmethod parser_command_epilog(group=<object object>)[source]
Parameters:

group (str | None)

Return type:

str

exception neuralib.argp.dispatch.DispatchCommandNotFound[source]
__init__(command, group=None)[source]
Parameters:
  • command (str)

  • group (str | None)