<commit_before># Copyright (c) 2016 <NAME>


import json
import uuid
from libtree.core.node_data import NodeData
from libtree.core.positioning import (ensure_free_position,
                                      find_highest_position,
                                      shift_positions)
from libtree.core.query import (get_children, get_descendant_ids, get_node,
                                get_root_node)
from libtree import exceptions


def print_tree(cur, start_node=None, indent='  ', _level=0):
    """
    Print tree to stdout.

    :param start_node: Starting point for tree output.
                       If ``None``, start at root node.
    :type start_node: int, Node, NodaData or None
    :param str indent: String to print per level (default: '  ')
    """
    if start_node is None:
        start_node = get_root_node(cur)

    print('{}{}'.format(indent * _level, start_node))  # noqa

    for child in list(get_children(cur, start_node)):
        print_tree(cur, child, indent=indent, _level=_level + 1)


def insert_node(cur, parent, properties=None, position=None,
                auto_position=True, id=None):
    """
    Create a ``Node`` object, insert it into the tree and then return
    it.

    :param parent: Reference to its parent node. If `None`, this will
                   be the root node.
    :type parent: Node or uuid4
    :param dict properties: Inheritable key/value pairs
                            (see :ref:`core-properties`)
    :param int position: 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.
                         If `auto_position` is disabled, this is just a
                         value.
    :param bool auto_position: See :ref:`core-positioning`
    :param uuid4 id: Use this UUID4.
    """
    if id is None:
        id = str(uuid.uuid4())

    parent_id = None
    if parent is not None:
        parent_id = parent.id

    if properties is None:
        properties = {}

    # Can't run set_position() because the node doesn't exist yet
    if auto_position:
        if type(position) == int and position >= 0:
            ensure_free_position(cur, parent, position)
        else:
            position = find_highest_position(cur, parent) + 1

    sql = """
        INSERT INTO
          nodes
          (id, parent, position, properties)
        VALUES
          (%s, %s, %s, %s);
    """
    cur.execute(sql, (id, parent_id, position, json.dumps(properties)))

    return NodeData(id, parent_id, position, properties)

<commit_msg>Support UUID4 being passed to insert_node()<commit_after># Copyright (c) 2016 <NAME>


import json
import uuid
from libtree.core.node_data import NodeData
from libtree.core.positioning import (ensure_free_position,
                                      find_highest_position,
                                      shift_positions)
from libtree.core.query import (get_children, get_descendant_ids, get_node,
                                get_root_node)
from libtree import exceptions


def print_tree(cur, start_node=None, indent='  ', _level=0):
    """
    Print tree to stdout.

    :param start_node: Starting point for tree output.
                       If ``None``, start at root node.
    :type start_node: int, Node, NodaData or None
    :param str indent: String to print per level (default: '  ')
    """
    if start_node is None:
        start_node = get_root_node(cur)

    print('{}{}'.format(indent * _level, repr(start_node)))  # noqa

    for child in list(get_children(cur, start_node)):
        print_tree(cur, child, indent=indent, _level=_level + 1)


def insert_node(cur, parent, properties=None, position=None,
                auto_position=True, id=None):
    """
    Create a ``Node`` object, insert it into the tree and then return
    it.

    :param parent: Reference to its parent node. If `None`, this will
                   be the root node.
    :type parent: Node or uuid4
    :param dict properties: Inheritable key/value pairs
                            (see :ref:`core-properties`)
    :param int position: 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.
                         If `auto_position` is disabled, this is just a
                         value.
    :param bool auto_position: See :ref:`core-positioning`
    :param uuid4 id: Use this UUID4.
    """
    if id is None:
        id = str(uuid.uuid4())

    parent_id = None
    if parent is not None:
        parent_id = str(parent)

    if properties is None:
        properties = {}

    # Can't run set_position() because the node doesn't exist yet
    if auto_position:
        if type(position) == int and position >= 0:
            ensure_free_position(cur, parent, position)
        else:
            position = find_highest_position(cur, parent) + 1

    sql = """
        INSERT INTO
          nodes
          (id, parent, position, properties)
        VALUES
          (%s, %s, %s, %s);
    """
    cur.execute(sql, (id, parent_id, position, json.dumps(properties)))

    return NodeData(id, parent_id, position, properties)


{'fraction_non_alphanumeric': 0.08738042678440029, 'fraction_numerical': 0.005334805003679176, 'mean_word_length': 2.0493550196298376, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4782958', 'n_tokens_mistral': 1547, 'n_tokens_neox': 1470, 'n_words': 523}