import re


def explode(bus_str):
    """
    Explode a bus into its separate lines.

    This function takes a bus expression like "ADDR[0:3]" and returns
    "ADDR0,ADDR1,ADDR2,ADDR3". It also works if the order is reversed,
    e.g. "ADDR[3:0]" returns "ADDR3,ADDR2,ADDR1,ADDR0". If the input
    string is not a valid bus expression, then the string is returned
    in a one-element list.

    Args:
        bus_str: A string containing a bus expression like "D[0:3]".

    Returns:
        A list of bus lines like ['D0', 'D1', 'D2', 'D3'] or a one-element
        list with the original input string if it's not a valid bus expression.
    """

    bus = re.match(r"^([A-Za-z_]+)\[([0-9]+):([0-9]+)\]$", bus_str)
    if not bus:
        return [bus_str]  # Not a valid bus expression, so return input string.
    bus_name = bus.group(1)
    begin_num = int(bus.group(2))
    end_num = int(bus.group(3))
    dir = [1, -1][int(begin_num > end_num)]  # Bus indexes increasing or decreasing?
    bus_pin_nums = range(begin_num, end_num + dir, dir)
    non_num = "([^0-9]|$)"
    return [bus_name + str(n) + non_num for n in bus_pin_nums]