In Python how to write such a function which will call all functions in current file with given prefix?
For example:
def prepare(self):
# ??? to call prepare_1, prepare_2
def prepare_1(self):
def prepare_2(self):
How to write prepare
so it will call all functions started with prepare_
?
Use globals to access global namespace, dict.items to iterate over it and callable and str.startswith to identify that function has name you wish and it's callable:
def prepare(self):
for key, value in globals().items():
if callable(value) and key.startswith('prepare_'):
value()
def prepare_1(self):print 1
def prepare_2(self):print 2
If these functions are methods of a class, use dir(self)
to list all attributes of self
.
class C:
def prepare(self):
print(dir(self))
for name in dir(self):
if name.startswith('prepare_'):
method = getattr(self, name)
method()
def prepare_1(self):
print('In prepare_1')
def prepare_2(self):
print('In prepare_2')
C().prepare()
Output:
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'prepare', 'prepare_1', 'prepare_2']
In prepare_1
In prepare_2
Update: if you want to call methods from outside of class C:
obj = C()
for name in dir(obj):
if name.startswith('prepare_'):
m = getattr(obj, name)
print(m)
m()
Output:
<bound method C.prepare_1 of <__main__.C object at 0x7f347c9dff28>>
In prepare_1
<bound method C.prepare_2 of <__main__.C object at 0x7f347c9dff28>>
In prepare_2
It's been asked for, so here's a quick hack:
import functools
class FunctionGroup(object):
"""
Defines a function group as a list of functions that can be
executed sequentially from a single call to the function group.
Use
@func_group.add
def my_func(...):
...
to add functions to the function group.
`func_group(...)` calls the added functions one by one.
It returns a list of the return values from all evaluated functions.
Processing terminates when one of the function raises an
exception and the exception is propagated to the caller.
"""
def __init__(self):
self.funcs = []
def add(self, func):
self.funcs.append(func)
return func
def __call__(self, *args, **kwargs):
return [
func(*args, **kwargs) for func in self.funcs
]
prepare_group = FunctionGroup()
Note that the __call__()
implementation is rather primitive and does nothing to handle exceptions.
Usage example:
@prepare_group.add
def prepare_1():
print "prep 1"
@prepare_group.add
def prepare_2():
print "prep 2"
prepare_group()
Maybe abused to call methods, of course:
class C(object):
def m(self):
pass
c = C()
func_group.add(c.m)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With