polymerist.genutils.trees.treebase

Interfaces for encoding arbitrary classes into tree-like data structures

Attributes

T

Classes

NodeCorrespondence

Abstract base for implementing how to build an anytree Node tree for an arbitrary class

Functions

compile_tree_factory(→ Callable[[T, Optional[int], ...)

Factory method for producing a tree-generating function from a NodeCorrespondence

Module Contents

polymerist.genutils.trees.treebase.T
class polymerist.genutils.trees.treebase.NodeCorrespondence[source]

Bases: abc.ABC, Generic[T]

Abstract base for implementing how to build an anytree Node tree for an arbitrary class

abstractmethod name(obj: T) str[source]

Define how to obtain a string name

abstractmethod has_children(obj: T) bool[source]

Define how to check if an object can produce children in the first place before attempting to do so

abstractmethod children(obj: T) Iterable[T] | None[source]

Define how to obtain node children from an instance Should return NoneType if the instance is “leaf-like”

polymerist.genutils.trees.treebase.compile_tree_factory(node_corresp: NodeCorrespondence[T], class_alias: str | None = None, obj_attr_name: str | None = None, exclude_mixin: polymerist.genutils.filters.Filter[T] | None = None) Callable[[T, int | None, polymerist.genutils.filters.Filter[T] | None], anytree.node.Node][source]

Factory method for producing a tree-generating function from a NodeCorrespondence

Parameters:
  • node_corresp (NodeCorrespondence[T]) – Definition of a correpondence between an arbitrary type and a Tree Node

  • class_alias (str (optional)) – Name of the corresponding class to inject into docstring If not provided, will default to the __name__ of the class wrapped by node_corresp

  • obj_attr_name (str (optional)) – The name of the Node attribute to which a copy of the class instance should be bound If not provided, will default to the value of class_alias

  • exclude_mixin (Filter[T] (optional)) – An optional “master” filter to mix into any

Returns:

compile_tree – Factory function which takes an instance of type T, builds a Tree from it, and returns the root Node

Return type:

Callable[[T, Optional[int], Optional[Filter[T]]], Node]