PathNode class

class pathtreelib.PathNode(path, parent=None)

The PathNode class describe a tree node containing information associated to a path.

The children of a node are sorted by: type (directories first, then files), path (alphabetically on the file/dir name).

Parameters:
  • path (Path) –

  • parent (PathNode | None) –

path

the path associated to the node

parent

the parent directory of the node, as a PathNode object

children

the children files/directories of the node, as list of PathNode objects

data

a dictionary that can contain custom properties of the node (e.g. height, subtree size, …)

__init__(path, parent=None)

Create a new path node and all its subtree.

Parameters:
  • path (Path) – the path of the node

  • parent (PathNode | None) – the path of the parent node (if None, it is a root node)

Return type:

None

__str__()

Return a string describing the node properties.

Print the node name then the properties.

Returns:

A string describing the node.

Return type:

str

compute_bottom_up_property(property_name, base_func, recursive_func)

Compute a property of bottom-up type in the subtree of the node.

The bottom-up properties are recursively computed from the leaves (files) to the root: when the node is a leaf the proerty can be computed directly (without involving other nodes), when the node is an inner node the property is computed based also on the children nodes properties. Hence there are 2 functions that must be specified: the base case function (for leaves), the recursive function (for inner nodes).

Parameters:
  • property_name (str | PathTreeProperty) – the name of the property (used as key in property dict)

  • base_func (Callable[[PathNode], Any]) – the function to compute the property on the leaves

  • recursive_func (Callable[[PathNode, list[PathNode]], Any]) – the function to compute the property on the inner nodes (assuming it is already computed on the leaves)

Return type:

None

Parameters of base_func:

  • leaf (PathNode): the current node

Parameters of recursive_func:

  • inode (PathNode): the current node

  • children (list(PathNode)): the list of children of the current node

Examples:

Compute the height.

>>> base_func = lambda node: 0
>>> recursive_func = lambda node, children: 1 + min([
>>>     int(child.property["height"])
>>>     for child in children
>>> ])
>>> root.compute_bottom_up_property("height", base_func, recursive_func)
compute_individual_property(property_name, property_func)

Compute an individual property in the subtree of the node.

The property does not exploit any knowledge on other nodes. However can exploit information stored in the node property dict (e.g. previously computed height).

Parameters:
  • property_name (str | PathTreeProperty) – the name of the property (used as key in property dict)

  • property_func (Callable[[PathNode], Any]) – the function to compute the property on the node

Parameters of property_func:

  • node (PathNode): the current node

Examples:

Compute a flag to identify directories.

>>> property_func = lambda node: node.path.is_dir()
>>> root.compute_individual_property("is_dir", property_func)
compute_top_down_property(property_name, root_func, parent_func)

Compute a property of top-down type in the subtree of the node.

The top-down properties are recursively computed from the root to the leaves: when the node is root the proerty can be computed directly (without involving other nodes), when the node is a not-root node the property is computed based also on the parent node properties. Hence there are 2 functions that must be specified: the root function (for the root), the parent function (for not-root nodes).

Parameters:
  • property_name (str | PathTreeProperty) – the name of the property (used as key in property dict)

  • root_func (Callable[[PathNode], Any]) – the function to compute the property on the root

  • parent_func (Callable[[PathNode, PathNode], Any]) – the function to compute the property on the not-root nodes (assuming it is already computed on the parent)

Return type:

None

Parameters of root_func:

  • root (PathNode): the current node

Parameters of parent_func:

  • node (PathNode): the current node

  • parent (PathNode): the parent of the current node

Examples:

Compute the depth.

>>> root_func = lambda root: 0
>>> parent_func = lambda node, parent: 1 + parent.property["depth"]
>>> root.compute_top_down_property("depth", root_func, parent_func)
copy()

Return a deepcopy of the node.

The pointers to other nodes are copied without modification hence point to the same nodes.

Returns:

A copy of the node.

Return type:

PathNode

is_dir()

Return true if and only if the node is a directory.

Check if the node is a directory.

Returns:

True if and only if the node is a directory.

Return type:

bool

is_file()

Return true if and only if the node is a file.

Check if the node is a file.

Returns:

True if and only if the node is a file.

Return type:

bool

is_inode()

Return true if and only if the node is an inner node.

Check if the node has children nodes.

Returns:

True if and only if the node is an inner node.

Return type:

bool

is_leaf()

Return true if and only if the node is a leaf.

Check if the node has children nodes.

Returns:

True if and only if the node is a leaf.

Return type:

bool

is_root()

Return true if and only if the node is root.

Check if the node has a parent node.

Returns:

True if and only if the node is root.

Return type:

bool

remove_property(property_name)

Remove a property of the node.

Parameters:

property_name (str | PathTreeProperty) – the name of the property to remove.

Returns:

True if the property is successfully removed, false if the property is missing.

Return type:

bool