Skip to content

Instantly share code, notes, and snippets.

@highfestiva
Last active February 17, 2016 20:43
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 highfestiva/eaeb04fc786d0f124f7b to your computer and use it in GitHub Desktop.
Save highfestiva/eaeb04fc786d0f124f7b to your computer and use it in GitHub Desktop.
A nifty little utility to compress arbitrary data or text into a string.
#!/usr/bin/env python3
from sys import byteorder
from zlib import compress,decompress
def stringify(b):
i = int().from_bytes(b, byteorder)
s = ''
while i:
j = i%95
s += chr(32+j)
i //= 95
return s
def unstringify(s):
i = 0
for c in reversed(s):
if ord(c) < 32:
continue
i *= 95
i += ord(c)-32
b = i.to_bytes((i.bit_length()+7)//8, byteorder)
return b
def chrfix_single(c):
return c if c not in "'\\" else '\\'+c
def chrfix_triple(c):
return c if c != "\\" else '\\\\'
def pack(d):
b = compress(d if type(d)==bytes else str(d).encode())
return stringify(b)
def packr(d):
'''Converts to single quote string.'''
return "'" + ''.join(chrfix_single(c) for c in pack(d)) + "'"
def packrr(d):
'''Converts to triple quote string.'''
return "'''" + ''.join(chrfix_triple(c) for c in pack(d)) + "'''"
def unpack(s):
b = unstringify(s)
return decompress(b).decode()
if __name__ == '__main__':
assert unstringify(stringify(b'something')) == b'something'
assert unpack(pack('packed2bits')) == 'packed2bits'
print('small string ratio:', len(pack('a very tiny string'))/len('a very tiny string'))
print(packr('var = [3,4,92]'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment