pedarProbe.node.DynamicNode#

class pedarProbe.node.DynamicNode[source]#

Bases: Node

Derived from Node to realised the layer layout restructuring feature (restructure()).

Note

Class Attributes

self.loc_map

A dictionary that stores the layer names and its corresponding level, which is also its index in self.loc.

With the self.loc, the Node stores the information of its upper nodes in the node tree. However, to implement layer layout restructuring, it’s necessary to be aware of the structure of its lower nodes’ layout.

Pursuing this goal, in DynamicNode every layer of the whole node tree (from the root node) is designated with a name and the corresponding level value is stored in self.loc_map. Therefore, the DynamicNode is aware of the structure of the whole node tree. The default value of self.loc_map:

self.loc_map = {
    'root': 0,
    'subject': 1,
    'condition': 2,
    'trail': 3,
    'foot': 4,
    'stance': 5,
}
__init__(*args, **kwargs)#

Methods

__init__(*args, **kwargs)

add_branch(branch_node)

Add branch to the node.

branch_names()

Return a list of branch nodes' names.

branches()

Return a list of branch nodes objects.

change_loc_map(start_level, layout)

Change loc_map with a restructured layer layout representation.

clean_copy()

Create and return a deep copy of the node only with its major attributes, including name, loc, and level.

clear()

collect_layer(layer, nodes)

In the node tree starting from this node, recursively collect all nodes of a specific layer.

collect_leaf(nodes)

Recursively collect all leaf nodes starting from this node.

copy()

fromkeys([value])

Create a new dictionary with keys from iterable and values set to value.

get(key[, default])

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

is_layer(layer)

Judgment of whether the node belongs to a specific layer.

is_leaf()

Judgment of whether the node is a leaf node or not.

items()

keys()

layer_layout()

Get the layer layout representation of the node tree starting from this node.

pop(k[,d])

If key is not found, d is returned if given, otherwise KeyError is raised

popitem()

2-tuple; but raise KeyError if D is empty.

print()

Recursively print the structure of the node tree starting from this node.

restructure([layout])

Return the restructured the node tree from this node.

set_source(source_node)

Set the source node of the node.

setdefault(key[, default])

Insert key with a value of default if key is not in the dictionary.

setup(*args, **kwargs)

Compared with setup() of the base class Node, initialisation of the self.loc_map is added.

update([E, ]**F)

If E is present and has a .keys() method, then does: for k in E: D[k] = E[k] If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v In either case, this is followed by: for k in F: D[k] = F[k]

values()

setup(*args, **kwargs)[source]#

Compared with setup() of the base class Node, initialisation of the self.loc_map is added.

is_layer(layer: str) bool[source]#

Judgment of whether the node belongs to a specific layer.

Parameters:

layer – name of the layer.

Returns:

True or False

Return type:

bool

layer_layout() tuple[source]#

Get the layer layout representation of the node tree starting from this node.

Returns:

From left to right stores the names of layers of this node, the branch nodes, the branch nodes’ branch nodes, and so on, down to the leaf node level.

For example, in the default layout, call layer_layout() of the trail layer node will return: ('trail', 'foot', 'stance').

Return type:

tuple

Attention

The layer layout representation is also used for indicating the way to restructure the node tree in restructure().

collect_layer(layer: str, nodes: list) Iterable[Type[DynamicNode]][source]#

In the node tree starting from this node, recursively collect all nodes of a specific layer.

Parameters:
  • layer – name of the layer.

  • nodes – A list that will stores the collected nodes. If it’s not empty, newly collected nodes will be append to it without erasing the existing items.

Returns:

A list of the collected nodes.

Return type:

list

Warning

A list must be passed to nodes. Otherwise the class may use nodes created in the last call of collect_leaf() as the initial value, which may cause incorrect result.

Example

nodes = n1.collect_layer(layer='stance', nodes=[])
change_loc_map(start_level: int, layout: str)[source]#

Change loc_map with a restructured layer layout representation.

Parameters:
  • start_level – the start level of restructuring.

  • layout

    the restructured layer layout representation.

    Tip

    For more information see layer_layout().

Attention

This method is automatically called in restructure().

restructure(layout: tuple = ('root', 'subject', 'condition', 'trail', 'foot', 'stance')) Type[DynamicNode][source]#

Return the restructured the node tree from this node.

Attention

The restructured the node tree is return, while the original node tree remains unchanged. This design is based on the fact that, in restructuring, some layers may be compress (aka flatten). In this case, the restructuring is irreversible, therefore it’s better to keep the original node tree as a backup.

Parameters:

layout

the restructured layer layout representation.

Tip

For more information see layer_layout().

Example

To implement the node tree restructuring, it’s better to check the layer layout of the node to be restructured:

print(n1.layer_layout)

For example, ('root', 'subject', 'condition', 'trail', 'foot', 'stance'). Assume that we’d like to make 'condition' layer directly under :code:’root’ layer, compress all other layer as one layer, and named the compressed layer as 'compress':

n2 = n1.restructure(layout=('root', 'condition', 'compress'))

Now we have the restructured n2 and the original n1 remains unchanged.