Помогите чайнику улучшить код пожалуйста.
import random, json
def shift(lst,steps):
""" Циклический кольцевой сдвиг списка до минимума"""
lst = lst[steps:] + lst[:steps]
return lst
class Proteckt:#класс для шифровиния
SimvolsKey= {}
KeySimvols= ""
text=0
long=0
ProtectText=""
vingerPText=""
def CreateKey(self,length=0,strr=0):#Создание ключа
"""
Создание ключегово масива
"""
if strr==0:
strr="""АаБбВвГгДдЕеЁёЖжЗзИиЙйКкЛлМмНнОоПпРрСсТтУуФфХхЦцЧчШшЩщЪъЫыЬьЭэЮюЯя
1234567890 _-,.:;?/'"|\\(){}[]!@#$%^&*\n\t«»%
AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz"""
simvols=[]#масив символов
mass=[]
#собираю все символы
for simvolsas in strr:
if not(simvolsas in simvols):
simvols.append(simvolsas)
self.SimvolsKey.update({simvolsas:[]})
if length==0:
length=int(1324/3)
else:
length=int(length*123/300)
if (length<3):
length=3
self.long=length
for q in range(length):
mass.append([])
for sim in range(len(simvols)):
while True:
r=random.randint(0,999)
if not(r in mass[q]):
mass[q].append(r)
break
for w in mass:
e=0
for q in simvols:
self.SimvolsKey[q].append(w[e])
e=e+1
def saveKey(self,File):#Сохраняю ключь в файл
File=File+".json"
with open(File, 'w', encoding='utf-8') as fh:
fh.write(json.dumps(self.SimvolsKey, ensure_ascii=False))
def loadKey(self,File='Key.json'):#Загружаю из файла
try:
with open(File,'r', encoding='utf-8') as fh:
self.SimvolsKey = json.load(fh)
except FileNotFoundError:
print("ключевой файл не найден")
def code(self,strr=0):#Шифрование текста
w=0
e=0
i=self.long
if strr==0 and self.text!=0:
for simvol in self.text:
ProtectSimvol=self.SimvolsKey[simvol][e]
if(w==0):
self.ProtectText+=str(ProtectSimvol)
w+=1
else:
self.ProtectText+=" "+str(ProtectSimvol)
e+=1
if(e>=i):
e=0
pass
elif(strr!=0) and(type(strr)== str):
pass
else:
print ("шибка текст отсутствует либо имеет не подходящий формат")
def decode(self,strr=0):#Расшифровка текста
q=''
e=0
ww=1
strrq=''.join(self.SimvolsKey.keys())
i=self.long
if(strr==0):
mass=self.ProtectText.split(" ")
for Sim in mass:
for q in strrq:
if (int(Sim) == self.SimvolsKey[q][e]):
ww=0
self.KeySimvols+=q
if (ww==1):
self.KeySimvols+="\n"+Sim+"\n"
e+=1
if(e>=i):
e=0
pass
def codeFile(self,File_name):
fill=open(File_name,"r")
fillProt=open(File_name+"11","w")
e=0
i=self.long
for char in fill.read():
fillProt.write(str(self.SimvolsKey[char][e])+' ')
e+=1
if(e>=i):
e=0
fill.close()
fillProt.close()
def decodeFile(self,File_name):
fill=open(File_name)
fillProt=open(File_name+"00","w")
e=0
i=self.long
strr=''.join(self.SimvolsKey.keys())
for char in fill.read():
fillProt.write(str(self.SimvolsKey[char][e])+' ')
e+=1
if(e>=i):
e=0
fill.close()
fillProt.close()
def VingCodPText(self,key,strr=0):#шифрование защтщёного тексто на основе винжера
if (len(self.ProtectText)<1):
print("сначало зашифруйте текст при помощи функции класа code")
return
try:
int(key)
except ValueError:
print("ключь может состоять тоолько из цифр")
return
vinjer={'1':['q','w','e','r','t','y','u','i','o','p','a'],
'2':['q','w','e','r','t','y','u','i','o','p','a'],
'3':['q','w','e','r','t','y','u','i','o','p','a'],
'4':['q','w','e','r','t','y','u','i','o','p','a'],
'5':['q','w','e','r','t','y','u','i','o','p','a'],
'6':['q','w','e','r','t','y','u','i','o','p','a'],
'7':['q','w','e','r','t','y','u','i','o','p','a'],
'8':['q','w','e','r','t','y','u','i','o','p','a'],
'9':['q','w','e','r','t','y','u','i','o','p','a'],
'0':['q','w','e','r','t','y','u','i','o','p','a']}
move=0
simvol=0
for ef in self.ProtectText:
vinjer[key[simvol]]=shift(vinjer[key[simvol]],move)
if ef==" ":
move=5+int(key[simvol])
self.vingerPText+=vinjer[key[simvol]][10]
else:
move=int(ef)+int(key[simvol])
self.vingerPText+=vinjer[key[simvol]][int(ef)]
if(move>7):
move-=11
simvol+=1
if (simvol>len(key)-1):
simvol=0
def VingDecodVText(self,key,strr=0):
if (len(self.vingerPText)<1):
print("сначало зашифруйте текст при помощи функции класа VingCodPText")
return
try:
int(key)
except ValueError:
print("ключь может состоять тоолько из цифр")
return
self.ProtectText=''
move=0
simvol=0
vinjer={'1':['q','w','e','r','t','y','u','i','o','p','a'],
'2':['q','w','e','r','t','y','u','i','o','p','a'],
'3':['q','w','e','r','t','y','u','i','o','p','a'],
'4':['q','w','e','r','t','y','u','i','o','p','a'],
'5':['q','w','e','r','t','y','u','i','o','p','a'],
'6':['q','w','e','r','t','y','u','i','o','p','a'],
'7':['q','w','e','r','t','y','u','i','o','p','a'],
'8':['q','w','e','r','t','y','u','i','o','p','a'],
'9':['q','w','e','r','t','y','u','i','o','p','a'],
'0':['q','w','e','r','t','y','u','i','o','p','a']}
for sim in self.vingerPText:
vinjer[key[simvol]]=shift(vinjer[key[simvol]],move)
for er in range(11):
if (vinjer[key[simvol]][er]==sim):
if (er==10):
move=5+int(key[simvol])
self.ProtectText+=" "
else:
move=er+int(key[simvol])
self.ProtectText+=str(er)
break
if(move>7):
move-=11
simvol+=1
if (simvol>len(key)-1):
simvol=0