Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def methdispatch(func):
dispatcher = singledispatch(func)
def wrapper(*args, **kw):
try:
return dispatcher.dispatch(args[1].__class__)(*args, **kw)
except IndexError:
return dispatcher.dispatch(args[0].__class__)(*args, **kw)
wrapper.register = dispatcher.register
update_wrapper(wrapper, func)
return wrapper
@singledispatch
def tosexp(obj, **kwds):
"""
Convert an object to an S-expression (`dumps` is just calling this).
See this table for comparison of lispy languages, to support them
as much as possible:
`Lisp: Common Lisp, Scheme/Racket, Clojure, Emacs Lisp - Hyperpolyglot
`_
Most classes can be supported by tosexp() by adding a __to_lisp_as__ method
that returns a restructuring of an instance. The method can use builtin
types, sexpdata hinting classes, and instances of classes that have
tosexp() support.
Methods that require customizing the recursion or output string of tosexp()
should be registered with @sexpdata.tosexp.register(). Also the default
@singledispatch
def dds(var, level=0, sequence=0):
"""Single dispatcher that generates the DDS response."""
raise StopIteration
@singledispatch
def augment_node(node, opts):
raise Exception('Failed to augment imports for %s' % node)
def __init__(self):
self.routes = {
'GET': defaultdict(dict),
'POST': defaultdict(dict),
'PUT': defaultdict(dict),
'DELETE': defaultdict(dict),
'OPTIONS': defaultdict(dict),
'PATCH': defaultdict(dict),
'HEAD': defaultdict(dict)
}
self._prepare_response = singledispatch(self._prepare_response)
self._prepare_response.register(str, self._prepare_response_str)
self._prepare_response.register(
response.Response, self._prepare_response_object)
super(Router, self).__init__()
@singledispatch
def ascii(var, printname=True):
"""A single dispatcher for the ASCII response."""
raise StopIteration
@singledispatch
def fmap(functor, func):
# type: (Any, Callable[[Any], Any]) -> Any
'''Applies a function to the data 'inside' a functor.
Uses functools.singledispatch so you can write your own functors
for use with the library.'''
return functor.map(func)
@singledispatch
def pretty_print_node(node, cx):
raise Exception('Pretty printing failed at %s' % node)
@singledispatch
def notification_log_mutator(event, initial):
return entity_mutator(event, initial)
def generate_dispatches(self):
"""Generate singledispatches
"""
self.add_template_paths = singledispatch(self.add_template_paths)
self.add_template_paths.register(str, self._add_template_paths_str)
self.add_template_paths.register(list, self._add_template_paths_list)
self.add_template_paths.register(tuple, self._add_template_paths_tuple)