Modul gotchas:) 
Author Message
 Modul gotchas:)

Hi all!

happy new year, first of all!!!

My question is simple, yet I missed the right answer, although have lots
of books on Python:(

Suppose, one has a module, which defines some set of functions.

The question:

How can one get the names of all these functions as list entries?

E.g.,

all_func=[]

import myModule

all_func=myModule.functions()

of course, this will not work.

What is to be done? Some special functions are to be written or, is
there a built-in tool?

thanks a lot!

Grtz,
Giorgi



Mon, 21 Jun 2004 05:13:44 GMT  
 Modul gotchas:)

Quote:

>Hi all!

>happy new year, first of all!!!

>My question is simple, yet I missed the right answer, although have lots
>of books on Python:(

>Suppose, one has a module, which defines some set of functions.

>The question:

>How can one get the names of all these functions as list entries?

>E.g.,

>all_func=[]

>import myModule

>all_func=myModule.functions()

>of course, this will not work.

>What is to be done? Some special functions are to be written or, is
>there a built-in tool?

you can use the built-in "dir" function to do things like that.
The following code filters functions from the "dir" output:

import myModule
for f in dir(myModule):
    if callable(getattr(myModule, d)):
        print d

cheers

--
Sylvain Thenault

  LOGILAB           http://www.logilab.org



Sun, 20 Jun 2004 20:55:24 GMT  
 Modul gotchas:)
Thanx!

Meanwhile, I found another solution:
myModule.__dict__.keys()

I guess it will produce the same result?

Quote:


> >Hi all!

> >happy new year, first of all!!!

> >My question is simple, yet I missed the right answer, although have lots
> >of books on Python:(

> >Suppose, one has a module, which defines some set of functions.

> >The question:

> >How can one get the names of all these functions as list entries?

> >E.g.,

> >all_func=[]

> >import myModule

> >all_func=myModule.functions()

> >of course, this will not work.

> >What is to be done? Some special functions are to be written or, is
> >there a built-in tool?

> you can use the built-in "dir" function to do things like that.
> The following code filters functions from the "dir" output:

> import myModule
> for f in dir(myModule):
>     if callable(getattr(myModule, d)):
>         print d

> cheers

> --
> Sylvain Thenault

>   LOGILAB           http://www.logilab.org



Mon, 21 Jun 2004 06:20:40 GMT  
 Modul gotchas:)

Quote:
> Hi all!

> happy new year, first of all!!!

> My question is simple, yet I missed the right answer, although have lots
> of books on Python:(

> Suppose, one has a module, which defines some set of functions.

> The question:

> How can one get the names of all these functions as list entries?

> [snip]

Chapter 2 of Dive Into python show how this can be done (a little edited):

def methodlist(object):
        return [method for method in dir(object) if callable(getattr    
(object, method))]

See http://www.diveintopython.org/apihelper_divein.html



Sun, 20 Jun 2004 21:22:31 GMT  
 Modul gotchas:)
Thanx for the hint!

However, when I applied this to the 'math' module, it yielded []...
I must have missed something.

If I have all_func list of the function names, i.e., strings, not
pointers, I can do something like this:

class someClass:
    ....

    def getVector(self, names):
        vector=[]
        import myModule
        all_func=myModule.some_way_to_get_the_names()
        for name in names:
            if name in all_func:
                f=eval('myModule.'+name)
                vector.append(f(self.somthing))
        return vector

the names must be the list of strings...

Quote:


> -0800:

> >How can one get the names of all these functions as list entries?

> >E.g.,

> >all_func=[]

> >import myModule

> >all_func=myModule.functions()

> >of course, this will not work.

> >What is to be done? Some special functions are to be written or, is
> >there a built-in tool?

> all_func=filter(lambda x:type(myModule.__dict__[x])==\
> type(lambda x:x),myModule.__dict__.keys())

> Now the question is: what are you going to do with that list?

> --
> #define print void main(){ printf(
> print "signature under construction"
> #define pass );}
> pass



Mon, 21 Jun 2004 06:35:32 GMT  
 Modul gotchas:)

    ...

Quote:
> Suppose, one has a module, which defines some set of functions.
> How can one get the names of all these functions as list entries?
> all_func=[]

You don't need to bind name all_func to anything: it gets rebound
two statements later, so this binding has no effect at all.

