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.

For argument gettype, Setting to ‘default’ uses the value of self.default_gettype for gettype (its default is ‘hybrid’). 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 except that it will contain all attributes even if some source nodes have duplicate names. The only downside to ‘full_dict’ is that the keys cannot be referenced by name like with dict, but all values() are still valid.

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’. See method’s description body above for explanation of what each value does.

Returns

An iterable or non-iterable form of the attribute name formed from self or descendant nodes. Depends on the value given to gettype.

__getattr__(name: str)Any

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

__getitem__(name: Any)Section

Return child node name of self.

__iter__()Iterable[Section]

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

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

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

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 all_leaves: Section

This method differs from leaves in that it will return all leaves even if some have duplicate names/keys. However, unlike the leaves property, you cannot access the returned leaves through keys in the form structure.all_leaves[‘key/name’].

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.

clear()None

Not supported.

property cls: Type[Section]

The unique structure-wide class of each node.

copy()None

Not supported.

deep_str(breadthfirst: bool = True)str

Print the output of node_str <Section.node_str() for self and all of its descendants.

Parameters

breadthfirst – Set True to print descendants in a breadth-first pattern or False for depth-first.

property entries: Section

A synonym for property leaves.

fromkeys(*args: Any, **kwds: Any)None

Not supported.

get(*args: Any, **kwds: Any)None

Not supported.

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

Default method called by __call__. See the docstring of __call__ for the full details of what this method does.

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

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 no parent.

items()Tuple[Iterable[Any], Iterable[Any]]

Return iterator over child names and children.

keys()Iterable[Any]

Return iterator over child names.

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.

NOTE: This will exclude any leaves with duplicate names/keys. To avoid this use all_leaves.

property leaves_iter: iter

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

node_str()str

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

node_withchildren_fromiter(itr: iter, all_nodes=False)Section

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

Remove child name from self.

popitem()Tuple[Any, Any]

Remove last added child from self.

property sections: Section

A synonym for property children.

setdefault(*args: Any, **kwds: Any)Any

Not supported.

update(*args: Any, **kwds: Any)None

Not supported.

values()Iterable[Any]

Return iterator over chilren.