Re: [Tutor] Dynamically naming functions
On 26/03/06, Kent Johnson [EMAIL PROTECTED] wrote: Ed Singleton wrote: How does one go about creating functions, classes, or callable objects when you don't know their name in advance? (For example you want to read their names in from a text file or database). I want to use this in a few different places. For example Faces, the Python Project Management Planner Tool Thingy, uses nested functions to put tasks within a project: def MyProject(): start = 2006-03-06 resource = Me def Task1(): start = 2006-03-13 def Task2(): effort = 1w I'd like to load these from a database (using SQLObject), but I'm not sure how I can define the name of the function from a filed in a database (or read in from a text file). Hi Ed, I was just wondering how this came out - did you find a way to generate these functions dynamically? Or change Faces? Or give up? I just temporarily delayed the problem. I'm going to have a look at trying to change the Faces code to use dictionaries as per your suggestion, but it would a big task for me (my biggest so far probably). (I have a new job that lets me do everything in Python, but sometimes they direct my efforts towards a particular task, which delays my working on interesting things). Ed ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Dynamically naming functions
Ed Singleton wrote: How does one go about creating functions, classes, or callable objects when you don't know their name in advance? (For example you want to read their names in from a text file or database). I want to use this in a few different places. For example Faces, the Python Project Management Planner Tool Thingy, uses nested functions to put tasks within a project: def MyProject(): start = 2006-03-06 resource = Me def Task1(): start = 2006-03-13 def Task2(): effort = 1w I'd like to load these from a database (using SQLObject), but I'm not sure how I can define the name of the function from a filed in a database (or read in from a text file). Hi Ed, I was just wondering how this came out - did you find a way to generate these functions dynamically? Or change Faces? Or give up? Thanks, Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Dynamically naming functions
Very rarely, its easy to do backups of huge amounts of data if you know where to find it, its hard to trawl all over a changing structure looking for the things that need backing up. Particularly if, when you try to restore it, it needs to go in a different place to where you found it! Backing up is generally one of the lesser of my worries. Backing up is usually a no brainer, especially in a static structure, but even in a dynamic one you can just do a find on the file types. It's restoring a dynamic structure thats really hard, and usually a manual process... Maintaining content is primary. Oddly enough I usually expect content maintenance to be a 'business as usual' kind of thing. The content goes in one place per author and the code picks it up as needed. Unless you are dynamically generating static pages, they are a nightmare to maintain. If the content is dynamic in any way I always generate them dynamically - and that includes anything with company logo etc on it. About the only pure html pages I use are the Error reports - 404s etc... be quite repetitive. A large amount of the code on any page is the same as on any other page (page structure, boilerplate, etc). But most of that is template code just picking up content from a folder someplace (logo images, standard menus etc) and CSS style settings. I think it's a generally accepted principle that computers are better at handling thousands of files like that better than humans are. The Absolutely. Yes but a CMS normally uses a static structure with dynamic content. I think we might be using different meanings of structure. I'm referring to the site structure, as in which page is a parent of which other page. The site structure is just another aspect of the content. Ah, OK. I'm referring to the physical site structure, the folder heirarchy. A reference to the parent/child pages is just another attribute like content and title. Agreed, I'd expect those to be managed in the code and templates. A (good) CMS would create a dynamic structure for the user to browse, with folders and pages easily creatable and movable, I'm not sure I agree with that. It should allow movement of pages across folders with auto update iof relative paths etc, but not necessarily dynamic (ie at run time) changes. and references to pages would be dynamic so that if yo0u move a page internal links to it still work. I don;t necessarily see that as needing to be a dynamic feature, I'd probably prefer to do those kinds of changes under controlled circumstances and do the updates as a batch update process. Dynamic content is 'A Good Thing', dynamic structure is usually bad. But structure is just an aspect of content. If you mean navigation structure I agree, but physical structure - where the files live is absolutely not. And to make it so creates a huge headache for the site administrators - who usually cost a good deal more than the developers or content providers! Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Dynamically naming functions
On 13/03/06, Alan Gauld [EMAIL PROTECTED] wrote: For website, I can't really see how I can not have a dynamic structure. There's no way I'm writing a function for each folder. Hmm, this may be a CherryPie concept thing but the vast majority of websites do not have dynamic structures. It really shouldn't be necessary. Why would you need to write a function per folder, simply write one function that takes the folder as an argument. I do take your point though, however you often find that it's easy to maintain something dynamic than huge amounts of more static stuff Very rarely, its easy to do backups of huge amounts of data if you know where to find it, its hard to trawl all over a changing structure looking for the things that need backing up. Particularly if, when you try to restore it, it needs to go in a different place to where you found it! Backing up is generally one of the lesser of my worries. Maintaining content is primary. Unless you are dynamically generating static pages, they are a nightmare to maintain. Particularly as it tends to be quite repetitive. A large amount of the code on any page is the same as on any other page (page structure, boilerplate, etc). I think it's a generally accepted principle that computers are better at handling thousands of files like that better than humans are. The more of the repetitive stuff that can be removed and given to a computer, the better. in the difference between hundreds of static web pages and using a cms of some kind). Yes but a CMS normally uses a static structure with dynamic content. The templates stored in one place and the content in another. The templates know where to look for the content and the content doesn't care where the templates are. I think we might be using different meanings of structure. I'm referring to the site structure, as in which page is a parent of which other page. The site structure is just another aspect of the content. A reference to the parent/child pages is just another attribute like content and title. A (good) CMS would create a dynamic structure for the user to browse, with folders and pages easily creatable and movable, and references to pages would be dynamic so that if yo0u move a page internal links to it still work. Dynamic content is 'A Good Thing', dynamic structure is usually bad. But structure is just an aspect of content. BTW On the concept of loading your entire site at startup; if you are sharing a server you will make yourself very unpopular since you will be a huge resource hog. That's why ASP, JSP and other frameworks go to a lot of trouble to manage session lengths etc - to free up any unused resources and keep server speed up. Loading the structure of the site in the form of links might be reasonable, but only load content when you absolutely must. This also helps the maintainers update the site without restarting it. I have my own dedicated server, but point taken. Though I will still need to reload the structure of the site when the maintainers change it (adding/removing pages etc). Ed ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Dynamically naming functions
On 10/03/06, Kent Johnson [EMAIL PROTECTED] wrote: Ed Singleton wrote: How does one go about creating functions, classes, or callable objects when you don't know their name in advance? (For example you want to read their names in from a text file or database). I want to use this in a few different places. For example Faces, the Python Project Management Planner Tool Thingy, uses nested functions to put tasks within a project: def MyProject(): start = 2006-03-06 resource = Me def Task1(): start = 2006-03-13 def Task2(): effort = 1w I'd like to load these from a database (using SQLObject), but I'm not sure how I can define the name of the function from a filed in a database (or read in from a text file). This is truly bizarre use of nested functions. Faces must be looking at the compiled function objects to pick this out. To be honest, this didn't seem that bizarre to me. If I understand properly (which I probably don't) functions are just callable objects like any other callable object (or at least can be treated as such). Isn't this just a handy way of creating a nested object structure that's readable? I would look into the Project objects themselves and see if there is a way to create them dynamically, rather than trying to build this structure dynamically at run time. I'd also like to be able to do this in CherryPy/TurboGears so that I can create a dynamic site structure based on fields in a database. This seems more practical. I would define a class that is configured by the database can be used as a CP model object. Then you can insert the class into the CP site structure using setattr(). In general you can set an attribute of an object using setattr(): setattr(foo, 'bar', 3) is the same as foo.bar = 3 but the attribute name is specified as a string so it can be determined at runtime. This makes sense, and I think I can see how I would use it. To create a bunch of objects from some data (just name and start date): for fname, startdate in data: def foo: start = setattr(foo, __name__, fname) setattr(foo, start, startdate) Sorting out the nesting should be fairly straightforward (if the above works). Thanks Ed ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Dynamically naming functions
Ed Singleton wrote: On 10/03/06, Kent Johnson [EMAIL PROTECTED] wrote: Ed Singleton wrote: I want to use this in a few different places. For example Faces, the Python Project Management Planner Tool Thingy, uses nested functions to put tasks within a project: def MyProject(): start = 2006-03-06 resource = Me def Task1(): start = 2006-03-13 def Task2(): effort = 1w I'd like to load these from a database (using SQLObject), but I'm not sure how I can define the name of the function from a filed in a database (or read in from a text file). This is truly bizarre use of nested functions. Faces must be looking at the compiled function objects to pick this out. To be honest, this didn't seem that bizarre to me. If I understand properly (which I probably don't) functions are just callable objects like any other callable object (or at least can be treated as such). Isn't this just a handy way of creating a nested object structure that's readable? Why not just use nested dicts? MyProject = dict( start = 2006-03-06, resource = Me, Task1 = dict(start = 2006-03-13), Task2 = dict(effort = 1w), ) Maybe the appearance isn't quite as nice but the intent is clear, the code to access the data is *much* simpler, and it lends itself to the kind of programmatic creation you are trying to do. Take a look at task.Task._compile() in the faces source to see the gyrations it takes to extract the data in functional form. The Zen of Python says, Explicit is better than implicit Simple is better than complex In general you can set an attribute of an object using setattr(): setattr(foo, 'bar', 3) is the same as foo.bar = 3 but the attribute name is specified as a string so it can be determined at runtime. This makes sense, and I think I can see how I would use it. To create a bunch of objects from some data (just name and start date): for fname, startdate in data: def foo: start = setattr(foo, __name__, fname) setattr(foo, start, startdate) Sorting out the nesting should be fairly straightforward (if the above works). No, it won't work. A function attribute is not the same as a local variable of the function. What you propose is essentially def foo(): pass foo.bar = 'data' but what you need is def foo(): bar = 'data' which is very different. faces looks at foo.func_code.co_names to find the names you have used, and it actually runs the function with a tracing hook to capture the values. There doesn't seem to be any other way to define a Task in faces, either. You have to actually create functions. I guess this might be a good place to use exec - just create the source code for the function defs in a string and exec it, then retrieve the function from the global namespace and pass it to faces. Then tell me why this API is better than using nested dicts. Kent Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Dynamically naming functions
On 10/03/06, Alan Gauld [EMAIL PROTECTED] wrote: How does one go about creating functions, classes, or callable objects when you don't know their name in advance? (For example you want to read their names in from a text file or database). First point, names of functions are no different to names of other things. def f(x): y = blah return y is essentialy the same as saying f = lambda x: blah... lambda defines a nameless (aka anonymous) function and then assigns that function to the variable called f. Of course Pythons lambda construct is a bit brain dead so we can achieve the same result with just assigning an existing function to a new name def f(x): y = nblah... return y g = f Now g is a new name that refers to the function called f. I have to say I assumed that if you then changed f, g would point to the new f rather than the old f, but I tried it out and it seems otherwise: def f(x): ... print x ... f(1) 1 a = f a(1) 1 def f(x): ... print x * 2 ... f(1) 2 a(1) 1 b = f b(1) 2 Which is really cool, except that the names a and b need to be decided at run time. And with nested function definitions we can write functions that return functions: def makeMultiplyAndAdd(constants): def f(x): return constant[0] * constant[1] + x return f I'd like to load these from a database (using SQLObject), but I'm not sure how I can define the name of the function from a field in a database (or read in from a text file). Same way as you would for any other kind of variable. This has been discussed many times and there is some trickery you can do using the built in dictionaries that Python uses for its namespaces. But the biggest proiblem with introducing new names at run time is: How does the existing code get to know about those names that didn't exist when the code was written? Ypou need to have the code go exploring for names, work out what kind ioopf value they hold etc... It all gets very messy and complicated and fault prone. So the normal way to do this is to use a dictionary. The dictionary is a collection of names with values asociated with them. Those values can be functions or classes or anything else. Now your code can access the new names by using standard dictionary iterators etc. All you need are some conventions around how many parameters the functions have, their types etc. I'd also like to be able to do this in CherryPy/TurboGears so that I can create a dynamic site structure based on fields in a database. Dynamic site structure shouldn't need dynamic creation of functions although the structure might need to be dynamically loaded into a data structure in the code. It might also be a parameter of the functions. Doesn't CherryPy/TurboGears require Classes and Functions for it's data structures? (Or parameters passed to them, of course). I had been thinking that I would load my whole site at startup, but I guess it would probably be better to write a function that takes the parameters and looks up the first to see if it exists as a page, then looks to see if the second is a child of the first etc. But this really feels like something CherryPy should be doing for me. But as always remember that dynamic anything usually means much harder to maintain. All of your backup/archiving tools and error reporting tools etc will need to understand your dynamic structure too. Dynamic sounds like fun but usually means grief and is only justified as a last resort IMHO! For website, I can't really see how I can not have a dynamic structure. There's no way I'm writing a function for each folder. I do take your point though, however you often find that it's easy to maintain something dynamic than huge amounts of more static stuff (as in the difference between hundreds of static web pages and using a cms of some kind). But if you must go dynamic Python is as good a language to do it in as you'll get. That's certainly been my experience so far.. Thanks Ed ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Dynamically naming functions
Ed Singleton wrote: On 10/03/06, Alan Gauld [EMAIL PROTECTED] wrote: Dynamic site structure shouldn't need dynamic creation of functions although the structure might need to be dynamically loaded into a data structure in the code. It might also be a parameter of the functions. Doesn't CherryPy/TurboGears require Classes and Functions for it's data structures? (Or parameters passed to them, of course). CherryPy uses classes and functions as the foundation of the site hierarchy but the dynamic part of the site is usually handled by a function that processes parameters and generates a page, not by creating a function or class to handle every conceivable page. For website, I can't really see how I can not have a dynamic structure. There's no way I'm writing a function for each folder. I do take your point though, however you often find that it's easy to maintain something dynamic than huge amounts of more static stuff (as in the difference between hundreds of static web pages and using a cms of some kind). No, you don't have to do that, you create a handler at a relatively high level that knows how to respond to a request. See for example http://www.cherrypy.org/wiki/PositionalParameters and the section Partial matches and the default method in the tutorial: http://www.cherrypy.org/wiki/CherryPyTutorial Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Dynamically naming functions
Kent Johnson wrote: Why not just use nested dicts? MyProject = dict( start = 2006-03-06, resource = Me, Task1 = dict(start = 2006-03-13), Task2 = dict(effort = 1w), ) or nest class instances directly: MyProject = Project( start = 2006-03-06, resource = Me, Task1 = Task(start = 2006-03-13), Task2 = Task(effort = 1w), ) That seems pretty clear to me, it would be a snap to create dynamically and the implementation is simple. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Dynamically naming functions
On 13/03/06, Kent Johnson [EMAIL PROTECTED] wrote: Ed Singleton wrote: On 10/03/06, Kent Johnson [EMAIL PROTECTED] wrote: Ed Singleton wrote: I want to use this in a few different places. For example Faces, the Python Project Management Planner Tool Thingy, uses nested functions to put tasks within a project: def MyProject(): start = 2006-03-06 resource = Me def Task1(): start = 2006-03-13 def Task2(): effort = 1w I'd like to load these from a database (using SQLObject), but I'm not sure how I can define the name of the function from a filed in a database (or read in from a text file). This is truly bizarre use of nested functions. Faces must be looking at the compiled function objects to pick this out. To be honest, this didn't seem that bizarre to me. If I understand properly (which I probably don't) functions are just callable objects like any other callable object (or at least can be treated as such). Isn't this just a handy way of creating a nested object structure that's readable? Why not just use nested dicts? MyProject = dict( start = 2006-03-06, resource = Me, Task1 = dict(start = 2006-03-13), Task2 = dict(effort = 1w), ) Maybe the appearance isn't quite as nice but the intent is clear, the code to access the data is *much* simpler, and it lends itself to the kind of programmatic creation you are trying to do. Take a look at task.Task._compile() in the faces source to see the gyrations it takes to extract the data in functional form. The Zen of Python says, Explicit is better than implicit Simple is better than complex Point taken. Dict's are easier. And the appearance is fine. I've just discovered with a little playing, that you can do: def z(v): ... def f(x): ... print x * v ... return f ... c = z(3) c(1) 3 funcdict = dict(foo = z(4)) funcdict[foo](1) 4 Which was obvious enough that I thought of trying it, but surprising enough that I was really pleased when it worked. In general you can set an attribute of an object using setattr(): setattr(foo, 'bar', 3) is the same as foo.bar = 3 but the attribute name is specified as a string so it can be determined at runtime. This makes sense, and I think I can see how I would use it. To create a bunch of objects from some data (just name and start date): for fname, startdate in data: def foo: start = setattr(foo, __name__, fname) setattr(foo, start, startdate) Sorting out the nesting should be fairly straightforward (if the above works). No, it won't work. A function attribute is not the same as a local variable of the function. What you propose is essentially def foo(): pass foo.bar = 'data' but what you need is def foo(): bar = 'data' which is very different. faces looks at foo.func_code.co_names to find the names you have used, and it actually runs the function with a tracing hook to capture the values. There doesn't seem to be any other way to define a Task in faces, either. You have to actually create functions. I guess this might be a good place to use exec - just create the source code for the function defs in a string and exec it, then retrieve the function from the global namespace and pass it to faces. Then tell me why this API is better than using nested dicts. Agreed, though to be fair the author of faces has probably overcomplicated the code for handling it (which is a damn shame as it is really quite a nice project planning tool). I guess you can't blindly iterate over the methods of functions, as there are other methods there that you might not want to use (like setattr and stuff, maybe?). Ed ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Dynamically naming functions
Ed Singleton wrote: On 13/03/06, Kent Johnson [EMAIL PROTECTED] wrote: I've just discovered with a little playing, that you can do: def z(v): ... def f(x): ... print x * v ... return f ... c = z(3) c(1) 3 funcdict = dict(foo = z(4)) funcdict[foo](1) 4 Which was obvious enough that I thought of trying it, but surprising enough that I was really pleased when it worked. Yes, functions are first-class objects in Python. Functions can be assigned to variable, passed to and returned from functions, stored in lists and dictionaries...this is *very* useful in many contexts. faces looks at foo.func_code.co_names to find the names you have used, and it actually runs the function with a tracing hook to capture the values. There doesn't seem to be any other way to define a Task in faces, either. You have to actually create functions. I guess this might be a good place to use exec - just create the source code for the function defs in a string and exec it, then retrieve the function from the global namespace and pass it to faces. Then tell me why this API is better than using nested dicts. Agreed, though to be fair the author of faces has probably overcomplicated the code for handling it (which is a damn shame as it is really quite a nice project planning tool). I guess you can't blindly iterate over the methods of functions, as there are other methods there that you might not want to use (like setattr and stuff, maybe?). He may have overcomplicated it, but not by much. Offhand I couldn't even think of how he might have done it, I had to look at the code. Python has some ability to look at the details of a function - the attributes of func_code - but to find out what value a local variable gets, you have to call the function. These values aren't exposed outside the function, that is why he uses the settrace() hook. You might want to contact the author of faces and see what he suggests. Maybe he would add an alternate method of creating Tasks and Projects. Otherwise building and exec'ing a string is the only thing I can think of. Yuck. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Dynamically naming functions
For website, I can't really see how I can not have a dynamic structure. There's no way I'm writing a function for each folder. Hmm, this may be a CherryPie concept thing but the vast majority of websites do not have dynamic structures. It really shouldn't be necessary. Why would you need to write a function per folder, simply write one function that takes the folder as an argument. I do take your point though, however you often find that it's easy to maintain something dynamic than huge amounts of more static stuff Very rarely, its easy to do backups of huge amounts of data if you know where to find it, its hard to trawl all over a changing structure looking for the things that need backing up. Particularly if, when you try to restore it, it needs to go in a different place to where you found it! in the difference between hundreds of static web pages and using a cms of some kind). Yes but a CMS normally uses a static structure with dynamic content. The templates stored in one place and the content in another. The templates know where to look for the content and the content doesn't care where the templates are. Dynamic content is 'A Good Thing', dynamic structure is usually bad. BTW On the concept of loading your entire site at startup; if you are sharing a server you will make yourself very unpopular since you will be a huge resource hog. That's why ASP, JSP and other frameworks go to a lot of trouble to manage session lengths etc - to free up any unused resources and keep server speed up. Loading the structure of the site in the form of links might be reasonable, but only load content when you absolutely must. This also helps the maintainers update the site without restarting it. Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Dynamically naming functions
How does one go about creating functions, classes, or callable objects when you don't know their name in advance? (For example you want to read their names in from a text file or database). I want to use this in a few different places. For example Faces, the Python Project Management Planner Tool Thingy, uses nested functions to put tasks within a project: def MyProject(): start = 2006-03-06 resource = Me def Task1(): start = 2006-03-13 def Task2(): effort = 1w I'd like to load these from a database (using SQLObject), but I'm not sure how I can define the name of the function from a filed in a database (or read in from a text file). I'd also like to be able to do this in CherryPy/TurboGears so that I can create a dynamic site structure based on fields in a database. Thanks Ed ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Dynamically naming functions
Ed Singleton wrote: How does one go about creating functions, classes, or callable objects when you don't know their name in advance? (For example you want to read their names in from a text file or database). I want to use this in a few different places. For example Faces, the Python Project Management Planner Tool Thingy, uses nested functions to put tasks within a project: def MyProject(): start = 2006-03-06 resource = Me def Task1(): start = 2006-03-13 def Task2(): effort = 1w I'd like to load these from a database (using SQLObject), but I'm not sure how I can define the name of the function from a filed in a database (or read in from a text file). This is truly bizarre use of nested functions. Faces must be looking at the compiled function objects to pick this out. I would look into the Project objects themselves and see if there is a way to create them dynamically, rather than trying to build this structure dynamically at run time. I'd also like to be able to do this in CherryPy/TurboGears so that I can create a dynamic site structure based on fields in a database. This seems more practical. I would define a class that is configured by the database can be used as a CP model object. Then you can insert the class into the CP site structure using setattr(). In general you can set an attribute of an object using setattr(): setattr(foo, 'bar', 3) is the same as foo.bar = 3 but the attribute name is specified as a string so it can be determined at runtime. Kent Thanks Ed ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Dynamically naming functions
How does one go about creating functions, classes, or callable objects when you don't know their name in advance? (For example you want to read their names in from a text file or database). First point, names of functions are no different to names of other things. def f(x): y = blah return y is essentialy the same as saying f = lambda x: blah... lambda defines a nameless (aka anonymous) function and then assigns that function to the variable called f. Of course Pythons lambda construct is a bit brain dead so we can achieve the same result with just assigning an existing function to a new name def f(x): y = nblah... return y g = f Now g is a new name that refers to the function called f. And with nested function definitions we can write functions that return functions: def makeMultiplyAndAdd(constants): def f(x): return constant[0] * constant[1] + x return f I'd like to load these from a database (using SQLObject), but I'm not sure how I can define the name of the function from a field in a database (or read in from a text file). Same way as you would for any other kind of variable. This has been discussed many times and there is some trickery you can do using the built in dictionaries that Python uses for its namespaces. But the biggest proiblem with introducing new names at run time is: How does the existing code get to know about those names that didn't exist when the code was written? Ypou need to have the code go exploring for names, work out what kind ioopf value they hold etc... It all gets very messy and complicated and fault prone. So the normal way to do this is to use a dictionary. The dictionary is a collection of names with values asociated with them. Those values can be functions or classes or anything else. Now your code can access the new names by using standard dictionary iterators etc. All you need are some conventions around how many parameters the functions have, their types etc. I'd also like to be able to do this in CherryPy/TurboGears so that I can create a dynamic site structure based on fields in a database. Dynamic site structure shouldn't need dynamic creation of functions although the structure might need to be dynamically loaded into a data structure in the code. It might also be a parameter of the functions. But as always remember that dynamic anything usually means much harder to maintain. All of your backup/archiving tools and error reporting tools etc will need to understand your dynamic structure too. Dynamic sounds like fun but usually means grief and is only justified as a last resort IMHO! But if you must go dynamic Python is as good a language to do it in as you'll get. Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Dynamically naming functions
I want to use this in a few different places. For example Faces, the Python Project Management Planner Tool Thingy, uses nested functions to put tasks within a project: def MyProject(): start = 2006-03-06 resource = Me def Task1(): start = 2006-03-13 def Task2(): effort = 1w I'd like to load these from a database (using SQLObject), but I'm not sure how I can define the name of the function from a filed in a database (or read in from a text file). Hi Ed, If we have a bunch of functions in a module, then we can get at a function from its string name by using the getattr() built-in function. http://www.python.org/doc/lib/built-in-funcs.html#l2h-31 For example, let's say that we have a module like the math module. ## import math ## We know that we can get the function by naming it. ## math.sin built-in function sin math.sin(3.14) 0.0015926529164868282 ## But we can also get it if we have a string: ## def getMathFunction(name): ... return getattr(math, name) ... getMathFunction(sin) built-in function sin getMathFunction(sin)(3.14) 0.0015926529164868282 ## And of course, that string can come from anywhere, including a database. Does this answer your question? If you have questions, please feel free to ask. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor