peng/functions.py
def _build_expr(tokens, higher_oplevel=-1, ldelim="(", rdelim=")"):
    """Build mathematical expression from hierarchical list."""
    # Numbers
    if isinstance(tokens, str):
        return tokens
    # Unary operators
    if len(tokens) == 2:
        return "".join(tokens)
    # Multi-term operators
    oplevel = _get_op_level(tokens[1])
    stoken = ""
    for num, item in enumerate(tokens):
        if num % 2 == 0:
            stoken += _build_expr(item, oplevel, ldelim=ldelim, rdelim=rdelim)
        else:
            stoken += item
    if (oplevel < higher_oplevel) or (
        (oplevel == higher_oplevel) and (oplevel in _OP_PREC_PAR)
    ):
        stoken = ldelim + stoken + rdelim
    return stoken
def _build_expr(tokens, higher_oplevel=-1, ldelim="(", rdelim=")"):
    """Build mathematical expression from hierarchical list."""
    # Numbers
    if isinstance(tokens, str):
        return tokens
    # Unary operators
    if len(tokens) == 2:
        return "".join(tokens)
    # Multi-term operators
    oplevel = _get_op_level(tokens[1])
    stoken = ""
    for num, item in enumerate(tokens):
        if num % 2 == 0:
            stoken += _build_expr(item, oplevel, ldelim=ldelim, rdelim=rdelim)
        else:
            stoken += item
    if (oplevel < higher_oplevel) or (
        (oplevel == higher_oplevel) and (oplevel in _OP_PREC_PAR)
    ):
        stoken = ldelim + stoken + rdelim
    return stoken
Build mathematical expression from hierarchical list.