################################################################################ # # File: pythonutils.py # RCS: $Header: /u/testbed/CVSROOT/dldev/src/IBClient/pythonutils.py,v 1.1 1996/09/18 23:38:38 hassan Exp $ # Description: General utilities that improve life with Python even further # Author: Andreas Paepcke, Stanford University # Created: Fri Aug 9 12:31:55 1996 # Modified: Fri Aug 9 13:11:52 1996 (Andreas Paepcke) paepcke@Walrus # Language: Python # Package: N/A # Status: Experimental (Do Not Distribute) # # (c) Copyright 1996, Stanford University, all rights reserved. # ################################################################################ import types # Given an item and a class (not a string, but the class), return 1 if the item # is an instance of that class: def isInstanceOf(item, aClass): if (type(item) == types.InstanceType) and\ (item.__class__ == aClass): return(1) else: return(0) # Return an array of the names of all of aClass' subclasses # (aClass is a class obj, not a str): def allSubclassesOf(module, aClass): subclasses = [] for name, value in eval(module).__dict__.items(): if type(value) == types.ClassType: if aClass in value.__bases__: subclasses.append(name) return(subclasses) # Given a module name and a class name, return the class object. If the module is # not currently loaded, it gets imported: def find_class(module, name): env = {} try: exec 'from %s import %s' % (module, name) in env except ImportError: raise SystemError, \ "Failed to import class %s from module %s" % \ (name, module) klass = env[name] if type(klass) != types.ClassType: raise SystemError, \ "Imported object %s from module %s is not a class" % \ (name, module) return klass