def _get_expression_levels(expr):
    """
    :returns: dictionary with the level of depth of each part of the expression. Brackets are ignored in the result.
    e.g.: ['A', 'OR', 'B', 'AND', '(', 'A', 'IF', '(', 'NOT', 'C', 'IF', 'D', ')', ')']
    => {0: [0, 1, 2, 3], 1: [5, 6], 2: [8, 9, 10, 11]}
    """
    level = 0
    levels = dict()
    for i, x in enumerate(expr):
        if x == '(':
            level += 1
        elif x == ')':
            level -= 1
        else:
            levels.setdefault(level, []).append(i)
    return levels