473,545 Members | 2,451 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problems with dynamic function creation.

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,p arams)
+
""" print 'Calling mk%s%s'\n"""%(n ame,cname)
+
""" return %s%s( mk%s%sdict(%s) )\n"""%(name,cn ame,
name, cname, params))
return v1, v2
return auxgen

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

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

def AuxFnDefs(auxfn data, 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
2 1518
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

54
4512
by: tshad | last post by:
I have a function: function SalaryDisplay(me) { var salaryMinLabel = document.getElementById("SalaryMin"); salaryMinLabel.value = 200; alert("after setting salaryMinLabel = " + salaryMinLabel.value); } I also have an asp.net object:
1
2129
by: andrew queisser | last post by:
I've been trying to dynamically create a class DevT that's derived from a generic base GenBase<T>. It doesn't seem to work. I'm attaching a code sample below that illustrates the problem. CreateType() fails when the base class is a parametrized class, as in DevT : GenBase<int>. CreateType() works if the base class is not parametrized, even...
15
3022
by: rwf_20 | last post by:
I just wanted to throw this up here in case anyone smarter than me has a suggestion/workaround: Problem: I have a classic producer/consumer system which accepts 'commands' from a socket and 'executes' them. Obviously, each different command (there are ~20 currently) has its own needed functionality. The dream goal here would be to...
2
1034
by: arun.hallan | last post by:
I had the problem of accessing controls that i had added on the fly. I had a look at the following webpage: http://www.codeproject.com/aspnet/retainingstate.asp , which worked. However i have the following problem: depending on what item a user selects from a combo box, the "NumberOfControls" variable changes. What i want to happen is...
0
2057
by: Pascal Costanza | last post by:
Dynamic Languages Day @ Vrije Universiteit Brussel ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Monday, February 13, 2006, VUB Campus Etterbeek The VUB (Programming Technology Lab, System and Software Engineering Lab), ULB (deComp) and the Belgian Association for Dynamic Languages (BADL) are very pleased to invite you to a whole...
13
4692
by: xian_hong2046 | last post by:
Hello, I think dynamic memory allocation is supposed to be used when one doesn't know in advance how much memory to allocate until run time. An example from Thinking in C++ is to dynamically create an array (using "new") since one doesn't know it size when writing the program. However, it looks to me that the size information must come...
4
5055
by: hobbes992 | last post by:
Howdy folks, I've been working on a c project, compiling using gcc, and I've reached a problem. The assignment requires creation of a two-level directory file system. No files have to be added or deleted, however it must be initialized by a function during run-time to contain so many users which each contain so many directories of which each...
2
5003
by: jmgopi | last post by:
Hi: Can somebody provide me samples on how to create a dynamic CollapsiblePanel using ASP.NET AJAX Toolkit. Any points are highly appreciated. Thanks, GJM
1
4880
by: cdmsenthil | last post by:
I have an Infragistics UltrawebGrid . Each Row in the grid is attached to a context menu using Infragistics CSOM Upon click on the menu, I am creating an Iframe dynamically which points to another page in the same domain which also contains infragistics datagid populated with default data retrieved from Data Base. After creating the frame I...
0
7680
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7934
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
5349
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4966
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3476
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3459
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1908
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1033
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
731
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.