k3modutil
Utilities for loading and inspecting Python submodules. Load all submodules of a package and build module trees.
k3modutil is a component of pykit3 project: a python3 toolkit set.
Installation
pip install k3modutil
Quick Start
import k3modutil
import mypackage
# Load all direct submodules of a package
submodules = k3modutil.submodules(mypackage)
# {'submod1': <module>, 'submod2': <module>, ...}
# Load all submodules recursively as a tree
tree = k3modutil.submodule_tree(mypackage)
# {'submod1': {'module': <module>, 'children': {...}}, ...}
# Load only leaf modules (no children)
leaves = k3modutil.submodule_leaf_tree(mypackage)
API Reference
k3modutil
submodule_leaf_tree(root_module)
Load all submodules of root_module recursively. And put them in a submodule-leaf
dict. Every key of this dict is a submodules' name, and every value is a
submodule-leaf dict of the submodule the key named, or the submodule itself if
the submodule is not the directory structure.
If no submodule loaded, submodule-leaf dict will be {}.
:param root_module: is a module.
:return: the submodule-leaf dict of root_module.
Or None if root_module is not the directory structure.
Example:
{'submod1': submodule_leaf_tree(
Source code in k3modutil/modutil.py
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | |
submodule_tree(root_module)
Load all submodules of root_module recursively. And put them in a submodule
dict. Every key of this dict is a submodule's name, and every value in this dict
has a 'module' part which is the submodule the key named and a 'children' part which
is the submodule dict of the 'module' part. If the 'module' part has no submodule,
the 'children' part will be assigned to {}. If the 'module' part is not the
directory structure, the 'children' part will be assigned to None.
:param root_module: is a module.
:return: the submodule dict of root_module.
Or None if root_module is not the directory structure.
Source code in k3modutil/modutil.py
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | |
submodules(root_module)
Load all submodules of root_module.
And map these submodules names to submodules.
:param root_module: is a module.
:return: a dict whose keys are name of submodules and values are submodules loaded.
Or {} if no submodule loaded.
Or None if root_module is not the directory structure.
Source code in k3modutil/modutil.py
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | |
License
The MIT License (MIT) - Copyright (c) 2015 Zhang Yanpo (张炎泼)