-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathvaluedispatch.py
More file actions
50 lines (42 loc) · 1.28 KB
/
valuedispatch.py
File metadata and controls
50 lines (42 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# -*- coding: utf-8 -*-
"""
valuedispatch
~~~~~~~~~~~~~
:mod:`valuedispatch`-like API but dispatches value instead of type.
:copyright: (c) 2015-2016 by What! Studio
:license: BSD, see LICENSE for more details.
"""
from functools import update_wrapper
__version__ = '0.0.1'
__all__ = ['valuedispatch']
# Import or define :class:`MappingProxyType`.
try:
from singledispatch_helpers import MappingProxyType
except ImportError:
try:
from collections import UserDict
except ImportError:
from UserDict import UserDict
class MappingProxyType(UserDict):
def __init__(self, data):
UserDict.__init__(self)
self.data = data
def valuedispatch(func):
"""Decorates a function to dispatch handler of the value of the first
argument.
"""
registry = {}
def dispatch(value):
return registry.get(value, func)
def register(value, func=None):
if func is None:
return lambda f: register(value, f)
registry[value] = func
return func
def wrapper(*args, **kw):
return dispatch(args[0])(*args, **kw)
wrapper.register = register
wrapper.dispatch = dispatch
wrapper.registry = MappingProxyType(registry)
update_wrapper(wrapper, func)
return wrapper