11import enum
22import queue
33import os
4+ import logging
5+
6+ # Configs
7+ DEBUG = False
8+ logging .basicConfig (level = logging .DEBUG , format = '%(asctime)s-%(levelname)s: %(message)s' , datefmt = '%M:%S' )
49
510
611class dir (enum .Enum ):
@@ -60,6 +65,7 @@ def __init__(self, head, name='tree', isPrefect=False):
6065 head .setHead ()
6166 self .nodes .append (head )
6267 else :
68+ if DEBUG : logging .error ("Illegal node: {}" .format_map (head .name ))
6369 raise Exception ("Illegal node" )
6470
6571 def insertNode (self ):
@@ -117,21 +123,6 @@ def __init__(self, head, name='huffman tree', isPrefect=True):
117123 super ().__init__ (head , name , isPrefect )
118124 self .q = queue .Queue ()
119125
120- # def insertNode(self, node1, node2):
121- # newNode = node("{},{}".format(node1.name, node2.name), node1.value + node2.value)
122- # newNode.addChild(node1)
123- # newNode.addChild(node2)
124- # if self.q.qsize() == 1:
125- # self.insertNode(self.q.get(), newNode)
126- # else:
127- # self.q.put(newNode)
128- #
129- # def insertSingleNode(self, child):
130- # headNode = self.q.get()
131- # newNode = node("{},{}".format(headNode.name, child.name), headNode.value + child.value)
132- # newNode.addChild(headNode)
133- # newNode.addChild(child)
134-
135126 def generate (self , minHeap ):
136127 for x in range (int (minHeap .getSize () / 2 )):
137128 try :
@@ -152,7 +143,11 @@ def generate_helper(self):
152143 node1 = self .q .get ()
153144 node2 = self .q .get ()
154145 newNode = node ("{},{}" .format (node1 .name , node2 .name ), node1 .value + node2 .value )
155- # print(node1.name.replace('\n','\\n') + ' | ' + node2.name.replace('\n','\\n') + ' | ' + newNode.name.replace('\n','\\n'))
146+ if DEBUG : logging .info (
147+ "Huffman tree nodes added: " + node1 .name .replace ('\n ' , '\\ n' ) + ' | ' + node2 .name .replace ('\n ' ,
148+ '\\ n' ) + ' | ' + newNode .name .replace (
149+ '\n ' , '\\ n' ))
150+
156151 newNode .addChild (node1 )
157152 newNode .addChild (node2 )
158153 self .q .put (newNode )
@@ -180,6 +175,8 @@ def generateFrequency(self):
180175 self .frequency .update ({char : self .frequency [char ] + 1 })
181176 else :
182177 self .frequency [char ] = 1
178+
179+ if DEBUG : logging .info ("Characters frequency: {}" .format (self .frequency ))
183180 file .close ()
184181
185182 def generateMinHeap (self ):
@@ -188,6 +185,7 @@ def generateMinHeap(self):
188185 self .minHeap = minHeap (head )
189186 for n in self .frequency :
190187 self .minHeap .insertNode (node (n , self .frequency [n ]))
188+ if DEBUG : logging .info ("Min Heap generated: {}" .format (self .minHeap .name ))
191189
192190 def generateTree (self ):
193191 self .generateFrequency ()
@@ -205,20 +203,18 @@ def generateTable_helper(self, code, count, node):
205203 file = open (self .tablePath , 'a+' )
206204 if node .name == '\n ' :
207205 file .write ("{}\t {}\t {}\n " .format ('\\ n' , count - 1 , code ))
206+ if DEBUG : logging .info ("huffman table row: {}\t {}\t {}\n " .format ('\\ n' , count - 1 , code ))
208207 elif node .name == '\t ' :
209208 file .write ("{}\t {}\t {}\n " .format ('\\ t' , count - 1 , code ))
209+ if DEBUG : logging .info ("huffman table row: {}\t {}\t {}\n " .format ('\\ t' , count - 1 , code ))
210210 else :
211211 file .write ("{}\t {}\t {}\n " .format (node .name , count - 1 , code ))
212+ if DEBUG : logging .info ("huffman table row: {}\t {}\t {}\n " .format (node .name , count - 1 , code ))
212213
213214 file .close ()
214215 else :
215216 self .generateTable_helper (code + '0' , count + 1 , node .childs [0 ])
216217 self .generateTable_helper (code + '1' , count + 1 , node .childs [1 ])
217- # for index, n in enumerate(node.childs):
218- # code += str(index)
219- # count += 1
220- # print(n.name + ' | ' + str(index))
221- # self.generateTable_helper(code, count, n)
222218
223219 def tableToDict (self , tablePath ):
224220 file = open (tablePath , 'r' )
@@ -231,10 +227,8 @@ def tableToDict(self, tablePath):
231227 self .tableDict ['\t ' ] = array [2 ]
232228 else :
233229 self .tableDict [array [0 ]] = array [2 ]
234- # print(array)
235- # print(self.tableDict)
236-
237230 file .close ()
231+ if DEBUG : logging .info ("table converted to dict: {}" .format (self .tableDict ))
238232
239233 def exportZipped (self , zip_path ):
240234 str = ''
@@ -253,6 +247,8 @@ def exportZipped(self, zip_path):
253247 file .write (binary_format )
254248 file .close ()
255249
250+ if DEBUG : logging .info ("zipped file exported to: {}" .format (zip_path ))
251+
256252 def importZipped (self , path , zip_path , table_path ):
257253 zip_file = open (zip_path , 'rb' ).read ()
258254 byte_arr = list (zip_file )
@@ -267,7 +263,7 @@ def importZipped(self, path, zip_path, table_path):
267263 file = open (path , 'w' )
268264 file .write (decoded_string )
269265 file .close ()
270- return decoded_string
266+ if DEBUG : logging . info ( "decoded file exported: {}" . format ( path ))
271267
272268 def decoder (self , string_byte , table_path ):
273269
0 commit comments