473,387 Members | 3,801 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Quantify over all functions of a module

Hello,

I want to refer to all functions of a module, say test2.py, without
having to manually enumerate them.

The following code comes closest to what I am trying to do.
It does not work, because dir() does not return functions, but
function names as strings.

##### test2.py #####
def plus_2(_int):
return _int + 2

def plus_3(_int):
return _int + 3
#### test.py #####
#!/usr/bin/env python

import re
import test2

func_list = []

for func in dir(test2):
test = re.search("^__", func)
if not test:
func_list.append(func)

for func in func_list:
print apply(func, (2,))
I failed to find a solution by searching the net, probably because I
didn't know the right keywords. And I suppose that searching
would not be necessary, if I knew the right keywords.
Jörg
Jul 18 '05 #1
4 1554
> something like this might work:

for name, func in vars(test2):
if not name.startswith("__") and callable(func):
func_list.append(func)


ahem. better make that:

for name, func in vars(test2).items():
if not name.startswith("__") and callable(func):
func_list.append(func)

</F>


Jul 18 '05 #2
Joerg Schuster wrote:
The following code comes closest to what I am trying to do.
It does not work, because dir() does not return functions, but
function names as strings.

##### test2.py #####
def plus_2(_int):
return _int + 2

def plus_3(_int):
return _int + 3
#### test.py #####
#!/usr/bin/env python

import re
import test2

func_list = []

for func in dir(test2):
test = re.search("^__", func)
if not test:
func_list.append(func)

for func in func_list:
print apply(func, (2,))
I failed to find a solution by searching the net, probably because I
didn't know the right keywords. And I suppose that searching
would not be necessary, if I knew the right keywords.


if you look up "dir" in the documentation, you'll find related functions
including:

getattr(module, name) => object

and

vars(module) => dictionary mapping names to objects

something like this might work:

for name, func in vars(test2):
if not name.startswith("__") and callable(func):
func_list.append(func)

</F>


Jul 18 '05 #3
In article <ma**************************************@python.o rg>,
Fredrik Lundh <fr*****@pythonware.com> wrote:
something like this might work:

for name, func in vars(test2):
if not name.startswith("__") and callable(func):
func_list.append(func)


ahem. better make that:

for name, func in vars(test2).items():
if not name.startswith("__") and callable(func):
func_list.append(func)


....and if you wanna show off your Python 2.2+ knowledge, make that
iteritems(). ;-)
--
Aahz (aa**@pythoncraft.com) <*> http://www.pythoncraft.com/

Weinberg's Second Law: If builders built buildings the way programmers wrote
programs, then the first woodpecker that came along would destroy civilization.
Jul 18 '05 #4
Thanks.
Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Peter Åstrand | last post by:
There's a new PEP available: PEP 324: popen5 - New POSIX process module A copy is included below. Comments are appreciated. ---- PEP: 324 Title: popen5 - New POSIX process module
8
by: Bo Peng | last post by:
Dear list, I am writing a Python extension module that needs a way to expose pieces of a big C array to python. Currently, I am using NumPy like the following: PyObject* res =...
5
by: Sans Spam | last post by:
OK, I have a string that reads like this: ... How do I quantify how many occurences there are in classic ASP? Do i use the inStr function and if so, how??? Here's my thought process... ...
1
by: Frode Langelo | last post by:
Hi, I am running Rational Quantify 2002a.06.00 on Solaris 2.8 . Is there a way to have Quantify exclude libraries from its measurements by for instance defining these in a file, or is that only...
2
by: Bryan Olson | last post by:
The current Python standard library provides two cryptographic hash functions: MD5 and SHA-1 . The authors of MD5 originally stated: It is conjectured that it is computationally infeasible to...
0
by: Steven Bethard | last post by:
Ok, so I have a module that is basically a Python wrapper around a big lookup table stored in a text file. The module needs to provide a few functions:: get_stem(word, pos, default=None)...
10
by: Tom Plunket | last post by:
I've got a bunch of code that runs under a bunch of unit tests. It'd be really handy if when testing I could supply replacement functionality to verify that the right things get called without...
13
by: André | last post by:
Hi, i'm developping asp.net applications and therefore i use VB.net. I have some questions about best practises. According what i read about class and module and if i understand it right, a...
3
by: Achava Nakhash, the Loving Snake | last post by:
I just had Rational PurifyPlus installed on my PC (running Windows XP) and would like to use the Rational Quantify part of it to profile a piece of code that I was given. We all use Microsoft...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...

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.