Skip to content

Instantly share code, notes, and snippets.

@adoc
Last active September 18, 2015 20:48
Show Gist options
  • Save adoc/28847046187c3f11d483 to your computer and use it in GitHub Desktop.
Save adoc/28847046187c3f11d483 to your computer and use it in GitHub Desktop.
silly encoder ideas
def bit_count(byte_or_num):
bits = 0
if isinstance(bytes, byte_or_num):
n = ord(byte_or_num)
else:
n = byte_or_num
while n > 1: # n >= 1 ????
n //= 2
bits += 1
return bits
class SillyEncoder(object):
__decodeables__ = ('abcdefghijklmnopqrstuvwxyz'
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
'1234567890')
def __init__(self):
self.__decodeables = self.__decodeables__
self.__bw = len(self.__decodeables)
assert self.__bw < 256, "O.o"
self.__bw_bit_count = bit_count(self.__bw)
def __iter_encoded_bytes(self, bytestring):
while bytestring:
byte = ord(bytestring[0])
rep_bytes = byte >> self.__bw_bit_count
bytestring >>= self.__bw_bit_count
yield chr
def encode(self, bytestring):
return str(self.__iter_encoded_bytes())
def decode(self, string):
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment