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 = <sections.types.SectionNoneType object>, gettype: GetType = 'default', default: typing.Any = <sections.types.SectionNoneType object>) 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, ‘self’. 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 ‘self’ will only search for attrs in self, and will never wrap the attr in an iterable form like the dict/list/iter options.

searches for attributes only in self. Setting to ‘nearest’ also searches through

Parameters

default – If not provided, AttributeError will be raised if attr name not found. If given, return default if attr name not found.

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__(names: Any) Section

x.__getitem__(y) <==> x[y]

__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 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.  Remove all items from od.
property cls: Type[Section]

The unique structure-wide class of each node.

copy() a shallow copy of od
property descendants: Section

Similar to leaves except all nodes in structure are returned.

property descendants_iter: iter

Return iterator that iterates through self and all self’s descendants.

descendants_str() 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.

property flat: Section

Synonym for descendants.

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

Not supported.

get(name: Any, default: Optional[Any] = None) None

Return the value for key if key is in the dictionary, else default.

insert(i: int, child: Section) None

Insert child’ at index `i of dict. The key for child will be taken from child’s name attribute. If i is negative, insert at end of dict.

insertitem(i: int, name: Any, child: Section) None

Insert child’ at index `i of dict. The key for child will be taken from child’s name attribute. If i is negative, insert at end of dict.

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.

property leaves_iter: iter

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

move_to_end(name: Any, last: bool = True) None

Move an existing child to either end of ordered children dict.

property node: Section

Return a shallow copy of self with no children. Useful for searching for attributes only in self.

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) 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_or_i: Union[Any, int]) Any

Remove child name_or_i from self. If there is no child with that name and name_or_i is int, remove child in position name_or_i.

popitem(last=True) Tuple[Any, Any]

Remove last added child from self.

property sections: Section

A synonym for property children.

setdefault(name: Any, default: Section) Any

If self has a child name, return it. If not, set child default with name name default and return default.

structure_change()[source]

Will be called every time there is a change in structure, i.e. whenever a node is added or removed or rearranged in child order. Meant for use when overriding.

update(other: Section) None

Add all children from other to self.

values() Iterable[Any]

Return iterator over children.