def huffman(infile): with open(infile, 'rb') as uncompressed: freqs = make_freq_table(uncompressed) tree = make_tree(freqs) uncompressed.seek(0) with open(infile + '.huf', 'wb') as compressed: util.compress(tree, uncompressed, compressed)
def encode(text): Lziv = open('Lziv_'+file,'w') #text = lz.prep_text(file) t1 = time.clock() lztext = lz.encode(text,windowlen) Lziv.write(lztext) Lziv.close() t2 = time.clock() lztext = lz.prep_text('Lziv_'+file) t3 = time.clock() tree = huff.make_tree(lztext) t4 = time.clock() h = open('huff_'+file,'w') t5 = time.clock() htext = huff.encode(tree,lztext) #h.write(tree) h.write(htext) t6 = time.clock() h.close return [t2-t1,t4-t3,t6-t5],tree
def run_compressor (filename): with open(filename, 'rb') as uncompressed: freqs = huffman.make_freq_table(uncompressed) tree = huffman.make_tree(freqs) uncompressed.seek(0) with open(filename+'.huf', 'wb') as compressed: util.compress(tree, uncompressed, compressed)
def read_tree (bitreader): '''Read a description of a Huffman tree from the given bit reader, and construct and return the tree. When this function returns, the bit reader should be ready to read the next bit immediately following the tree description. Huffman trees are stored in the following format: * TreeLeafEndMessage is represented by the two bits 00. * TreeLeaf is represented by the two bits 01, followed by 8 bits for the symbol at that leaf. * TreeBranch is represented by the single bit 1, followed by a description of the left subtree and then the right subtree. Args: bitreader: An instance of bitio.BitReader to read the tree from. Returns: A Huffman tree constructed according to the given description. ''' #pass freqs_table = huffman.make_freq_table(bitreader) tree = huffman.make_tree(freq_table) return tree
def encode(text): Lziv = open('Lziv_' + file, 'w') #text = lz.prep_text(file) t1 = time.clock() lztext = lz.encode(text, windowlen) Lziv.write(lztext) Lziv.close() t2 = time.clock() lztext = lz.prep_text('Lziv_' + file) t3 = time.clock() tree = huff.make_tree(lztext) t4 = time.clock() h = open('huff_' + file, 'w') t5 = time.clock() htext = huff.encode(tree, lztext) #h.write(tree) h.write(htext) t6 = time.clock() h.close return [t2 - t1, t4 - t3, t6 - t5], tree
def run_compressor(filename): # opening file to read with open(filename, 'rb') as uncompressed: # makefreqtable reads the whole file freqs = huffman.make_freq_table(uncompressed) tree = huffman.make_tree(freqs) # reinitializing the file 'editing cursor' back to # the start of the file uncompressed.seek(0) with open(filename + '.huf', 'wb') as compressed: # adding tree_stream as a parameter...???????? util.compress(tree, uncompressed, compressed)
def huffman(self,data): size = len(data)#in bytes hist = huffman.make_hist(data) raiz,tree = huffman.make_tree(hist) bs = 0#0 si es histograma bc = 4096 # 256*2*8#tiene que ser 16 bits packHist = self.packet.packHist(self.fs,bs,bc,hist) self.send(packHist) #print "PACKETS",len(range(0,size,self.chuckSizeToSend)) for i in range(0,size,self.chuckSizeToSend): perKb = data[i:i+self.chuckSizeToSend] com = huffman.compress(tree,perKb) bs +=1 bc = len(perKb)*8 pack = self.packet.pack(self.fs,bs,bc,com) self.send(pack) self.fs += 1 #TEST """
def huffman(self, data): size = len(data) #in bytes hist = huffman.make_hist(data) raiz, tree = huffman.make_tree(hist) bs = 0 #0 si es histograma bc = 4096 # 256*2*8#tiene que ser 16 bits packHist = self.packet.packHist(self.fs, bs, bc, hist) self.send(packHist) #print "PACKETS",len(range(0,size,self.chuckSizeToSend)) for i in range(0, size, self.chuckSizeToSend): perKb = data[i:i + self.chuckSizeToSend] com = huffman.compress(tree, perKb) bs += 1 bc = len(perKb) * 8 pack = self.packet.pack(self.fs, bs, bc, com) self.send(pack) self.fs += 1 #TEST """
def run(self): BS = -1 FS = -1 raiz = 0 tree = [] audio = [] while True: pack_h, addr = self.sock.recvfrom(10240) hist, header, body = self.packet.unPackByType(pack_h) fs = header[0] bs = header[1] bc = header[2] #si se reinicia el server if hist and fs > FS or (fs == 0 and FS > 3): FS = fs raiz, tree = huffman.make_tree(body) BS = -1 elif not hist and fs == FS and bs > BS and raiz and len(tree): BS = bs row = huffman.decode_string(tree, raiz, body) self.lock.acquire() self.fifo.append(row) self.lock.release()
def run(self): BS = -1 FS = -1 raiz = 0 tree = [] audio = [] while True: pack_h, addr = self.sock.recvfrom(10240) hist,header,body = self.packet.unPackByType(pack_h) fs = header[0] bs = header[1] bc = header[2] #si se reinicia el server if hist and fs > FS or (fs == 0 and FS > 3): FS = fs raiz, tree = huffman.make_tree(body) BS = -1 elif not hist and fs == FS and bs > BS and raiz and len(tree): BS = bs row = huffman.decode_string(tree,raiz,body) self.lock.acquire() self.fifo.append(row) self.lock.release()
def main(): txt = 1000 * open('README').read() t0 = time() freq = Counter(txt) print('count: %9.6f sec' % (time() - t0)) t0 = time() tree = huffTree(freq) print('tree: %9.6f sec' % (time() - t0)) write_dot(tree, 'tree.dot') code = huffCode(tree) # create tree from code (no frequencies) write_dot(make_tree(code), 'tree_raw.dot') a = bitarray() t0 = time() a.encode(code, txt) print('C encode: %9.6f sec' % (time() - t0)) # Time the decode function above t0 = time() res = decode(tree, a) Py_time = time() - t0 assert ''.join(res) == txt print('Py decode: %9.6f sec' % Py_time) # Time the decode method which is implemented in C t0 = time() res = a.decode(code) assert ''.join(res) == txt C_time = time() - t0 print('C decode: %9.6f sec' % C_time) print('Ratio: %f' % (Py_time / C_time))
def main(): p = OptionParser("usage: %prog [options] [FILE]") p.add_option( '-p', '--print', action="store_true", help="print Huffman code") p.add_option( '-t', '--tree', action="store_true", help="store the tree as a .dot file") opts, args = p.parse_args() if len(args) == 0: filename = 'README' elif len(args) == 1: filename = args[0] else: p.error('only one argument expected') with open(filename, 'rb') as fi: plain = bytearray(fi.read()) if len(args) == 0: plain *= 1000 t0 = time() freq = Counter(plain) print('count: %9.6f sec' % (time() - t0)) t0 = time() tree = huff_tree(freq) print('tree: %9.6f sec' % (time() - t0)) if opts.tree: write_dot(tree, 'tree.dot', 0 in plain) code = huff_code(tree) if opts.print: print_code(freq, code) if opts.tree: # create tree from code (no frequencies) write_dot(make_tree(code), 'tree_raw.dot', 0 in plain) a = bitarray() t0 = time() a.encode(code, plain) print('C encode: %9.6f sec' % (time() - t0)) # Time the decode function above t0 = time() res = bytearray(iterdecode(tree, a)) Py_time = time() - t0 print('Py decode: %9.6f sec' % Py_time) assert res == plain # Time the decode method which is implemented in C t0 = time() res = bytearray(a.iterdecode(code)) C_time = time() - t0 print('C decode: %9.6f sec' % C_time) assert res == plain print('Ratio: %f' % (Py_time / C_time))
while True: time.sleep(1) except KeyboardInterrupt: print "stop server" for thread in threading.enumerate(): if thread.isAlive(): try: thread._Thread__stop() except: print 'No se pudo matar todos los threads' exit(0) if __name__ == '__main__' and False: audio = Audio(byte=True,seconds=1) while 1: data = audio.get_chunck() d = list(data) print 'size',len(d),'bytes 17.28kb' hist = huffman.make_hist(d) raiz,tree = huffman.make_tree(hist) com = huffman.compress(tree,d) #print com d = huffman.decompress(tree,raiz,com) #print 'salida!',d t = time.time() audio.out_chunck(d) elapse = time.time() - t print "seconds: %s" % (elapse)
p2.start() try: while True: time.sleep(1) except KeyboardInterrupt: print "stop server" for thread in threading.enumerate(): if thread.isAlive(): try: thread._Thread__stop() except: print 'No se pudo matar todos los threads' exit(0) if __name__ == '__main__' and False: audio = Audio(byte=True, seconds=1) while 1: data = audio.get_chunck() d = list(data) print 'size', len(d), 'bytes 17.28kb' hist = huffman.make_hist(d) raiz, tree = huffman.make_tree(hist) com = huffman.compress(tree, d) #print com d = huffman.decompress(tree, raiz, com) #print 'salida!',d t = time.time() audio.out_chunck(d) elapse = time.time() - t print "seconds: %s" % (elapse)