pedarProbe.node.Node#

class pedarProbe.node.Node[source]#

Bases: Dict

Node class is derived from the dictionary class Dict to realised the basic node’s features.

Note

Class Attributes

self.name str

the name of the node.

self.level int

the level of layer. The root node’s level is 0, its branches’ level are 1, and so on.

self.loc list

from right to left, stores the names of the node, its source node, its source node’s source node, and so on, up to the root node level.

For example, self.loc = ['root', 'S4', 'fast walking', 'trail 1', 'L', 'stance 2']. It represents the location of the node in the node tree.

Example

import pedarProbe as pp
n1 = pp.node.Node()
n1.setup('S4')

n2 = pp.node.Node()
n2.setup('fast walking')

n1.add_branch(n2)
n1.print()  # print the node tree starting from n1
n1['fast walking'].print()  # print the node tree starting from n2
__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.

clean_copy()

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

clear()

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_leaf()

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

items()

keys()

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.

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([name])

Since the dictionary's __init__() method is different from ordinary python class, the initialisation procedure are implemented in setup().

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(name: str = '')[source]#

Since the dictionary’s __init__() method is different from ordinary python class, the initialisation procedure are implemented in setup().

Parameters:

name – the name of the node

Warning

Without calling setup(), the node object doesn’t provide full features of a node.

Example

import pedarProbe as pp
n1 = pp.node.Node()
n1.setup('S4')
add_branch(branch_node: Type[Node]) Type[Node][source]#

Add branch to the node.

Parameters:

branch_node – the node being added as a branch. Its name will be used as its keyword.

Example

import pedarProbe as pp
n1 = pp.node.Node()
n1.setup('S4')

n2 = pp.node.Node()
n2.setup('fast walking')

n1.add_branch(n2)

Then, the n2 node can be accessed with its name:

n1['fast walking']  # access n2

Warning

If the added node’s name is already exist in the branch nodes name list, the newly added node will replace it, with a warning message presented to the prompt.

set_source(source_node: Type[Node])[source]#

Set the source node of the node.

Parameters:

source_node – the node being set as the source branch

Attention

In most cases, the user doesn’t need to use this method. In add_branch(), when a node n2 is set as node n1’s branch, set_source() will be automatically called to set n1 as n2’s source node.

clean_copy() Type[Node][source]#

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

Returns:

the clean deep copy of the node.

Return type:

Node

Attention

In the derived classes of Node, the returned type are the derived classes, rather than the basic class Node.

is_leaf() bool[source]#

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

Returns:

True or False

Return type:

bool

branch_names() Iterable[str][source]#

Return a list of branch nodes’ names.

Return type:

dict_keys

branches() Iterable[Type[Node]][source]#

Return a list of branch nodes objects.

Return type:

dict_values

collect_leaf(nodes: list) Iterable[Node][source]#

Recursively collect all leaf nodes starting from this node.

Parameters:

nodes – A list that will stores the collected leaf 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 leaf 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

leafs = n1.collect_leaf(nodes=[])
print()[source]#

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

Example

n1.print()