Finding function declarations in Ubuntu on Python
-
08-07-2021 - |
Question
I have a vaguely defined function of a class Graph of a module I call gt (it is graph-tool). so i declare g = gt.graph() then want to use g.degree_property_map but do not know how. Therefore I want to see where in code g.degree_property_map or in this case just the function, is defined. How can I find that? I'm working on command line on a vm.
Thanks
For reference the library in question is graph-tool - http://projects.skewed.de/graph-tool/
Also I am currently importing it using from graph_tool.all import * . that is of course somewhat of a problem.
Solution
If you open interactive python (type python
and hit ENTER on the command line), you should be able to run the command help(<graph's module name>)
, then, under the FILE
section of the help documentation that is generated, you should see the absolute path to the code you are interested in.
For example, I just ran:
import numpy
help(numpy)
# Returned documentation containing:
# FILE
# /usr/lib/python2.7/dist-packages/numpy/__init__.py
Also,
import my_module # A module I just created that contains the "Line" class
help(my_module)
# Returned documentation containing:
# FILE
# /home/<my user name>/Programming/Python/my_module.py
OTHER TIPS
You could use inspect.getsource(gt.Graph.degree_property_map)
. (You have to import inspect
.)
Of course, what you pass into getsource()
will change depending on how you imported Graph
. So if you used from graphtools.all import *
, you'd just need to use inspect.getsource(Graph.degree_property_map)
.
If it is a normal function (not a builtin, ufunc, etc) you can try using the func_code
attribute
For example:
>>> inspect.iscode
<function iscode at 0x02EAEF30>
>>> inspect.iscode.func_code
<code object iscode at 02EB2B60, file "C:\Python27\lib\inspect.py", line 209>
Never mind I just did help(graph_tool) and manually jumped through code. Thanks for the help though!