Skip to content

Commit 633e13b

Browse files
authored
Python compression
1 parent 0a27057 commit 633e13b

File tree

3 files changed

+210
-0
lines changed

3 files changed

+210
-0
lines changed

helper.py

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
from sanatize import *
2+
3+
'''
4+
Encode
5+
6+
Take in user file names for the ouput and input files,
7+
then enocde text using the run algorithim
8+
'''
9+
10+
def encode(inputFile, outputFile):
11+
normalText = 0
12+
totalBuffer = ""
13+
14+
fileIn = open(inputFile, "r")
15+
fileOut = open(outputFile,"w")
16+
17+
fileIn.flush()
18+
fileOut.flush()
19+
20+
#Check if text is valid
21+
if(not(checkEncoding(inputFile))):
22+
print("ERROR: File contains invalid characters")
23+
return
24+
25+
print("\nEncoding in progress ...")
26+
#Encode each line while adding into the output file
27+
buffer = ""
28+
tempChar = ""
29+
for line in fileIn:
30+
run = 0
31+
index = 0
32+
33+
#Check for one character
34+
if(len(line) == 1 and line != " "):
35+
totalBuffer += line
36+
fileOut.write("1" + line)
37+
normalText += 1
38+
elif(line):
39+
for char in line:
40+
index += 1
41+
run += 1
42+
normalText += 1
43+
if(index >= len(line)):
44+
#Last character
45+
if(line[index - 1] == "\n"):
46+
normalText -= 1
47+
break
48+
buffer += str(run) + char
49+
run = 0
50+
elif(char != line[index]):
51+
buffer += str(run) + char
52+
run = 0
53+
totalBuffer += buffer
54+
fileOut.write(buffer + "\n")
55+
buffer = ""
56+
57+
fileIn.close()
58+
fileOut.close()
59+
60+
"Note compression ratio does not include new line characters"
61+
print("Encoding completed ...")
62+
print("Compression Ratio: " + str(normalText) + "/" + str(len(totalBuffer)))
63+
64+
'''
65+
Decode
66+
67+
Take in user file names for the ouput and input files,
68+
then decode text using the run algorithim
69+
'''
70+
71+
def decode(inputFile, outputFile):
72+
#Check if decoding scheme is valid
73+
if(not(checkDecoding(inputFile))):
74+
return
75+
76+
fileIn = open(inputFile, "r")
77+
fileOut = open(outputFile,"w")
78+
79+
print("\nDecoding in progress ...")
80+
81+
count = 0
82+
buffer = ""
83+
84+
fileIn.flush()
85+
fileOut.flush()
86+
87+
for line in fileIn:
88+
i = 0
89+
for char in line:
90+
if(char == "\n"):
91+
buffer += "\n"
92+
count -= 1
93+
elif((count % 2 == 0 and count > 1) or count == 0):
94+
#check for last character
95+
while(i < int(char)):
96+
buffer += line[count + 1]
97+
i += 1
98+
i = 0
99+
count += 1
100+
fileOut.write(buffer)
101+
buffer = ""
102+
count = 0
103+
104+
105+
print("Decoding completed ...")

sanatize.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
'''
2+
checkEncoding
3+
4+
Take in user input file name,
5+
then check text to be valid for encoding
6+
'''
7+
8+
def checkEncoding(fileName):
9+
#Check if file is valid
10+
try:
11+
file = open(fileName, "r")
12+
except:
13+
print("Invalid file\n")
14+
return False
15+
16+
file.flush()
17+
#Check if all characters are valid
18+
check = True
19+
unsuported = ""
20+
badCharacters = {'!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '-', '+', '=', '"', "'", "{", "[", "]", "}", "|", ";", ":", "<", ",", ">", ".", "/", "?"}
21+
for line in file:
22+
for char in line:
23+
if(char in badCharacters):
24+
check = False
25+
unsuported += ", " + char
26+
if(check == False):
27+
print("ERROR: Unsoported characters found\n")
28+
print(unsuported)
29+
file.close()
30+
return check
31+
32+
'''
33+
checkDecoding
34+
35+
Take in user input file name,
36+
then check encoded text for proper encoding before decoding
37+
'''
38+
39+
def checkDecoding(fileName):
40+
#Check if file is valid
41+
try:
42+
file = open(fileName, "r")
43+
except:
44+
print("Invalid file\n")
45+
return False
46+
47+
file.flush()
48+
#Check if all characters are valid
49+
check = True
50+
count = 0
51+
badCharacters = {'!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '-', '+', '=', '"', "'", "{", "[", "]", "}", "|", ";", ":", "<", ",", ">", ".", "/", "?"}
52+
53+
file.flush()
54+
55+
for line in file:
56+
i = 0
57+
for char in line:
58+
if(char == "\n"):
59+
buffer += "\n"
60+
count -= 1
61+
elif((count % 2 == 0 and count > 1) or count == 0):
62+
#check for invalid scheme
63+
#If valid only numbers should come here
64+
try:
65+
int(char)
66+
except:
67+
#Expected int was not found
68+
print("ERROR: expected run was not found\n")
69+
return False
70+
else:
71+
#Check for bad characters
72+
if(char in badCharacters):
73+
print("ERROR: Unsuported characters found\n")
74+
return False
75+
count += 1
76+
count = 0
77+
78+
file.close()
79+
return True

start.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
from helper import *
3+
4+
def menu():
5+
while True:
6+
print("#1. Encode a File \n#2. Decode a File \n#3. Exit")
7+
option = input()
8+
if(option == "1"):
9+
print("Input Source Filepath: ")
10+
source = input()
11+
print("\nInput Destination Filepath:")
12+
dest = input()
13+
encode(source, dest)
14+
elif(option == "2"):
15+
print("Input Source Filepath: ")
16+
filePath = input()
17+
print("\nInput Destination Filepath:")
18+
destPath = input()
19+
decode(filePath, destPath)
20+
elif(option == "3"):
21+
exit()
22+
else:
23+
print("Invalid entry..\n")
24+
25+
menu()
26+

0 commit comments

Comments
 (0)