Home

I have some functions I need to create at runtime. The way I'm creating
them is by calling a function which returns the string representation.
Then I exec the string.

Here's the code I use to gen the strings:
mkfactfns.py
-----------
import new
def mkfactfns( cname ):
def auxgen( name, params, dd ):
v1 = ( """def mk%s%sdict(%s):\n"""%(name, cname, params)
+
""" print 'In mk%s%sdict'\n"""%(name, cname)
+
""" return %s\n"""%dd)
v2 = ( """def mk%s%s(%s):\n"""%(name,cname,params)
+
""" print 'Calling mk%s%s'\n"""%(name,cname)
+
""" return %s%s( mk%s%sdict(%s) )\n"""%(name,cname,
name, cname, params))
return v1, v2
return auxgen

This is the caller of mkfactfns
--------------------------------
import mkfactfns
fbase = "ABC" # Factory BASEname
auxfactgen = mkfactfns.mkfactfns(fbase)

Then in the same module, I call auxfactgen
via
mkfactfns.AuxFnDefs( auxfndata, auxfactgen, globals(), "ABC" )

def AuxFnDefs(auxfndata, fnmaker, globs, cname ):
dictsuff = ('dict','')
for ii in auxfndata:
rr = fnmaker( *ii )
for jj in range( 2 ):
co = compile (rr[jj], '', 'exec')
exec co
name = 'mk%s%s%s'%(ii[0],cname,dictsuff[jj])
print 'co = ', co, 'name = ', name
nfunc = new.function( co, globs, name )
print 'Just added mk%s%s%s'%(ii[0],cname,dictsuff[jj])
globs['mk%s%s%s'%(ii[0],cname,dictsuff[jj])] = nfunc
print 'g = ', globs['mk%s%s%s'%(ii[0],cname,dictsuff[jj])]

Everything works just fine (that I know of) except that when I run a
function that takes 1 arg, I get the following message:

TypeError: ?() takes no arguments (1 given)

even though I know that I am passing one arg. I must be doing something
wrong, I just don't know what. :-(

--
Time flies like the wind. Fruit flies like a banana. Stranger things have .0.
happened but none stranger than this. Does your driver's license say Organ ..0
Donor?Black holes are where God divided by zero. Listen to me! We are all- 000
individuals! What if this weren't a hypothetical question?
steveo at syslang.net

previous
next

Re: error: conflicts with new declaration with ā€˜C’ linkage
Re: i am new to python-Please somebody help
assigning a string, with embedded nulls, to an ADO variant_t parm
Re: what libraries/classes to use for precision significant calculations?
Re: Parsing nested constructs
Dzieci Niczyje
Akogo
Podaruj Zycie
Fundacja Hobbit
Rodzic Po Ludzku