inspect

Capturing a function call as dict

Capturing a function call as dict Question: I’m trying to write a python function decorator, and part of my implementation is that I need to capture the function call and look through the supplied values. I already have the function signature by using inspect.signature, but I’m unsure how to compose it with passed arguments. Say …

Total answers: 1

inspect.getsourcelines(object) shows OSError if run from python shell but gives no error if run from a file

inspect.getsourcelines(object) shows OSError if run from python shell but gives no error if run from a file Question: I tried this code-snippet inside a script named test.py : from inspect import * def f1(p,r): “””Return f1 score from p,r””” return 2*p*r/(p+r) print(getsourcelines(f1)) If I run this from terminal with python3 test.py, it outputs the following …

Total answers: 2

Get line number where variable is defined

Get line number where variable is defined Question: I’m trying to extract the line number where a specific variable was created. This sounds like a simple question, after all, I could loop through the lines of code and try to search, or do a regular expression. But that might be a false line number and …

Total answers: 2

How to find out if (the source code of) a function contains a loop?

How to find out if (the source code of) a function contains a loop? Question: Let’s say, I have a bunch of functions a, b, c, d and e and I want to find out if they directly use a loop: def a(): for i in range(3): print(i**2) def b(): i = 0 while i …

Total answers: 3

get current function name inside that function using python

get current function name inside that function using python Question: For my logging purpose i want to log all the names of functions where my code is going Does not matter who is calling the function , i want the the function name in which i declare this line import inspect def whoami(): return inspect.stack()[1][3] …

Total answers: 6

Debugging: Get filename and line number from which a function is called?

Debugging: Get filename and line number from which a function is called? Question: I’m currently building quite a complex system in Python, and when I’m debugging I often put simple print statements in several scripts. To keep an overview I often also want to print out the file name and line number where the print …

Total answers: 6

What is the difference between a stack and a frame?

What is the difference between a stack and a frame? Question: Under what situations would I want to use one over the other? What is the difference between: >>> import inspect >>> print(inspect.getouterframes(inspect.currentframe())) [(<frame object at 0x8fc262c>, ‘<stdin>’, 1, ‘<module>’, None, None)] And: >>> import traceback >>> traceback.extract_stack() [(‘<stdin>’, 1, ‘<module>’, None)] Update: Another: >>> …

Total answers: 2

Python inspect.stack is slow

Python inspect.stack is slow Question: I was just profiling my Python program to see why it seemed to be rather slow. I discovered that the majority of its running time was spent in the inspect.stack() method (for outputting debug messages with modules and line numbers), at 0.005 seconds per call. This seems rather high; is …

Total answers: 3

Get full package module name

Get full package module name Question: For verbose debug messages in my application I’m using a function that returns a helpful prefix. Consider the following example: import inspect def get_verbose_prefix(): “””Returns an informative prefix for verbose debug output messages””” s = inspect.stack() module_name = inspect.getmodulename(s[1][1]) func_name = s[1][3] return ‘%s->%s’ % (module_name, func_name) def awesome_function_name(): …

Total answers: 4

Using a dictionary to select function to execute

Using a dictionary to select function to execute Question: I am trying to use functional programming to create a dictionary containing a key and a function to execute: myDict={} myItems=(“P1″,”P2″,”P3″,….”Pn”) def myMain(key): def ExecP1(): pass def ExecP2(): pass def ExecP3(): pass … def ExecPn(): pass Now, I have seen a code used to find the …

Total answers: 11