-
Notifications
You must be signed in to change notification settings - Fork 0
/
EncodeDecodeClass.py
139 lines (102 loc) · 3.66 KB
/
EncodeDecodeClass.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# Assessment 3 @ Noroff University College
_author_ = "Thomas Thaulow"
copyright = "Thomas Thaulow"
_email_ = "[email protected]"
class EncodeDecodeClass:
# Constructor
def __init__(self):
pass
# Encode
def EncodeStudentList(self, file_name, shift):
file = open(file_name, "r")
lines = file.readlines()
file2 = open("Encoded" + file_name, "w")
temp = ''
for line in lines:
record = line.split(",")
idnum = encodedecode_integer(record[0])
fname = encode(record[1], shift)
lname = encode(record[2], shift)
age = encodedecode_integer(record[3])
email = encode(record[4], shift)
course = encode(record[5], shift)
print(fname)
file2.write(f"{idnum},{fname},{lname},{age},{email},{course}")
file2.close()
file.close()
def DecodeStudentList(self, file_name, shift):
file = open(file_name, "r")
lines = file.readlines()
file2 = open(file_name.replace('Encoded', 'Decoded'), "w")
for line in lines:
record = line.split(",")
idnum = encodedecode_integer(record[0])
fname = decode(record[1], shift)
lname = decode(record[2], shift)
age = encodedecode_integer(record[3])
email = decode(record[4], shift)
course = decode(record[5], shift)
print(fname)
file2.write(f"{idnum},{fname},{lname},{age},{email},{course}")
file2.close()
file.close()
def encode(string, shift):
temp = ''
for ch in string:
if ch.isalpha():
# for small letters
# if unicode of 'ch' is in the range of small letters
if ord(ch) in range(97, 123):
# take number and add shift
# resulting number will be the encrypted number
# which we will change to character on line 113
val = ord(ch) + shift
if val > ord('z'):
# if the value is greater than 122
# we will subtract 25 from the value [ ord('z') - ord('a') ] = 25
val -= (ord('z') - ord('a'))
# the above expression can be written like this...
# val = val - ( ord('z') - ord('a') )
temp = temp + chr(val)
# for capital letters
elif ord(ch) in range(65, 91):
val = ord(ch) + shift
if val > ord('Z'):
val -= (ord('Z') - ord('A'))
temp = temp + chr(val)
else:
temp = temp + ch
return temp
def decode(string, shift):
temp = ''
for ch in string:
if ch.isalpha():
if ord(ch) in range(97, 123):
val = ord(ch) - shift
if val < ord('a'):
val += (ord('z') - ord('a'))
temp = temp + chr(val)
elif ord(ch) in range(65, 91):
val = ord(ch) - shift
if val < ord('A'):
val += (ord('Z') - ord('A'))
temp = temp + chr(val)
else:
temp = temp + ch
return temp
# This method does both encoding and decoding
# since it only swaps the first and last number.
def encodedecode_integer(integer):
text = str(integer)
length = len(text)
string = ''
first = text[0]
last = text[len(text) - 1]
for num in text:
if len(string) == 0:
string += last
elif len(string) == length - 1:
string += first
else:
string += num
return string