def key_name_to_key_number(key_string):

    if not isinstance(key_string, str):
        raise ValueError('key_string is not String')
    if not key_string[1] in ['#', 'b', ' ']:
        raise ValueError(
            '2nd character {} is not #, b nor blank_space'.format(
                key_string[1]))

    # split key and mode, ignore case
    key_str, mode_str = key_string.split()
    key_str = key_str.upper()
    mode_str = mode_str.lower()

    # instantiate default pitch classes and supported modes
    note_names_pc = {'C': 0, 'D': 2, 'E': 4,
                     'F': 5, 'G': 7, 'A': 9, 'B': 11}
    modes = ['major', 'minor']

    # check that both key and mode are valid
    if not key_str[0] in note_names_pc:
        raise ValueError('Key {} is not recognized'.format(key_str[0]))
    if mode_str not in modes:
        raise ValueError('Mode {} is not recognized'.format(mode_str))

    # lookup dictionary
    key_number = note_names_pc[key_str[0]]

    # if len is 2, has a sharp or flat
    if len(key_str) == 2:
        if key_str[1] == '#':
            key_number += 1
        else:
            key_number -= 1

    # circle around 12 pitch classes
    key_number = key_number % 12

    # offset if mode is minor
    if mode_str == 'minor':
        key_number += 12

    return key_number
Convert a correctly formated string key to key number
dataset/ETHPy150Open craffel/pretty-midi/pretty_midi/utilities.py/key_name_to_key_number