コード例 #1
0
 def test_writebyte(self):
     output = "testWrite"
     with TemporaryDirectory() as td:
         tempFileName = os.path.join(td, output)
         with codecs.open(tempFileName, 'w', 'utf-8') as testOutput:
             writer = bit.Writer(testOutput)
             writer.writebyte('a')
             writer.close()
         with codecs.open(tempFileName, 'r', 'utf-8') as testOutput:
             self.assertEqual(testOutput.read(), 'a' + chr(0b00000000))
コード例 #2
0
 def test_writeNode(self):
     output = "testNode"
     with TemporaryDirectory() as td:
         tempFileName = os.path.join(td, output)
         with codecs.open(tempFileName, 'w', 'utf-8') as testOutput:
             writer = bit.Writer(testOutput)
             huffman.writeNode(testTree, writer)
             writer.close()
         with codecs.open(tempFileName, 'r', 'utf-8') as testOutput:
             s = getFileBinary(testOutput)
             self.assertEqual(s, testTreeBinary)
コード例 #3
0
 def test_writesinglebit(self):
     output = "testWrite"
     with TemporaryDirectory() as td:
         tempFileName = os.path.join(td, output)
         with codecs.open(tempFileName, 'w', 'utf-8') as testOutput:
             writer = bit.Writer(testOutput)
             writer.write(1)
             writer.close()
         with codecs.open(tempFileName, 'r', 'utf-8') as testOutput:
             r = testOutput.read()
             self.assertEqual(r, chr(0b10000000) + chr(0b00000111))
コード例 #4
0
 def test_readTree(self):
     output = "testNode"
     with TemporaryDirectory() as td:
         tempFileName = os.path.join(td, output)
         with codecs.open(tempFileName, 'w', 'utf-8') as testOutput:
             writer = bit.Writer(testOutput)
             for b in testTreeBinary:
                 writer.write(int(b))
             writer.close()
         with codecs.open(tempFileName, 'r', 'utf-8') as testOutput:
             reader = bit.Reader(testOutput)
             t = huffman.readTree(reader)
             self.assertEqual(str(t), str(testTree))
コード例 #5
0
 def test_writeCodified(self):
     output = "testEncoded"
     with TemporaryDirectory() as td:
         tempFileName = os.path.join(td, 'testFile')
         with open(tempFileName, 'w') as testFile:
              testFile.write(testString)
         with open(tempFileName, 'r') as testFile:
             tempFileName = os.path.join(td, output)
             with codecs.open(tempFileName, 'w', 'utf-8') as testOutput:
                 writer = bit.Writer(testOutput)
                 huffman.writeCodified(testFile, testCodeDict, writer)
         with codecs.open(tempFileName, 'r', 'utf-8') as testOutput:
             s = getFileBinary(testOutput)
             self.assertEqual(s, testEncoded)
コード例 #6
0
 def test_decodeFile(self):
     output = "testDecoded"
     input_ = "testEncoded"
     with TemporaryDirectory() as td:
         outputName = os.path.join(td, output)
         tempFileName = os.path.join(td, input_)
         with codecs.open(tempFileName, 'w', 'utf-8') as testInput:
             writer = bit.Writer(testInput)
             for b in testEncoded:
                 writer.write(int(b))
             writer.close()
         with codecs.open(tempFileName, 'r', 'utf-8') as testInput:
             reader = bit.Reader(testInput)
             huffman.decodeFile(reader, outputName, testTree)
         with codecs.open(outputName, 'r', 'utf-8') as testDecoded:
             r = testDecoded.read()
             self.assertEqual(r, testString)
コード例 #7
0
def Compress(filename, outputName):
    with codecs.open(filename, 'r', 'utf-8') as file:
        # gerar a arvore a partit da frequencia dos caracteres no texto
        root = Harvest(GetMap(file))

        # gerar o dicionario
        dict = {}

        # Caso só tenha uma letra
        if(root.isLeaf()):
            dict[root.Value] = 0
        else:
            createDict(root, dict)

        # Resetar cursor
        file.seek(0, 0)

        # Escrever arvora
        with codecs.open(outputName, 'w', 'utf-8') as outputFile:
            writer = bit.Writer(outputFile)
            writeNode(root, writer)

            # Codificar
            writeCodified(file, dict, writer)