I like to document, particular what is in a module/package that I am working on, or one that already exists.
Here are the results of the functions in my npg (NumPy Geometry) module. I consists of separate scripts (modules) each containing a number of functions.
get_module_info(npg) :-----------------------------------------------------------------: Package: npg Functions/methods: Members: ( 1) : np |
def get_module_info(obj, max_number=100, verbose=True):
"""Get module (script) information, including source code if needed.
Parameters
----------
obj : module (script)
The imported object. It must be either a whole module or a script
that you imported. Import it. It is easier if it is in the same
folder as the script running this function.
verbose : boolean
True, prints the output. False, returns the string.
Requires
--------
>>> from textwrap import dedent, indent
>>> import inspect
Returns
-------
A string is returned for printing. It will be the whole module
so use with caution.
Example
-------
Importing this function from the following module to inspect the module
itself.
>>> from npgeom.npg_utils import get_module_info
>>> get_module_info(npg, False, True)
>>> # No quotes around module name, code=True for module code
"""
def wrapit(_in, counter=None):
"""Wrap a string."""
nmes = _in[1]
if len(nmes) > max_number:
nmes = nmes[:max_number] + [".... snip ...."]
sub = ", ".join([j for j in nmes])
sub = wrap(sub, 75)
txt = "\n".join([j for j in sub])
if counter is None:
return "\n{}\n{}".format(i[0], indent(txt, " "))
s = "\n({:2.0f}) : {}\n{}".format(counter, i[0], indent(txt, " "))
return s
# ----
ln = "\n:{}:\n\n".format("-" * 65)
f1 = "{}Package: {}\nFile: {}"
import inspect
from textwrap import indent, wrap
if not inspect.ismodule(obj):
out = "\nError... `{}` is not a module, but is of type... {}\n"
print(out.format(obj.__name__, type(obj)))
return None
# ----
path_parts = obj.__file__.split("\\")[:-1]
mod_path = "\\".join([i for i in path_parts])
out = []
mem = inspect.getmembers(obj, inspect.ismodule and not inspect.isbuiltin)
func = inspect.getmembers(obj, inspect.isroutine) # isfunction)
clas_ = inspect.getmembers(obj, inspect.isclass)
_a0 = sorted([i[0] for i in func if i[0].startswith("_")]) # dir(m[1])
_a1 = sorted([i[0] for i in func if not i[0].startswith("_")]) # dir(m[1])
_all = _a0 + _a1
_c0 = sorted([i[0] for i in clas_])
out.append(["Functions/methods:", _all])
out.append(["Classes :", _c0])
out.append(["\nMembers:", ""])
for m in mem:
if hasattr(m[1], "__file__"):
if mod_path in m[1].__file__:
r = inspect.getmembers(m[1], inspect.isroutine)
_a = [i[0] for i in r]
_a0 = sorted([i for i in _a if i.startswith("_")])
_a1 = sorted([i for i in _a if not i.startswith("_")])
_all = _a0 + _a1
out.append([m[0], _all])
else:
out.append([m[0], ["package: {}".format(m[1].__package__)]])
# ----
s = ""
for i in out[:3]:
s += wrapit(i, counter=None)
cnt = 1
for i in out[3:]:
s += wrapit(i, counter=cnt)
cnt += 1
args0 = [ln, obj.__name__, obj.__file__]
p0 = f1.format(*args0)
if verbose:
print("{}\n{}".format(p0, s))
# print(s)
else:
return "{}\n{}\n".format(p0, s)
Just don't try it on arcpy... you will have to limit the output. In this case 25 functions maximum per file
get_module_info(arcpy, 25, True) :-----------------------------------------------------------------: Package: arcpy Functions/methods: Members: ( 1) : _base ( 5) : _mp ....... BIG SNIP (70) : utils |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.