Reference

The following describes the available interface with the Section class, the class representing each node object in a sections tree structure.

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.

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

Run get_nearest_attr. This returns attribute name from self if self contains the attribute in either the singular or plural form for name. Else, try the same pattern for each of self’s children, putting the returned results from each child into a list. Else, raise AttributeError.

Parameters
  • name – The name of the attribute to find in self or self’s descendants

  • gettype – Valid values are ‘default’, ‘hybrid’ list, iter, dict, ‘full_dict’. Setting to ‘default’ uses the value of self.default_gettype for gettype. Setting to ‘hybrid’ returns a list if more than 1 element is found, else returns the non-iterable raw form of the element. Setting to list returns a list containing the attribute values. Setting to iter returns an iterable iterating through the attribute values. Setting to dict returns a dict containing pairs of the containing node’s name with the attribute value. Setting to ‘full_dict’ is faster than dict and returns a dict containing pairs of a reference to each node and its attribute value. ‘full_dict’ output is visually identical to dict for printing purposes, but it will contain all attributes even if some source nodes have duplicate names. The only downside to ‘full_dict’ that the keys cannot be referenced by name like with dict, but all values() are still valid.

Returns

The attribute name of self if present, else an iterable object containing the attribute name formed from the nearest relatives of self. The type of the iterable object depends on gettype.

__getattr__(name: str)Any[source]

Called if self node does not have attribute name, in which case try finding attribute name from __call__.

__getitem__(name: Any)Section[source]

Return child node name of self.

__iter__()Iterable[Section][source]

By default iterate over child nodes instead of their names/keys.

__setattr__(name: str, value: Any, _invalidate_cache=True)None[source]

If value is a list, recursively setattr for each child node with the corresponding value element from the value list.

__setitem__(name: Any, value: Union[Section, AnyDict])None[source]

Add a child name to self. Ensure added children are converted to the same unique Section type as the rest of the nodes in the structure, and update its name to name, and its parent to self.

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: