Python Forum

Full Version: 32 ASCII abbreviations as variables
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
32 ASCII abbreviations as variables assigned with their ASCII code values. this can help make code that uses ASCII control character codes more mnemonic and self documenting.
NUL,SOH,STX,ETX,EOT,ENQ,ACK,BEL,BS,TAB,NL,VTAB,FF,CR,SO,SI,DLE,DC1,DC2,DC3,DC4,NAK,SYN,ETB,CAN,EM,SUB,ESC,FS,GS,RS,US = range(32)
you might want to split up that long line.
it would be much better to use a dictionary, quick, untested example:
ascii_ctrl_characters = {
    '0x00': 'NUL',
    '0x01': 'SOH',
    ...
}
# or if indexed by name:
ascii_ctrl_characters = {
    'NUL': '0x00',
    'SOH': '0x01',
    ...
}

print(f"{ascii_ctrl_characters['SOH']}")
i have done that, except with the codes being ints.