Created
January 23, 2023 20:04
-
-
Save btknight/572fc886b54a3b947a6f683e24805f16 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from typing import Callable, Hashable | |
class FuncRegistry(dict): | |
"""Creates a registry of hashable objects to function mappings.""" | |
def __init__(self, *args, **kwargs): | |
super().__init__(*args, **kwargs) | |
def register_for(self, key: Hashable) -> Callable: | |
"""Decorator to register functions in the registry. | |
Parameters | |
key: Hashable | |
The key which should point to this function | |
Returns: Callable | |
Returns a decorator that registers the function to the key""" | |
def decorator(fn: Callable) -> Callable: | |
self[key] = fn | |
return fn | |
return decorator |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment