Skip to content

k3modutil

Action-CI Documentation Status Package

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( submod1) or submod1 }

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
def 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( <module> submod1) or <module> submod1 }
    """
    rst = submodules(root_module)
    if rst is None:
        return None

    for name, mod in rst.items():
        children = submodule_leaf_tree(mod)
        if children is None:
            rst[name] = mod
        else:
            rst[name] = children

    return rst

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
def 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.
    """
    """Example:
    {'submod1': {'module': <module> submod1,
     'children': submodule_tree(<module> submod1),
    },
    }
    """
    rst = submodules(root_module)
    if rst is None:
        return None

    for name, mod in rst.items():
        children = submodule_tree(mod)
        rst[name] = {"module": mod, "children": children}

    return rst

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
def 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.
    """
    mod_path = root_module.__file__

    fn = os.path.basename(mod_path)
    pathname = os.path.dirname(mod_path)
    if fn not in ("__init__.py", "__init__.pyc"):
        return None

    rst = {}
    for _, name, _ in pkgutil.iter_modules([pathname]):
        full_name = root_module.__name__ + "." + name
        mod = importlib.import_module(full_name)
        rst[name] = mod

    return rst

License

The MIT License (MIT) - Copyright (c) 2015 Zhang Yanpo (张炎泼)