功能齐全的摩斯密码库–Pymorsecoder
它有什么优点?
- 轻量(主要是因为只有100行代码)
- 功能强大
如何使用?
- 下载Pymorsecoder
- 使用help()函数(.doc)或者查看源码
复制以下源码就可以使用了
import sys
class MorsecodeError(Exception):
def __init__(self, value):
self.value = value
def __str__(self):
return repr(self.value)
class pymorse:
'''
轻量的摩斯密码库, 宗旨为简单 易懂
By Lemonix 2021/04/02
'''
__en_list = {"A": ".-", "B": "-...", "C": "-.-.", "D": "-..", "E": ".", "F": "..-.", "G": "--.", "H": "....", "I": "..", "J": ".---", "K": "-.-", "L": ".-..", "M": "--", "N": "-.", "O": "---", "P": ".--.", "Q": "--.-", "R": ".-.", "S": "...", "T": "-", "U": "..-", "V": "...-", "W": ".--", "X": "-..-", "Y": "-.--", "Z": "--.", "1": ".----", "2": "..---", "3": "...--", "4": "....-", "5": ".....", "6": "-....", "7": "--...", "8": "---..", "9": "----.", "0": "-----", '.': '.-.-.-', ':': '---...', ',': '--..--', ';': '-.-.-.', '?': '..--..', '=': '-...-', "'": '.----.', '/': '-..-.', '!': '-.-.--', '-': '-....-', '_': '..--.-', '"': '.-..-.', '(': '-.--.', ')': '-.--.-', '$': '...-..-', '&': '....', '@': '.--.-.', '+': '.-.-.'}
__de_list = {v:k for k,v in __en_list.items()}
version = 0.1
en_list = __en_list
de_list = __de_list
def __init__(self, code, *, ptype=list):
'''
实例化一个摩斯密码文本
test = pymorse(".-/" [, ptype=list(str) ])
ptype为返回结果的类型, list返回list, str返回string
'''
self.code = code.upper()
self.ptype = ptype
def __str__(self):
'''
print这个实例时返回此实例的文本内容
'''
return repr(self.code)
#---------------------------------
def en(self): # encrypt加密
'''
encrypt 加密文本
code.en()
例子:
test = pymorse("Lemonix")
print(test.en())
输出:
.-.././--/---/-./../-..-/
'''
try:
res_list = []
res_str = ""
for i in self.code:
res_list.append(pymorse.__en_list[i])
res_str += f"{pymorse.__en_list[i]}/"
except:
raise MorsecodeError("Contains special characters")
else:
if self.ptype == list:
return res_list
elif self.ptype == str:
return res_str
def de(self, *, sign): # decode解密
'''
decode 解密文本
code.de(sign=sign)
例子:
test = pymorse(".-.././--/---/-./../-..-/", sign="/")
print(test.de())
输出:
LEMONIX
'''
try:
res_list = []
res_str = ""
type_code = self.code.split(sign)
except:
raise MorsecodeError("Error sign")
else:
if type_code[-1] == "":
type_code = type_code[0:-1]
try:
for i in type_code:
res_list.append(pymorse.__de_list[i])
res_str += pymorse.__de_list[i]
except:
raise MorsecodeError("Error morsecode")
else:
if self.ptype == list:
return res_list
elif self.ptype == str:
return res_str
if __name__ == "__main__":
test1 = pymorse("ABCD", ptype=str)
print(test1)
print(test1.en())
test2 = pymorse(".-/-.../", ptype=str)
print(test2.de(sign="/"))
yontest = input("查看帮助?y/n :")
if yontest == "y":
print(help(pymorse))
else:
sys.exit()
'''
My Bilibili channel: https://b23.tv/wxyFrS
Thank u 4 using my works!
'''