Simple single dispatch.
| 708 | |
| 709 | |
| 710 | class Dispatch: |
| 711 | """Simple single dispatch.""" |
| 712 | |
| 713 | def __init__(self, name=None): |
| 714 | self._lookup = {} |
| 715 | self._lazy = {} |
| 716 | if name: |
| 717 | self.__name__ = name |
| 718 | |
| 719 | def register(self, type, func=None): |
| 720 | """Register dispatch of `func` on arguments of type `type`""" |
| 721 | |
| 722 | def wrapper(func): |
| 723 | if isinstance(type, tuple): |
| 724 | for t in type: |
| 725 | self.register(t, func) |
| 726 | else: |
| 727 | self._lookup[type] = func |
| 728 | return func |
| 729 | |
| 730 | return wrapper(func) if func is not None else wrapper |
| 731 | |
| 732 | def register_lazy(self, toplevel, func=None): |
| 733 | """ |
| 734 | Register a registration function which will be called if the |
| 735 | *toplevel* module (e.g. 'pandas') is ever loaded. |
| 736 | """ |
| 737 | |
| 738 | def wrapper(func): |
| 739 | self._lazy[toplevel] = func |
| 740 | return func |
| 741 | |
| 742 | return wrapper(func) if func is not None else wrapper |
| 743 | |
| 744 | def dispatch(self, cls): |
| 745 | """Return the function implementation for the given ``cls``""" |
| 746 | lk = self._lookup |
| 747 | if cls in lk: |
| 748 | return lk[cls] |
| 749 | for cls2 in cls.__mro__: |
| 750 | # Is a lazy registration function present? |
| 751 | try: |
| 752 | toplevel, _, _ = cls2.__module__.partition(".") |
| 753 | except Exception: |
| 754 | continue |
| 755 | try: |
| 756 | register = self._lazy[toplevel] |
| 757 | except KeyError: |
| 758 | pass |
| 759 | else: |
| 760 | register() |
| 761 | self._lazy.pop(toplevel, None) |
| 762 | meth = self.dispatch(cls) # recurse |
| 763 | lk[cls] = meth |
| 764 | lk[cls2] = meth |
| 765 | return meth |
| 766 | try: |
| 767 | impl = lk[cls2] |
no outgoing calls
searching dependent graphs…