def registry(attr, base=type):
class Registry(base):<EOL><INDENT>def __init__(cls, name, bases, attrs):<EOL><INDENT>super(Registry, cls).__init__(name, bases, attrs)<EOL>if not hasattr(cls, '<STR_LIT>'):<EOL><INDENT>cls.__registry__ = {}<EOL><DEDENT>key = getattr(cls, attr)<EOL>if key is not NotImplemented:<EOL><INDENT>assert key not in cls.__registry__<EOL>cls.__registry__[key] = cls<EOL><DEDENT><DEDENT>def __dispatch__(cls, key):<EOL><INDENT>try:<EOL><INDENT>return cls.__registry__[key]<EOL><DEDENT>except KeyError:<EOL><INDENT>raise ValueError('<STR_LIT>' % (attr, key))<EOL><DEDENT><DEDENT><DEDENT>return Registry<EOL>
Generates a meta class to index sub classes by their keys.