polymerist.genutils.importutils

Functionality for dynamically importing and inspecting Python modules and packages

Submodules

Attributes

module_tree

Functions

module_hierarchy() → str)

Generates a printable string which summarizes a Python packages hierarchy. Reminiscent of GNU tree output

iter_submodules(→ Generator[types.ModuleType, None, None])

Generates all modules which can be imported from the given toplevel module

module_tree_direct(→ anytree.node.Node)

Produce a tree from the Python package hierarchy starting with a given module

is_package(→ bool)

Determine whether a given Package-like (i.e. str or ModuleType) is a valid Python package

is_module(→ bool)

Determine whether a given Package-like (i.e. str or ModuleType) is a valid Python module

Package Contents

polymerist.genutils.importutils.module_hierarchy(module: types.ModuleType, recursive: bool = True, blacklist: Container[str] | None = None, style: str | anytree.render.AbstractStyle = ContStyle()) str[source]

Generates a printable string which summarizes a Python packages hierarchy. Reminiscent of GNU tree output

Parameters:
  • module (ModuleType) – The “root” module to begin importing from Represented in the Node object returned by this function

  • recursive (bool, default=True) – Whether or not to recursively import modules from subpackages and add them to the tree

  • blacklist (Container[str] (optional)) – List of module names to exclude from tree building If provided, will exclude any modules whose names occur in this list

  • style (str or AbstractStyle) – An element drawing style for the final tree structure printout

Returns:

module_summary – Printable string which displays the package structure

Return type:

str

polymerist.genutils.importutils.iter_submodules(module: types.ModuleType, recursive: bool = True, blacklist: Container[str] | None = None) Generator[types.ModuleType, None, None][source]

Generates all modules which can be imported from the given toplevel module

Parameters:
  • module (ModuleType) – The “root” module to begin importing from Represented in the Node object returned by this function

  • recursive (bool, default=True) – Whether or not to recursively import modules from subpackages and add them to the tree

  • blacklist (Container[str] (optional)) – List of module names to exclude from tree building If provided, will exclude any modules whose names occur in this list

Returns:

submodules – A generator which yields modules in traversal pre-order as they appear wihin the package hierarchy

Return type:

Generator[ModuleType]

polymerist.genutils.importutils.module_tree
polymerist.genutils.importutils.module_tree_direct(module: types.ModuleType, recursive: bool = True, blacklist: Container[str] | None = None) anytree.node.Node[source]

Produce a tree from the Python package hierarchy starting with a given module

Parameters:
  • module (ModuleType) – The “root” module to begin importing from Represented in the Node object returned by this function

  • recursive (bool, default=True) – Whether or not to recursively import modules from subpackages and add them to the tree

  • blacklist (Container[str] (optional)) – List of module names to exclude from tree building If provided, will exclude any modules whose names occur in this list

Returns:

modtree – The root node of the module tree, corresponding to the module object passed to “module”

Return type:

Node

polymerist.genutils.importutils.is_package(package: importlib.resources.Package) bool[source]

Determine whether a given Package-like (i.e. str or ModuleType) is a valid Python package

polymerist.genutils.importutils.is_module(module: importlib.resources.Package) bool[source]

Determine whether a given Package-like (i.e. str or ModuleType) is a valid Python module This will return True for packages, bottom-level modules (i.e. *.py) and Python scripts