How to use _build_code method in lisa

Best Python code snippet using lisa_python

huffman_compression.py

Source:huffman_compression.py Github

copy

Full Screen

...45 # build Huffman trie46 root = self._build_trie(freq)47 # build symbol table for chars and their binary representation48 st = dict()49 self._build_code(st, root, '')50 bw = BitWriter(output_file)51 # write the Huffman trie binary representation to the file52 self._write_trie(root, bw)53 # write number of bytes in original uncompressed message54 bw.writebits(length, 8)55 # use Huffman code to encode input56 for i in xrange(length):57 self.file.seek(i)58 code = st[ord(self.file.read(1))]59 for c in code:60 if c == '0':61 bw.writebit(False)62 else:63 bw.writebit(True)64 # close the output file65 # output_file.close()66 def extract(self, output_file):67 """68 Restore the compressed data69 """70 br = BitReader(self.file)71 root = self._read_trie(br)72 # number of bytes to write73 length = int(br.readbits(8))74 bw = BitWriter(output_file)75 # decode using the Huffman trie76 for i in xrange(length):77 node = root78 while not node.is_leaf:79 bit = br.readbit()80 if bit:81 node = node.right82 else:83 node = node.left84 # write the character to output85 bw.writebits(node.char, 8)86 # output_file.close()87 def _read_trie(self, br):88 bit = br.readbit()89 if bit:90 # it is a leaf, so read the next 8 bits and create the leaf node91 return self.Node(int(br.readbits(8)), -1)92 else:93 # it is an intermediary node, recursively create left and right nodes94 return self.Node(0, -1, self._read_trie(br), self._read_trie(br))95 def _write_trie(self, node, bw):96 if node.is_leaf:97 # put "1" at the beginning of the leaf node char98 bw.writebit(True)99 # put node character in 8 bits representation100 bw.writebits(node.char, 8)101 else:102 # put "0" for the intermediary node (as a delimiter)103 bw.writebit(False)104 self._write_trie(node.left, bw)105 self._write_trie(node.right, bw)106 def _build_trie(self, frequency):107 """108 Build a binary trie and return its root node109 """110 nodes = PriorityQueue()111 for char, freq in frequency.iteritems():112 if freq > 0:113 nodes.put(self.Node(char, freq))114 # merge two smallest tries115 while nodes.qsize() > 1:116 left = nodes.get()117 right = nodes.get()118 parent = self.Node(0, left.frequency + right.frequency, left, right)119 nodes.put(parent)120 # return the root node121 return nodes.get()122 def _build_code(self, st, node, s):123 """124 Build a symbol table from a Huffman trie.125 Each key has a binary value.126 """127 if not node.is_leaf:128 self._build_code(st, node.left, s + '0')129 self._build_code(st, node.right, s + '1')130 else:131 st[node.char] = s132if __name__ == '__main__':133 from StringIO import StringIO134 original_file = StringIO('abracadabra!')135 h = Huffman(original_file)136 compressed_file = StringIO()137 h.compress(compressed_file)138 compressed_file.seek(0)139 h2 = Huffman(compressed_file)140 restored_file = StringIO()141 h2.extract(restored_file)142 restored_file.seek(0)143 assert restored_file.read() == 'abracadabra!'

Full Screen

Full Screen

huffman1.py

Source:huffman1.py Github

copy

Full Screen

...14 return T[0] #]full15 16 def build_code(self, freq: "IMap<Symbol, Real>") -> "IMap<Symbol, str>":17 code = self.createMap()18 def _build_code(t, prefix):19 if len(t[0]) > 1:20 code[t[0][1]] = prefix21 else:22 _build_code(t[1], prefix + '0')23 _build_code(t[2], prefix + '1')24 tree = self.build_tree(freq)25 _build_code(tree, '') ...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run lisa automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful