def compress(filename): text = util.load_file_as_text(filename) encoded_values = encode(text) encoded_bytes = bytes(encoded_values) output = util.get_output_filename(filename) util.save_file_as_bytes(output, encoded_bytes) print(util.get_compression_ratio(encoded_bytes, text))
def compress(filename): text = util.load_file_as_text(filename) encoded_text = encode(text) encoded_bytes = util.to_byte_array(encoded_text) output = util.get_output_filename(filename) save(encoded_bytes, output) print(util.get_compression_ratio(encoded_bytes, text))
def compress(filename): text = util.load_file_as_text(filename) probs = util.calc_prob(text) ranges = calc_range(probs) encoded_value = encode(text, ranges) encoded_bytes = bytearray(struct.pack("f", encoded_value)) output = util.get_output_filename(filename) save(encoded_bytes, ranges, len(text), output) print(util.get_compression_ratio(encoded_bytes, text))
def compress(filename, n=8): text = util.load_file_as_text(filename) probs = calc_prob(text) codes = build_code(probs, n) encoded_text = encode(text, codes) encoded_bytes = util.to_byte_array(encoded_text) output = util.get_output_filename(filename) save(encoded_bytes, codes, output) print(util.get_compression_ratio(encoded_bytes, text))
def compress(filename): text = util.load_file_as_text(filename) freq = calc_freq(text) heap = make_heap(freq) root = build_tree(heap) codes = build_codes(root) encoded_text = encode(codes, text) encoded_bytes = util.to_byte_array(encoded_text) output = util.get_output_filename(filename) save(encoded_bytes, codes, output) print(util.get_compression_ratio(encoded_bytes, text))
def coding_test(compress_function, decompress_function): filename = 'test/files/bookkeeper' backup_filename = filename + '.backup' output_filename = util.get_output_filename(filename) shutil.copyfile(filename, backup_filename) try: compress_function(filename) decompress_function(output_filename) with open(filename) as source: with open(backup_filename) as backup: equal = source.read() == backup.read() os.remove(output_filename) except RuntimeError: equal = False os.remove(filename) os.rename(backup_filename, filename) return equal
def test_get_output_filename_with_extension(self): self.assertEqual(self.bookkeeper + '.ext', util.get_output_filename(self.bookkeeper, 'ext'))
def test_get_output_filename(self): self.assertEqual(self.bookkeeper + '.output', util.get_output_filename(self.bookkeeper))