def decompress(data, tree): binary = BitArray() binary.bytes = data binary = binary.bin text = [] index = 0 while True: try: c, binary, index = get_char(binary, tree, index) except IndexError: break text.append(c) return "".join(text)
def encode(word): # a dictionnary for all possible symbol symbols = {} # counts the number of occurences each symbol for letter in word: if letter in symbols: symbols[letter] += 1 else : symbols[letter] = 1 # here we construct the binary tree with tuples : tree = buildTree(symbols) # we have to return the tree too for decoding later # we could encode it better, but its native string representation is ok treeString = str(tree) treeString = treeString.replace(", ", ",") # the code we'll return, which first contains the length taken by the tree code = BitStream(12) code.int = len(treeString) # then we add the tree to the code codedTree = BitArray(8 * len(treeString)) codedTree.bytes = treeString code.append(codedTree) # a recursive traversal of the tree allows us to create a dictionnary of matching symbols and codes for encoding : symbols = buildEncodingDict(tree) # finally we encode the word codedWord = encodeWithDict(symbols, word) code.append(codedWord) return code
def bytes_to_board(data): a = BitArray() a.bytes = data.encode('ISO-8859-1') return a