dimanche 22 octobre 2017

Shorten python logic

Can anyone help improve this code to shorten it? This is used to format text to a file Example: if caps is on make the rest of the keys uppercase.

        log_text = ""
    if event.Key in ["Rshift", "Lshift"]:
        is_Shift = True
    elif event.Key == "Capital":
        is_caps += 1
    elif event.Key == "Back":
        with open(file_name, 'rb+') as f:
            f.seek(-1, os.SEEK_END)
            f.truncate()
    elif event.Key in specials:
        log_text += specials[event.Key]
    elif event.Key in f_keys:
        log_text += " <{}> ".format(event.Key)
    elif event.Key in arrows:
        log_text += arrows[event.Key]
    elif event.Ascii != 0:
        if is_Shift is True:
            if chr(event.Ascii) in numbers:
                log_text += numbers[chr(event.Ascii)]
            elif chr(event.Ascii) in symbols:
                log_text += symbols[chr(event.Ascii)]
            elif chr(event.Ascii).isalpha():
                log_text += chr(event.Ascii).upper()
            else:
                log_text += chr(event.Ascii)
        elif is_caps % 2 != 0:
            if chr(event.Ascii).isalpha():
                log_text += chr(event.Ascii).upper()
            else:
                log_text += chr(event.Ascii)
        else:
            if chr(event.Ascii).isalpha():
                log_text += chr(event.Ascii).lower()
            else:
                log_text += chr(event.Ascii)

Aucun commentaire:

Enregistrer un commentaire