Reputation: 11207
I want to do something like this:
def a():
# do stuff
return stuff
def b():
# do stuff
return different_stuff
def c():
# do one last thing
return 200
for func in this_file:
print func_name
print func_return_value
I essentially want to mimic this flask app, without the flask parts:
app = Flask(__name__)
app.register_blueprint(my_bp, url_prefix='/test')
my_bp.data = fake_data
def tests():
with app.test_client() as c:
for rule in app.url_map.iter_rules():
if len(rule.arguments) == 0 and 'GET' in rule.methods:
resp = c.get(rule.rule)
log.debug(resp)
log.debug(resp.data)
is this possible?
Upvotes: 0
Views: 111
Reputation: 32459
Maybe:
def a(): return 1
def b(): return 2
def c(): return 3
for f in globals ().values ():
if callable (f): continue
print f.__name__
print f ()
Upvotes: 1
Reputation: 41
Use this code to create the python module get_module_attrs.py
import sys
module = __import__(sys.argv[1])
for name in dir(module):
obj = getattr(module, name)
if callable(obj):
print obj.__name__
Then you can call it as $python get_module_attrs.py <name_of_module>
Enjoy it!!
Upvotes: 1
Reputation: 36574
Like this:
import sys
# some functions...
def a():
return 'a'
def b():
return 'b'
def c():
return 'c'
# get the module object for this file
module = sys.modules[__name__]
# get a list of the names that are in this module
for name in dir(module):
# get the Python object from this module with the given name
obj = getattr(module, name)
# ...and if it's callable (a function), call it.
if callable(obj):
print obj()
running this gives:
bgporter@varese ~/temp:python moduleTest.py
a
b
c
Note that the functions will not necessarily be called in the order of definition as they are here.
Upvotes: 3