42 questions
0
votes
0
answers
16
views
inspect module gives excessive type annotation for fastapi swagger
I'm trying specify annotations with inspect module:
import inspect
from inspect import Parameter
from fastapi import FastAPI, Query
from fastapi.params import Depends
from pydantic import BaseModel
...
3
votes
4
answers
374
views
inspect.signature: invalid method signature for lambda in class member
The following code throws a ValueError ('invalid method signature')
import inspect
class Foo:
boo = lambda: 'baa'
print(inspect.signature(Foo().boo))
Why? Changing it to boo = lambda x: 'baa'...
0
votes
1
answer
119
views
How can I get the _true_ signature of a Python function?
tldr;
inspect.signature gives us a function's signature, but this signature can lie (as assigning to the __signature__ attribute of a function will reveal.
Is there a python function (builtin if ...
1
vote
2
answers
115
views
Pythons inspect get source returning unrelated source
I need to get the source of a function 'get_id' and inspect.getsource() is returning the source of the function 'update_creds' and I have no idea why
Some specifics about the environment: Its running ...
0
votes
1
answer
467
views
Using weights and biases for storing pytorch experiments of different architectures
following the basic pytorch tutorial, I'm trying to experiment with different CNN architectures (different number of layers, channels per layer, etc.) and I want to be organized so I'm trying to use ...
0
votes
0
answers
240
views
Inspect find class in stack
I need to get the class (not just the name, the real class ref) from an inspect stack return.
Say i have this:
def methodthatneedcls():
cls = ### code here
class excls:
def somemethod():
...
1
vote
1
answer
132
views
python's inspect.getfile returns "<string>"
Consider this code:
from sqlalchemy import exists
import inspect
print(inspect.getfile(exists))
# Effectively calls:
print(exists.__code__.co_filename)
On 2 systems I've tested it on it prints:
<...
0
votes
0
answers
147
views
inspect.getmembers doesn't return member until a run a help()
getmembers from inspect doesn't return all the members. I am using this on a locally installed package, example code:
from inspect import getmembers, ismodule
import xyz
getmembers(xyz, ismodule) # []...
3
votes
0
answers
289
views
How to get the source code for property in python
I want to programmatically get the source code for a given class property (e.g., pandas.DataFrame.iloc). I tried using inspect.findsource(), which works fine for classes and functions. However, it ...
14
votes
3
answers
2k
views
How to find out if (the source code of) a function contains a loop?
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 < 3:
...
15
votes
2
answers
13k
views
How print every line of a python script as its being executed (including the console)?
I'd like to print every line of python script as it's being executed, as well as the log from the console as every line is being executed.
For example, for this script:
import time
print 'hello'
...
5
votes
0
answers
533
views
What is the difference between stack frame and execution frame?
I'm having trouble understanding the differences between stack frames and execution frames, mostly with respect to the traceback and inspect modules (in Python 3).
I thought they were the same but ...
4
votes
1
answer
2k
views
Getting all functions of python package
I have a multimodule Python package which has one file with class MyClass and few files with functions (one file can contain more than 1 function).
I tried to get all functions in this package.
...
40
votes
7
answers
51k
views
Get current function name from inside that function using Python
I want to log all the names of functions where my code is going. It does not matter who is calling the function.
import inspect
def whoami():
return inspect.stack()[1][3]
def foo():
print(...
54
votes
2
answers
15k
views
What is the difference between a stack and a frame?
What is the difference between:
>>> import inspect
>>> print(inspect.getouterframes(inspect.currentframe()))
[(<frame object at 0x8fc262c>, '<stdin>', 1, '<module>',...
11
votes
0
answers
3k
views
List modules in namespace package
I'm trying to get Python to list all modules in a namespace package.
I have the following file structure:
cwd
|--a
| `--ns
| |--__init__.py
| `--amod.py
|--b
| `--ns
| |--__init__.py
| ...
102
votes
12
answers
216k
views
Using a dictionary to select function to execute
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():
...
101
votes
7
answers
52k
views
How to get all methods of a Python class with given decorator?
How to get all methods of a given class A that are decorated with the @decorator2?
class A():
def method_a(self):
pass
@decorator1
def method_b(self, b):
pass
@decorator2
...
13
votes
3
answers
3k
views
How can I programmatically change the argspec of a function in a python decorator?
Given a function:
def func(f1, kw='default'):
pass
bare_argspec = inspect.getargspec(func)
@decorator
def func2(f1, kw='default'):
pass
decorated_argspec = inspect.getargspec(func2)
How can ...
17
votes
5
answers
8k
views
How does inspect.ismethod differentiate methods and functions?
I'm trying to get the name of all methods in my class.
When testing how the inspect module works, i extraced one of my methods by obj = MyClass.__dict__['mymethodname'].
But now inspect.ismethod(obj)...
430
votes
13
answers
343k
views
How can I get a list of all classes within current module in Python?
I've seen plenty of examples of people extracting all of the classes from a module, usually something like:
# foo.py
class Foo:
pass
# test.py
import inspect
import foo
for name, obj in inspect....