If you have the function name, you can do:

    if function_name in globals():

And if you have the actual function object, you can do:

    if function.__name__ in globals():

Note, those will yield hits even if there is an object of that name defined 
in a model file. If that is a concern, you can diff the globals before and 
after executing the controller to get a list of objects specifically 
defined in the controller. To do that, at the top of the controller file 
(or anywhere before the first component you want to register):

pre_controller_objects = dir()

Then at the bottom of the controller (or after the last component):

controller_objects = set(dir()) - set(pre_controller_objects)

Then, wherever you want to test for the existence of a function:

    if function_name in controller_objects:

Note, that will identify any objects defined in the controller, even if 
they are not functions. If that's a concern, you can further filter 
controller_objects to include only function objects.

Finally, if you only want to identify specific actions that your have 
defined as components, you could use a special prefix/postfix to identify 
them, or just manually maintain a list of their names. One other option 
would be to create a decorator that registers a component name in a special 
object. In a module or model file, or at the top of the controller:

class Component(object):
    def __init__(self):
        self.names = []

    def __call__(self, f):
        self.names.append(f.__name__)
        return f

component = Component()

Then you would decorate components:

@component
def component1():
    return dict()

@component
def component2():
    return dict()

Finally, wherever needed, check for a component as follows:

    if 'component1' in component.names:

Anthony

On Monday, October 19, 2015 at 4:19:30 PM UTC-4, A3 wrote:
>
>
> I want to be able to test if a certain function exists in a controller.
> (I am loading a component with help of another)
>
> controller: 
> default.py
> def myfunction()
>      return
>
> def mytest()
>      if exists(myfunction()):
>            do this.  
>

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to