Skip to content

Instantly share code, notes, and snippets.

@girasquid
Forked from mduheaume/gist:3409226
Created August 22, 2012 17:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save girasquid/3427762 to your computer and use it in GitHub Desktop.
Save girasquid/3427762 to your computer and use it in GitHub Desktop.
compressing a bunch of id's to construct a url
import random
import string
import base64
import zlib
import bz2
random_ids = [''.join([random.choice(string.ascii_lowercase) for x in range(8)]) for n in range(1000)]
long_string = ','.join(random_ids)
b64_string = base64.urlsafe_b64encode(long_string)
compressed_string = zlib.compress(long_string, 9)
b64_compressed_string = base64.urlsafe_b64encode(compressed_string)
bz2_compressed_string = bz2.compress(long_string, 9)
b64_bz2_compressed_string = base64.urlsafe_b64encode(bz2_compressed_string)
len(long_string)
len(b64_string)
len(compressed_string)
len(b64_compressed_string)
len(bz2_compressed_string)
len(b64_bz2_compressed_string)
@girasquid
Copy link
Author

You'll get slightly better compression if you use bz2 instead of zlib - bz2 will give you back some binary data, but because you're base64ing it you should be fine. My results (bz2 is last set):

8999
12000
5646
7528
5497
7332

@mduheaume
Copy link

Sweet. Thanks man!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment