shorten "compress" long integer to short ascii.

Vincent Davis vincent at vincentdavis.net
Thu Nov 19 22:45:08 EST 2015


My goal is to shorten a long integer into a shorter set of characters.
Below is what I have which gets me about a 45-50% reduction. Any suggestion
on how to improve upon this?
I not limited to ascii but I didn't see how going to utf8 would help.
The resulting string needs to be something I could type/paste into twitter
for example.

On a side note string.punctuation contains "\\" what is \\ ?

import string
import random

# Random int to shorten
r = random.getrandbits(300)
lenofr = len(str(r))

l = string.ascii_lowercase + string.ascii_uppercase +
'!"#$%&\'()*+,-./:;<=>?@[]^_`{|}~'
n = [str(x) for x in list(range(10,93))]
decoderdict = dict(zip(l, n))
encoderdict = dict(zip(n, l))

def encoder(integer):
    s = str(integer)
    ls = len(s)
    p = 0
    code = ""
    while p < ls:
        if s[p:p+2] in encoderdict.keys():
            code = code + encoderdict[s[p:p+2]]
            p += 2
        else:
            code = code + s[p]
            p += 1
    return code

def decoder(code):
    integer = ""
    for c in code:
        if c.isdigit():
            integer = integer + c
        else:
            integer = integer + decoderdict[c]
    return int(integer)

short = encoder(r)
backagain = decoder(short)

print(lenofr, len(short), len(short)/lenofr, r==backagain)



More information about the Python-list mailing list