Node object

class Node(transaction, id)

Representation of a tree node and entrypoint for local tree operations.

It’s a thin wrapper around the underlaying core functions. It does not contain any data besides the database ID and must therefore query the database every time the value of an attribute like parent has been requested. This decision has been made to avoid race conditions when working in concurrent or distributed environments, but comes at the cost of slower runtime execution speeds. If this becomes a problem for you, grab the the corresponding libtree.core.node_data.NodeData object via libtree.node.Node.node_data.

This object is tightly coupled to a libtree.transaction.Transaction object. It behaves like a partial which passes a database cursor and node ID into every libtree.core function. It also has a few convenience features like attribute access via Python properties and shorter method names.

Parameters:
  • transaction (Transaction) – Transaction object
  • id (uuid4) – Database node ID
__len__()

Return amount of child nodes.

__eq__(other)

Determine if this node is equal to other.

ancestors

Get bottom-up ordered list of ancestor nodes.

children

Get list of immediate child nodes.

delete()

Delete node and its subtree.

descendants

Get set of descendant nodes.

get_child_at_position(position)

Get child node at certain position.

Parameters:position (int) – Position to get the child node from
has_children

Return whether immediate children exist.

id

Database ID

inherited_properties

Get inherited property dictionary.

insert_child(properties=None, position=-1, id=None)

Create a child node and return it.

Parameters:
  • properties (dict) – Inheritable key/value pairs (see Property functions)
  • position (int) – Position in between siblings. If 0, the node will be inserted at the beginning of the parents children. If -1, the node will be inserted the the end of the parents children.
  • id (uuid4) – Use this ID instead of automatically generating one.
move(target, position=-1)

Move node and its subtree from its current to another parent node. Raises ValueError if target is inside this nodes’ subtree.

Parameters:
  • target (Node) – New parent node
  • position (int) – Position in between siblings. If 0, the node will be inserted at the beginning of the parents children. If -1, the node will be inserted the the end of the parents children.
node_data

Get a libtree.core.node_data.NodeData object for current node ID from database.

parent

Get parent node.

position

Get position in between sibling nodes.

properties

Get property dictionary.

recursive_properties

Get inherited and recursively merged property dictionary.

set_position(new_position)

Set position.

Parameters:position (int) – Position in between siblings. If 0, the node will be inserted at the beginning of the parents children. If -1, the node will be inserted the the end of the parents children.
set_properties(properties)

Set properties.

Parameters:properties (dict) – Property dictionary
swap_position(other)

Swap position with other position.

Parameters:other (Node) – Node to swap the position with
update_properties(properties)

Set properties.

Parameters:properties (dict) – Property dictionary