def mode_accidentals_to_key_number(mode, num_accidentals):

    if not (isinstance(num_accidentals, int) and
            num_accidentals > -8 and
            num_accidentals < 8):
        raise ValueError('Number of accidentals {} is not valid'.format(
            num_accidentals))
    if mode not in (0, 1):
        raise ValueError('Mode {} is not recognizable, must be 0 or 1'.format(
            mode))

    sharp_keys = 'CGDAEBF'
    flat_keys = 'FBEADGC'

    # check if key signature has sharps or flats
    if num_accidentals >= 0:
        num_sharps = num_accidentals / 6
        key = sharp_keys[num_accidentals % 7] + '#' * num_sharps
    else:
        if num_accidentals == -1:
            key = 'F'
        else:
            key = flat_keys[(-1 * num_accidentals - 1) % 7] + 'b'

    # find major key number
    key += ' Major'

    # use routine to convert from string notation to number notation
    key_number = key_name_to_key_number(key)

    # if minor, offset
    if mode == 1:
        key_number = 12 + ((key_number - 3) % 12)

    return key_number
Convert to pretty_midi's given number of accidentals and mode
dataset/ETHPy150Open craffel/pretty-midi/pretty_midi/utilities.py/mode_accidentals_to_key_number