Method dispatcher

Dispatcher is used to add methods (functions) to the server.

For usage examples see Dispatcher.add_method()

class jsonrpc.dispatcher.Dispatcher(prototype=None)[source]

Dictionary like object which maps method_name to method.

__init__(prototype=None)[source]

Build method dispatcher.

Parameters

prototype (object or dict, optional) – Initial method mapping.

Examples

Init object with method dictionary.

>>> Dispatcher({"sum": lambda a, b: a + b})
None
add_method(f=None, name=None)[source]

Add a method to the dispatcher.

Parameters
  • f (callable) – Callable to be added.

  • name (str, optional) – Name to register (the default is function f name)

Notes

When used as a decorator keeps callable object unmodified.

Examples

Use as method

>>> d = Dispatcher()
>>> d.add_method(lambda a, b: a + b, name="sum")
<function __main__.<lambda>>

Or use as decorator

>>> d = Dispatcher()
>>> @d.add_method
    def mymethod(*args, **kwargs):
        print(args, kwargs)

Or use as a decorator with a different function name >>> d = Dispatcher() >>> @d.add_method(name=”my.method”)

def mymethod(*args, **kwargs):

print(args, kwargs)

build_method_map(prototype, prefix='')[source]

Add prototype methods to the dispatcher.

Parameters
  • prototype (object or dict) – Initial method mapping. If given prototype is a dictionary then all callable objects will be added to dispatcher. If given prototype is an object then all public methods will be used.

  • prefix (string, optional) – Prefix of methods