how to wrap automatically functions from certain file
<p>It's a well known fact there are many ways to get a function name using python standard library, here's a little example:</p>

<pre><code>import sys
import dis
import traceback


def get_name():
    stack = traceback.extract_stack()
    filename, codeline, funcName, text = stack[-2]
    return funcName


def foo1():
    print("Foo0 start")
    print("Inside-_getframe {0}".format(sys._getframe().f_code.co_name))
    print("Inside-traceback {0}".format(get_name()))
    print("Foo1 end")


def foo2():
    print("Foo2 start")
    print("Inside {0}".format(sys._getframe().f_code.co_name))
    print("Inside-traceback {0}".format(get_name()))
    print("Foo2 end")


def foo3():
    print("Foo3 start")
    print("Inside {0}".format(sys._getframe().f_code.co_name))
    print("Inside-traceback {0}".format(get_name()))
    print("Foo3 end")

for f in [foo1, foo2, foo3]:
    print("Outside: {0}".format(f.__name__))
    f()
    print('-' * 80)
</code></pre>

<p>You can use <a href="https://docs.python.org/2/library/traceback.html" rel="nofollow">traceback</a>, <a href="https://docs.python.org/2/library/sys.html#sys._getframe" rel="nofollow">sys._getframe</a>, <a href="https://docs.python.org/2/library/dis.html" rel="nofollow">dis</a> and maybe there is a lot of more options... so far so good, python is awesome to do this kind of introspection.</p>

<p>Now, here's the thing, I'd like to know how to wrap automatically functions (at file level) to print its name and also measuring the execution time when they are executed. For instance, something like this:</p>

<pre><code>def foo1():
    print("Foo0 processing")


def foo2():
    print("Foo2 processing")


def foo3():
    print("Foo3 processing")

wrap_function_from_this_file()

for f in [foo1, foo2, foo3]:
    f()
    print('-' * 80)
</code></pre>

<p>Would print something like:</p>

<pre><code>foo1 started
Foo1 processing
foo1 finished, elapsed time=1ms
--------------------------------------------------------------------------------
foo2 started
Foo2 processing
foo2 finished, elapsed time=2ms
--------------------------------------------------------------------------------
foo3 started
Foo3 processing
foo3 finished, elapsed time=3ms
--------------------------------------------------------------------------------
</code></pre>

<p>As you can see, the idea would be not adding any wrapper per-function manually to the file's functions. <strong>wrap_function_from_this_file</strong> would automagically introspect the file where is executed and it'd modify functions wrapping them somewhat, in this case, wrapping the functions with some code printing its name and execution time.</p>

<p>Just for the record, <strong>I'm not asking for any profiler</strong>. I'd like to know whether this is possible to do and how.</p>

<p>Here's the solution I was looking for:</p>

<pre><code>import inspect
import time
import random
import sys

random.seed(1)


def foo1():
    print("Foo0 processing")


def foo2():
    print("Foo2 processing")


def foo3():
    print("Foo3 processing")


def wrap_functions_from_this_file():
    def wrapper(f):
        def _inner(*args, **kwargs):
            start = time.time()
            print("{0} started".format(f.__name__))
            result = f(*args, **kwargs)
            time.sleep(random.random())
            end = time.time()
            print('{0} finished, elapsed time= {1:.4f}s'.format(
                f.__name__, end - start))

        return _inner

    for o in inspect.getmembers(sys.modules[__name__], inspect.isfunction):
        globals()[o[0]] = wrapper(o[1])


wrap_functions_from_this_file()
for f in [foo1, foo2, foo3]:
    f()
    print('-' * 80)
</code></pre>

python python-2.7 python-3.x introspection