Skip to main content

ModuleAttributeTree

ClassSource

ModuleAttributeTree(
walk_method: Callable[[str, dict[str, Any], list[T]], T], root_name: str = "."
)

The attribute tree of a module/package.

This tree reconstructs the member structure of a full package. It can be converted to a serialized dictionary/list.

Arguments

Requires

NameTypeRequired
Description
walk_methodCallable[[str, dict[str, Any], list[T]], T]

A method used to serialize tree nodes. The input arguments are:

  1. The partial name of the node.
  2. The attributes of the current node.
  3. The serialized members of the current node.

The method will returns a serialized object representing the current node.

root_namestrThe name of the root node.

Methods

add

self.add(node_full_name: str, node_attrs: Mapping[str, Any] | None = None)

Add a node to the attribute tree.

Requires

NameTypeRequired
Description
node_full_namestrThe full name like "package.module.member".
node_attrsMapping[str, Any] | NoneAn optional dictionary of attributes attached to the current added node.

serialize

v_1: T = self.serialize()

Serialize the entire tree starting from root.

Returns

NameType
Description
v_1TThe serialized object of the root node, including all nested members.

serialize_unpacked

v_1: list[T] = self.serialize_unpacked()

Serialize the entire tree starting from root, with the root node unpacked.

Different from self.serialize(), this method will treat the root node as a leaf node without members, and put all members of the root as siblings. Therefore, the returned value is always a list.

Returns

NameType
Description
v_1list[T]The list of serialized nodes.