pythonuuencode

Does python provide a list of special characters for uuencoding?


I can find the uuencode character mappings on wikipedia. Does python have a means to loop through this list?

for x in [builtin_uuencode_mappings]:
    print(x)

I would like to focus on special characters such as "!@#$" and so on.


Solution

  • Python already has built-in support for encoding and decoding uuencoded messages.

    from codecs import encode  # decode also works
    print(encode("my message", 'uu'))
    # -> 'begin 666 <data>\n*;7D@;65S<V%G90  \n \nend\n'
    

    Internally python uses the binascii package to encode or decode the message line by line. We can use that to encode a single byte or even all bytes in the range(64) (because uuencoding tranforms 6bit into an ascii character: 2**6 == 64).

    To generate all necessary bit patterns we can count to 64 and shift the result by 2 bit to the left. That way the highest 6 bits count from 0 to 64. Then it's just a matter of converting that into python bytes, uuencode them and extract the actual character.

    In python2

    from binascii import b2a_uu
    for byte in range(64):
        pattern = chr(byte << 2)  # str and bytes are identical in python2
        encoded = b2a_uu(pattern)
        character = encoded[1]  # encoded[0] is the character count in that line
        print "{:2} -> {!r}".format(byte, character)
    

    In python3 the first part is a little bit ugly.

    from binascii import b2a_uu
    for byte in range(64):
        pattern = bytes([byte << 2])  # chr().encode() will not work!
        encoded = b2a_uu(pattern)
        character = chr(encoded[1]) 
        print(f"{byte:2} -> {character!r}")
    

    Thanks to Mark Ransom who explained why the bit shifting actually works.