How to call all functions with a name starting with this prefix?

In Python, how to write a function that calls all the functions in the current file with the given prefix?

For instance:

def prepare(self):
  # ??? to call prepare_1, prepare_2

def prepare_1(self):

def prepare_2(self):

How to write prepareso that it calls all functions launched with prepare_?

+4
source share
3 answers

If these functions are class methods, use dir(self)to list all attributes 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()

Conclusion:

['__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 the class C:

obj = C()
for name in dir(obj):
    if name.startswith('prepare_'):
        m = getattr(obj, name)
        print(m)
        m()

Conclusion:

<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
+3
source

globals , dict.items, callable str.startswith, , , , :

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
+8

He was asked, so hack quickly:

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 implementation is __call__()pretty 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()

Perhaps abuse of method calls, of course:

class C(object):
    def m(self):
       pass
c = C()
func_group.add(c.m)
+1
source

Source: https://habr.com/ru/post/1652193/


All Articles