By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,812 Members | 1,647 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,812 IT Pros & Developers. It's quick & easy.

Problems with dynamic function creation.

P: n/a
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
Aug 22 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Steven W. Orr wrote:

[snip mess]
Everything works just fine (that I know of) except
Sometimes you have to just fix the code you have. This however doesn't just
smell funny, it's hopeless. Start with clearly stating your goals and then
do a rewrite.

Should you need help again, remember post a self-contained example and an
actual traceback.

Peter

Aug 22 '07 #2

P: n/a
Peter Otten wrote:
Steven W. Orr wrote:

[snip mess]
>Everything works just fine (that I know of) except

Sometimes you have to just fix the code you have. This however doesn't just
smell funny, it's hopeless. Start with clearly stating your goals and then
do a rewrite.

Should you need help again, remember post a self-contained example and an
actual traceback.
Just in case you don't find Peter's advice particularly constructive,
please allow me to point out that he is doing you a favor. You will get
much better advice if you ask this group how to achieve your real ands,
rather than how to implement a possibly half-baked solution that you
have already conceived.

In other words (in case an analogy will help), your position is a little
like that of an amateur shipbuilder who has decided to make his first
steel ship, and is trying to hold the hull plates together by banging
nails through two overlapping plates into pieces of wood behind them.

So you are now asking "what kind of nails should I use to keep the hull
from falling apart?" when you should be asking "what do I need to do to
hold the hull together?". This latter question could be answered by some
people who wanted to explain riveting technology to you, whereas others
could try and show you that welding, while a little more difficult to
achieve, gave a truly professional result. You could then take your pick
of the most suitable solutions.

At the moment you appear to be the man with a hammer who is seeing all
problems as nails.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
--------------- Asciimercial ------------------
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
----------- Thank You for Reading -------------

Aug 23 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.