<s> from pydoc import locate <EOL> import types <EOL> from rllab . misc . ext import iscanr <EOL> def classesinmodule ( module ) : <EOL> md = module . __dict__ <EOL> return [ <EOL> md [ c ] for c in md if ( <EOL> isinstance ( md [ c ] , type ) and md [ c ] . __module__ == module . __name__ <EOL> ) <EOL> ] <EOL> def locate_with_hint ( class_path , prefix_hints = [ ] ) : <EOL> module_or_class = locate ( class_path ) <EOL> if module_or_class is None : <EOL> hint = "<STR_LIT:.>" . join ( prefix_hints ) <EOL> module_or_class = locate ( hint + "<STR_LIT:.>" + class_path ) <EOL> return module_or_class <EOL> def load_class ( class_path , superclass = None , prefix_hints = [ ] ) : <EOL> module_or_class = locate_with_hint ( class_path , prefix_hints ) <EOL> if module_or_class is None : <EOL> raise ValueError ( "<STR_LIT>" % class_path ) <EOL> if type ( module_or_class ) == types . ModuleType : <EOL> if superclass : <EOL> classes = filter ( lambda x : issubclass ( x , superclass ) , classesinmodule ( module_or_class ) ) <EOL> if len ( classes ) == <NUM_LIT:0> : <EOL> if superclass : <EOL> raise ValueError ( '<STR_LIT>' % ( str ( superclass ) , class_path ) ) <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' % ( class_path ) ) <EOL> elif len ( classes ) > <NUM_LIT:1> : <EOL> if superclass : <EOL> raise ValueError ( '<STR_LIT>' % ( str ( superclass ) , class_path ) ) <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' % ( class_path ) ) <EOL> else : <EOL> return classes [ <NUM_LIT:0> ] <EOL> elif isinstance ( module_or_class , type ) : <EOL> if superclass is None or issubclass ( module_or_class , superclass ) : <EOL> return module_or_class <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' % ( str ( module_or_class ) , str ( superclass ) ) ) <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' % str ( module_or_class ) ) </s>
