import re


def camel_to_snake(name, upper=True):
    """
    Convert string from camel case to snake style.
    """
    name = re.sub("^([A-Z])|(?<!_)([A-Z])|([A-Z])", lambda m:
            m.group(1).lower() if m.group(1) else (('_' + m.group(2).lower()) if m.group(2) else m.group(3).lower()), name)
    if upper:
        name = name.upper()
    return name