<commit_before># -*- coding: utf-8 -*-
from __future__ import division

from ethereum.utils import encode_hex

from raiden.utils import keccak


def hash_pair(first, second):
    if first > second:
        return keccak(second + first)
    return keccak(first + second)


class NoHash32Error(Exception):
    pass


def merkleroot(elements, proof=None, first=True):
    """
    Args:
        elements (List[str]): List of hashes that make the merkletree.
        proof (list): Empty or with the element for which a proof shall be
            built, proof will be in proof.

            The proof contains all elements between `element` and `root`.
            If on all of [element] + proof is recursively hash_pair applied one
            gets the root.

    Returns:
        str: The root element of the merkle tree.
    """
    if first:
        elements = build_lst(elements)

    if not elements:
        return ''

    proof = proof or [None]
    searching = proof.pop()
    assert searching is None or searching in elements
    out = []
    for i in range(len(elements) // 2):
        first = elements[i * 2]
        second = elements[i * 2 + 1]

        hash_ = hash_pair(first, second)
        if first == searching:
            proof.extend((second, hash_))
        elif second == searching:
            proof.extend((first, hash_))
        out.append(hash_)

    if len(elements) % 2:
        hash_ = elements[-1]
        out.append(hash_)
        if hash_ == searching:
            proof.append(hash_)

    if len(out) > 1:
        return merkleroot(out, proof, False)

    if searching:
        proof.pop()  # pop root
    return out[0]


def build_lst(elements):
    result = list()

    for item in set(elements):
        if item:
            if len(item) != 32:
                raise NoHash32Error()

            result.append(item)

    result.sort()<commit_msg>clean code in raiden.mtree, get rid of internal 'first' argument<commit_after># -*- coding: utf-8 -*-
from __future__ import division

from ethereum.utils import encode_hex

from raiden.utils import keccak


def hash_pair(first, second):
    if first > second:
        return keccak(second + first)
    return keccak(first + second)


class NoHash32Error(Exception):
    pass


def _merkleroot(elements, proof=None):
    proof = proof or [None]
    searching = proof.pop()
    assert searching is None or searching in elements
    out = []
    for i in range(len(elements) // 2):
        first = elements[i * 2]
        second = elements[i * 2 + 1]

        hash_ = hash_pair(first, second)
        if first == searching:
            proof.extend((second, hash_))
        elif second == searching:
            proof.extend((first, hash_))
        out.append(hash_)

    if len(elements) % 2:
        hash_ = elements[-1]
        out.append(hash_)
        if hash_ == searching:
            proof.append(hash_)

    if len(out) > 1:
        return _merkleroot(out, proof)

    if searching:
        proof.pop()  # pop root
    return out[0]


def merkleroot(elements, proof=None):
    """
    Args:
        elements (List[str]): List of hashes that make the merkletree.
        proof (list): Empty or list with a single element for which a proof shall be
            built. The resulting proof will be in proof, i.e. the list
            is modified in place.

            The proof contains all elements between `element` and `root`.
            If on all of [element] + proof is recursively hash_pair applied one
            gets the root.

    Returns:
        str: The root element of the merkle tree.
    """
    elements = build_lst(elements)
    if not elements:
        return ''
    return _merkleroot(elements, proof=proof)


def build_lst(elements):
    result = list()

    for item in set(elements):
        if item:
            if len(item) != 32:
                raise NoHash32Error()

            result.append(item)

    result.sort()
{'fraction_non_alphanumeric': 0.09419551934826884, 'fraction_numerical': 0.007637474541751527, 'mean_word_length': 2.1207307386814933, 'pattern_counts': {'":': 0, '<': 3, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5647828', 'n_tokens_mistral': 1171, 'n_tokens_neox': 1119, 'n_words': 391}