-
Notifications
You must be signed in to change notification settings - Fork 0
Second commit #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
dev/main.py
Outdated
| f.close() | ||
| sample_freqs = count_frequencies(sample_text) | ||
|
|
||
| if sys.argv[1] == "encode": |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Если парсишь аргументы командной строки то лучше использовать argparse или что то подобное
dev/main.py
Outdated
| return ''.join(result_text) | ||
|
|
||
|
|
||
| def decode_vigenere(text, k): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
decode = shift с другим ключем. Не стоит несколько раз писать один и тот же код как и для цезаря
| error = 0 | ||
| for i in range(26): | ||
| error += abs(frequencies1[i] - frequencies2[i]) | ||
| return error |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Лучше отнормировать на 26
dev/main.py
Outdated
| def L2_MSE(frequencies1, frequencies2): | ||
| error = 0 | ||
| for i in range(26): | ||
| error += abs(frequencies1[i] - frequencies2[i]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
это L1 мера, а не L2 и MAE, а не MSE) И MSE в своем названии подразумевает что метрика считается в L2 пространстве, и выходит что в названии некоторая тавтология. Лучше переименуй. Например просто в MAE
dev/main.py
Outdated
| print(shift_text(input_txt, k, "stc")) | ||
| else: | ||
| input_1 = open(sys.argv[7]) | ||
| input_1 = open(arguments.input_file) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
лучше считывать и записывать в файл из отдельной функции
dev/main.py
Outdated
| input_1.close() | ||
| output_1 = open(sys.argv[9], "w") | ||
| output_1.write(shift_text_caesar(input_txt, k)) | ||
| output_1 = open(arguments.output_file, "w") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
with open лучше
dev/main.py
Outdated
| return shift_text(text, 26 - min_k, "stc") | ||
|
|
||
|
|
||
| f = open('War_and_Peace.txt') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
лучше это делать только если тебя попросили взломать текст
dev/main.py
Outdated
| freqs = count_frequencies(shift_text_caesar(text, 26 - k)) | ||
| if L2_MSE(freqs, sample) < min_error: | ||
| min_error = L2_MSE(freqs, sample) | ||
| freqs = count_frequencies(shift_text(text, 26 - k, "stc")) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
26 непонятно что это, посмотри весь код
dev/main.py
Outdated
| result_text = ['a' for i in range(len(text))] | ||
| for i in range(len(text)): | ||
| result_text[i] = shift(text[i], k) | ||
| if type == "stc": |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
type == 'stc' совершенно непонятно. Лучше уж передавать сразу два аргумента шифр и дейтсвие
dev/main.py
Outdated
| if sys.argv[3] == "caesar": | ||
| k = int(sys.argv[5]) | ||
|
|
||
| pasrsing = argparse.ArgumentParser() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
лучше добавить subparser под каждое из твоих возможных действий
dev/main.py
Outdated
| if sys.argv[3] == "caesar": | ||
| k = int(sys.argv[5]) | ||
|
|
||
| pasrsing = argparse.ArgumentParser() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
стоит запускать в __name__ == '__main__':
dev/main.py
Outdated
| k = int(sys.argv[5]) | ||
|
|
||
| pasrsing = argparse.ArgumentParser() | ||
| pasrsing.add_argument("action", type=str, help="decode/encode/decode-without-key") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
желательно вынести парсинг аргументов в отдельную функию
|
ок |
No description provided.