Dynamically add Class to Modules

callmebill at gmail.com callmebill at gmail.com
Fri Dec 9 02:21:43 CET 2005


Hi Michael... It didn't seem to take.  Here is some of the actual code:

[[[[[[[[[[ from the runner ]]]]]]]]]]]]]
                        print "+++++++++++++++++++++++++++++++++++++++"
                        print "::Dir before exec:",dir(testModule)
                        import CodeGenBase
                        if hasattr( testModule,"TheTestCode" ):
                                print testModule.TheTestCode
%(testModule.TheTestName,

testModule.TheTestName )
                                exec testModule.TheTestCode
%(testModule.TheTestName,

testModule.TheTestName )

                        else:
                                print "PASSING"
                        print "::Dir after exec:",dir( testModule )
                        print "+++++++++++++++++++++++++++++++++++++++"

[[[[[[[[[[[[[[[[[[[ from the test file ]]]]]]]]]]]]]]]]]]]]
TheTestName = "FOO_TEST_NAME"
TheTestCode = \
"""class %sTestCase( CodeGenBase.CodeGenBase ):
    def __init__( self,methodName ):
        CodeGenBase.CodeGenBase.__init__( self,methodName )

    def test%s(self):
        self.SetupAndRunConfigTxtTests()

"""


.....It doesn't look like the new class is sticking.  Below is some
output:
+++++++++++++++++++++++++++++++++++++++
::Dir before exec: ['BuildRunBase', 'CodeGenBase', 'ConfigHelper',
'TheTestCode'
, 'TheTestName', '__builtins__', '__doc__', '__file__', '__name__',
'isSupported
', 'os', 'sys']
class FOO_TEST_NAMETestCase( CodeGenBase.CodeGenBase ):
    def __init__( self,methodName ):
        CodeGenBase.CodeGenBase.__init__( self,methodName )

    def testFOO_TEST_NAME(self):
        self.SetupAndRunConfigTxtTests()


::Dir after exec: ['BuildRunBase', 'CodeGenBase', 'ConfigHelper',
'TheTestCode',
 'TheTestName', '__builtins__', '__doc__', '__file__', '__name__',
'isSupported'
, 'os', 'sys']
+++++++++++++++++++++++++++++++++++++++



Hopefully I just missed something obvious, which happens all too often.
 Any ideas?

Thanks again for the help!




More information about the Python-list mailing list