def meta_command(name, bases, attrs):
 commands = {}
 docs = {}
 for (attr, value) in attrs.items():
  if getattr(value, '__command__', False):
   commands[attr] = value
   docs[attr] = (getattr(value, '__doc__', None) or ('perform the %s command' % attr)).strip()
 attrs['__commands__'] = commands
 attrs['__docs__'] = docs
 def run(self, command, *args, **kwargs):
  return self.__commands__[command](self, *args, **kwargs)
 attrs.setdefault('run', run)
 return type(name, bases, attrs)

def meta_command ( name , bases , attrs ) : commands = { } docs = { } for ( attr , value ) in attrs . items ( ) : if getattr ( value , '__command__' , False ) : commands [ attr ] = value docs [ attr ] = ( getattr ( value , '__doc__' , None ) or ( 'perform the %s command' % attr ) ) . strip ( ) attrs [ '__commands__' ] = commands attrs [ '__docs__' ] = docs def run ( self , command , * args , ** kwargs ) : return self . __commands__ [ command ] ( self , * args , ** kwargs ) attrs . setdefault ( 'run' , run ) return type ( name , bases , attrs )
look for attrs with a truthy attribute __command__ and add them to an attribute __commands__ on the type that maps names to decorated methods .