Quote:
> import myModule
> all_func=myModule.functions()

> of course, this will not work.

No, but, after the import, you could for example do:

all_func = [name for name in dir(myModule) if
callable(getattr(myModule,name))]

this isn't QUITE what you asked since it gives you the name of every
CALLABLE object that is a module attribute: this includes functions,
builtin-functions, and classes too (and potentially more things yet,
such as instances of classes exposing a __call__ special method that
the module is trying to "masquerade", aka "pass off as", functions:-)].

If you do need to check for FUNCTIONS specifically, and rule out every
other callable object, you can to that, of course:

import types
import myModule
all_func = [name for name in dir(myModule)
    if type(getattr(myModule,name)) is types.FunctionType]

However, you're "normally" quite OK with accepting "stuff masquerading
as functions" as equivalent to functions.  The test for 'callable',
while maybe a bit weak, is more likely closer to what you really need,
than the test for type-identity with 'functions'.  Still, it's up to you.

Quote:
> What is to be done? Some special functions are to be written or, is
> there a built-in tool?

There are several built-in functions:
    dir(obj)            get all attributes name directly defined in obj
    getattr(obj, name)  get attribute value given attribute name
    callable(obj)       check if an object can be called as a function
    type(obj)           get the typeobject for an object
and language 'tools' such as list comprehensions, to put together
lists.  If you do this often, you may indeed want to write a "special
function" of your own that puts together a few of these built-in tools
to give you the overall functionality that you desire.

Alex



Sun, 20 Jun 2004 20:31:24 GMT  
 Modul gotchas:)
Thanx!
Just what I needed!
Quote:


> > Hi all!

> > happy new year, first of all!!!

> > My question is simple, yet I missed the right answer, although have lots
> > of books on Python:(

> > Suppose, one has a module, which defines some set of functions.

> > The question:

> > How can one get the names of all these functions as list entries?

> > [snip]

> Chapter 2 of Dive Into Python show how this can be done (a little edited):

> def methodlist(object):
>         return [method for method in dir(object) if callable(getattr
> (object, method))]

> See http://www.diveintopython.org/apihelper_divein.html



Mon, 21 Jun 2004 06:39:31 GMT  
 Modul gotchas:)
Thank you all  for the help!!!

Greetings, Giorgi

Quote:

> Hi all!

> happy new year, first of all!!!

> My question is simple, yet I missed the right answer, although have lots
> of books on Python:(

> Suppose, one has a module, which defines some set of functions.

> The question:

> How can one get the names of all these functions as list entries?

> E.g.,

> all_func=[]

> import myModule

> all_func=myModule.functions()

> of course, this will not work.

> What is to be done? Some special functions are to be written or, is
> there a built-in tool?

> thanks a lot!

> Grtz,
> Giorgi



Mon, 21 Jun 2004 07:09:00 GMT  
 Modul gotchas:)
Right!

thank you for the help!

Quote:


> -0800:

> >However, when I applied this to the 'math' module, it yielded []...
> >I must have missed something.

> Well, it depends on how you define "function". Are you perhaps looking
> for builtin functions too? What about callable items?

> >If I have all_func list of the function names, i.e., strings, not
> >pointers, I can do something like this:

> >class someClass:
> >    ....

> >    def getVector(self, names):
> >        vector=[]
> >        import myModule
> >        all_func=myModule.some_way_to_get_the_names()
> >        for name in names:
> >            if name in all_func:
> >                f=eval('myModule.'+name)
> >                vector.append(f(self.somthing))
> >        return vector

> Why don't you just use a list of functions and "apply"?

> --
> #define print void main(){ printf(
> print "signature under construction"
> #define pass );}
> pass



Mon, 21 Jun 2004 07:08:29 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. Most annoying Java gotchas

2. IFC Gotchas.

3. wxPython gotchas

4. Win32Com: VB5 COM server gotchas

5. FORTRAN "gotchas", and how to aviod them

6. Modul

7. GPF in modul CWCIF16.DLL

8. ICP CON 7520 or Adam 4520 Modul

9. Error in Modul NIDAQ32.DLL

10. Did fun-o finished the xml modul?

11. Disk Om Modul

12. OS/390 LOAD MODUL FORMAT Description

 

 
Powered by phpBB® Forum Software