我不明白为什么我会收到以下错误,并希望得到一些建议。谢谢。
错误:
Traceback (most recent call last):
File "test.py", line 5, in <module>
ciphertext = md.encrypt(plaintext)
File "/Volumes/Mac/Computer/Programming/Cryptography/crypto.py", line 87, in encrypt
ciphertext = Text()
TypeError: __init__() takes exactly 2 arguments (1 given)
代码:
import crypto
plaintext = crypto.Text('plaintext.txt')
md = crypto.MonomeDinome('fred', 'wilma')
ciphertext = md.encrypt(plaintext)
print ciphertext
Crypto.py 有以下代码:
class Text:
def __init__(self, filename):
self.load(filename)
def load(self, filename):
fp = open(filename, "r")
self.rawtext = fp.read()
fp.close
self.text = self.convert(self.rawtext)
def convert(self, txt):
rval = ""
for c in txt.upper():
if c.isalpha():
rval += c
return rval
def set(self, lst):
self.text = "".join(lst)
def get(self):
return self.text
def __str__(self):
rval = ""
pos = 0
for c in self.text:
rval += c
pos += 1
if pos % 60 == 0:
rval += '\n'
elif pos % 5 == 0:
rval += " "
return rval
class MonomeDinome:
def __init__(self, dkw, lkw):
self.digitsKey = self.digitsScramble(dkw)
self.lettersKey = self.lettersScramble(lkw)
def digitsScramble(self, dkw):
dkl = list((dkw.upper() + "ZZZZZZZZZZ")[0:10])
for i in "0123456789":
pos = self.findLowestLetter(dkl)
if pos != -1:
dkl[pos] = i
return "".join(dkl)
def findLowestLetter(self, lst):
pos = -1
lowest = ''
for i in range(len(lst)):
if lst[i].isalpha():
if (lowest == '') or (lst[i] < lowest):
lowest = lst[i]
pos = i
return pos
def lettersScramble(self, lkw):
rlist = []
for a in lkw.upper():
if a == "W":
a = "V"
if a == "J":
a = "I"
if not a in rlist:
rlist.append(a)
for a in "ABCDEFGHIKLMNOPQRSTUVXYZ":
if not a in rlist:
rlist.append(a)
return "".join(rlist)
def __str__(self):
return "digitsKey = " + self.digitsKey + "\n" + \
"lettersKey = " + self.lettersKey
def encrypt(self, plaintext):
rlist = []
for char in plaintext.get():
rlist.append(char)
ciphertext = Text()
ciphertext.set(rlist)
return ciphertext