I can't figure out how to list all functions from an imported module.
I searched Google, but all the answers I found didn't work. Did
something change in Python 2.2, perhaps there's a new method of doing
it? 6 2001
In article <88**************************@posting.google.com >, kl*******@home.com (Kamilche) wrote: I can't figure out how to list all functions from an imported module. I searched Google, but all the answers I found didn't work. Did something change in Python 2.2, perhaps there's a new method of doing it?
I would start with "dir (moduleName)"
On Thu, 2004-05-27 at 08:06, Kamilche wrote: I can't figure out how to list all functions from an imported module. I searched Google, but all the answers I found didn't work. Did something change in Python 2.2, perhaps there's a new method of doing it?
Try the inspect module.
// m kl*******@home.com (Kamilche) wrote in message news:<88**************************@posting.google. com>... I can't figure out how to list all functions from an imported module. I searched Google, but all the answers I found didn't work. Did something change in Python 2.2, perhaps there's a new method of doing it?
e.g the module os import os,types for k,v in os.__dict__.items():
.... if type(v) == types.BuiltinFunctionType or\
.... type(v) == types.BuiltinMethodType or\
.... type(v) == types.FunctionType or\
.... type(v) == types.MethodType:
.... print '%-20s: %r' % (k,type(v))
....
rename : <type 'builtin_function_or_method'
lseek : <type 'builtin_function_or_method'>
_get_exports_list : <type 'function'>
execle : <type 'function'>
chmod : <type 'builtin_function_or_method'>
execlp : <type 'function'>
open : <type 'builtin_function_or_method'>
write : <type 'builtin_function_or_method'>
putenv : <type 'builtin_function_or_method'>
fdopen : <type 'builtin_function_or_method'>
_pickle_statvfs_result: <type 'function'>
startfile : <type 'builtin_function_or_method'>
umask : <type 'builtin_function_or_method'>
system : <type 'builtin_function_or_method'>
_execvpe : <type 'function'>
getpid : <type 'builtin_function_or_method'>
tmpnam : <type 'builtin_function_or_method'>
dup : <type 'builtin_function_or_method'>
spawnve : <type 'builtin_function_or_method'>
getenv : <type 'function'>
isatty : <type 'builtin_function_or_method'>
execvpe : <type 'function'>
dup2 : <type 'builtin_function_or_method'>
read : <type 'builtin_function_or_method'>
execvp : <type 'function'>
popen3 : <type 'builtin_function_or_method'>
_make_stat_result : <type 'function'>
execve : <type 'builtin_function_or_method'>
utime : <type 'builtin_function_or_method'>
execl : <type 'function'>
chdir : <type 'builtin_function_or_method'>
renames : <type 'function'>
strerror : <type 'builtin_function_or_method'>
remove : <type 'builtin_function_or_method'>
fstat : <type 'builtin_function_or_method'>
execv : <type 'builtin_function_or_method'>
execlpe : <type 'function'>
tempnam : <type 'builtin_function_or_method'>
tmpfile : <type 'builtin_function_or_method'>
popen4 : <type 'builtin_function_or_method'>
popen2 : <type 'builtin_function_or_method'>
stat : <type 'builtin_function_or_method'>
abort : <type 'builtin_function_or_method'>
close : <type 'builtin_function_or_method'>
_exists : <type 'function'>
spawnl : <type 'function'>
makedirs : <type 'function'>
access : <type 'builtin_function_or_method'>
unsetenv : <type 'function'>
mkdir : <type 'builtin_function_or_method'>
spawnv : <type 'builtin_function_or_method'>
listdir : <type 'builtin_function_or_method'>
_pickle_stat_result : <type 'function'>
lstat : <type 'builtin_function_or_method'>
spawnle : <type 'function'>
getcwd : <type 'builtin_function_or_method'>
unlink : <type 'builtin_function_or_method'>
_make_statvfs_result: <type 'function'>
popen : <type 'builtin_function_or_method'>
times : <type 'builtin_function_or_method'>
pipe : <type 'builtin_function_or_method'>
removedirs : <type 'function'>
_exit : <type 'builtin_function_or_method'>
rmdir : <type 'builtin_function_or_method'>
Regards
Peter
Peter Abel wrote: kl*******@home.com (Kamilche) wrote in message news:<88**************************@posting.google. com>...
I can't figure out how to list all functions from an imported module. I searched Google, but all the answers I found didn't work. Did something change in Python 2.2, perhaps there's a new method of doing it?
e.g the module os
import os,types for k,v in os.__dict__.items():
... if type(v) == types.BuiltinFunctionType or\ ... type(v) == types.BuiltinMethodType or\ ... type(v) == types.FunctionType or\ ... type(v) == types.MethodType: ... print '%-20s: %r' % (k,type(v))
Yuck... wouldn't using "callable(v)" be a lot nicer?
-Peter
Peter Hansen <pe***@engcorp.com> wrote in
news:o-********************@powergate.ca: Peter Abel wrote:
kl*******@home.com (Kamilche) wrote in message news:<88**************************@posting.google. com>...
I can't figure out how to list all functions from an imported module. I searched Google, but all the answers I found didn't work. Did something change in Python 2.2, perhaps there's a new method of doing it?
e.g the module os
>import os,types >for k,v in os.__dict__.items():
... if type(v) == types.BuiltinFunctionType or\ ... type(v) == types.BuiltinMethodType or\ ... type(v) == types.FunctionType or\ ... type(v) == types.MethodType: ... print '%-20s: %r' % (k,type(v))
Yuck... wouldn't using "callable(v)" be a lot nicer?
It depends whether he wants to include callable classes or really does want
just functions.
If he doesn't want callable classes, then a nicer way would be to use
isinstance, which also gives you the option of factoring out the list of
types: FUNCTION_TYPES = (types.BuiltinFunctionType,
types.BuiltinMethodType,
types.FunctionType,
types.MethodType,)
for k,v in os.__dict__.items():
if isinstance(v, FUNCTION_TYPES):
print '%-20s: %r' % (k,type(v))
Thanks guys. It DOES work, as you stated... my problem was I had a
module called the same name as a variable I was using, yeesh.
I'm used to C, where you have to declare variables before use, and the
compiler catches stuff like this. I might have to go back to a
Hungarian-like naming conventions for variables, it appears.
--Kamilche This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Blair Hall |
last post by:
Can anyone please tell me how to correctly use a built in function
when there is a function of the same name in local scope?
Here is an example. Suppose the following is in myApply.py:
def...
|
by: Aubrey Hutchison |
last post by:
Question about functions:
1)--- module "A" contains the following
X = 999666 #easy to notice value
Y = 111222 #easy to notice value
Product = 69696969 #easy to...
|
by: John Hunter |
last post by:
What is the best way to get a list of all functions actually defined
in a module, ignoring those functions that the module may have
imported but not defined?
The following gets a list of all the...
|
by: Philippe C. Martin |
last post by:
Hi,
I have a fairly large project going on and would like to figure out
automatically from the source which files are being imported.
ex: find_out mymain.py
Is there an easy way to...
|
by: Martin M. |
last post by:
Hi,
I have the following question: How can an imported module see/find the
path to itself?
Background: From my main script I import a module which needs a file
(AppleScript) located in the...
|
by: Brian |
last post by:
I just have a basic style question here. Suppose you have the program:
def foo1():
do something
def foo2()
do something else
Assume that you want to call these functions at execution. Is...
|
by: devicerandom |
last post by:
Hi,
I am currently using the Cmd module for a mixed cli+gui application. I
am starting to refactor my code and it would be highly desirable if
many commands could be built as simple plugins.
...
|
by: Matt_D |
last post by:
Good afternoon.
As a self-tutoring project I am writing a one-time-pad encrypt/decrypt
script. I have completed the encryption portion and am working
currently on the decryption algorithm. My...
|
by: Nadeem |
last post by:
Hello all,
I'm trying to write a function that will dynamically generate other
functions via exec. I then want to be able to import the file (module)
containing this function and use it in other...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
| |