sections

Flexible tree data structures for organizing lists and dicts into sections. https://github.com/trevorpogue/sections

class sections.MetaSection[source]

Parses args and kwds passed to a sections() call or Section instantiation and returns a Section tree structure. Parses node names/keys, separate attrs intended for current node vs child nodes, constructs current node, then recursively repeats for all child nodes.

class sections.Section(*args: SectionKeysOrObjects, parent: Optional[SectionParent] = None, **kwds: SectionAttr)[source]

Objects instantiated by Section are nodes in a sections tree structure. Each node has useful methods and properties for organizing lists/dicts into sections and for conveniently accessing/modifying the sub-list/dicts from each section/subsection.

property children: Section

Get self nodes’s children. Returns a Section node that has no public attrs and has shallow copies of self node’s children as its children. This can be useful if self has an attr attr but you want to access a list of the childrens’ attr attr, then write section.children.attr to access the attr list.

property cls: Type[Section]

The unique structure-wide class of each node.

deep_str(breadthfirst: bool = True, _topcall: bool = True)str[source]

Print the output of :met:`node_str <Section.node_str` for self and all of its descendants.

property entries: Section

A synonym for property leaves.

get_nearest_attr(name: str, gettype: GetType = 'default')Union[Any, List[Any], Iterable[Any], AnyDict][source]

Default method called by __call__. See the docstring of __call__ for the full details. :meta private:

get_node_attr(name: str, gettype: GetType = 'default')Any[source]

Return attribute name only from self as opposed to searching for attribute attr in descendant nodes as well.

property ischild: bool

True iff self node has a parent.

property isleaf: bool

True iff self node has no children.

property isparent: bool

True iff self node has any children.

property isroot: bool

True iff self node has not parent.

property leaves: Section

Get all leaf node descendants of self. Returns a Section node that has no public attrs and has shallow copies of self node’s leaves as its children. This can be useful if self has an attr attr but you want to access a list of the leaves’ attr attr, then write section.leaves.attr to access the leaf attr list.

property leaves_iter: iter

Return iterator that iterates through all self’s leaf node descendants.

node_str()str[source]

Neatly print the public attributes of the Section node and its class, as well as its types property output.

node_withchildren_fromiter(itr: iter)Section[source]

Perform a general form of the task performed in leaves. Return a Section node with any children referenced in the iterable from the itr argument.

property nofchildren: int

Nunber of children Sections/nodes.

pop(name: Any)Any[source]

Remove child name from self.

popitem()Tuple[Any, Any][source]

Remove last added child from self.

property sections: Section

A synonym for property children.

setdefault(*args: Any, **kwds: Any)Any[source]

Not supported yet. :meta private:

update(*args: Any, **kwds: Any)None[source]

Invalidate descendant attr cache after adding/removing nodes. :meta private: