Skip to content

Instantly share code, notes, and snippets.

@btknight
Created January 23, 2023 20:04
Show Gist options
  • Save btknight/572fc886b54a3b947a6f683e24805f16 to your computer and use it in GitHub Desktop.
Save btknight/572fc886b54a3b947a6f683e24805f16 to your computer and use it in GitHub Desktop.
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