python - 我怎样才能让函数/方法根据正在使用的类将密码附加到不同的列表中?
问题描述
我正在制作一个密码生成器,作为我在 python 中的 OOP 的第一次尝试。我有 3 节课Simple
,Complex
并且Memorable
. Simple是 Simple 的父类,Complex
而 Memorable 也是Simple的子类。两者都Simple
具有Complex
相同的生成方法来生成密码:
def generate(self, num_of_passwords: int):
characters = ''
if self.characters is None:
characters = string.ascii_letters + string.digits
else:
characters = self.characters
for i in range(num_of_passwords):
password = ''
for c in range(self.length):
password += secrets.choice(characters)
output.append(password)
return output
Memorable 具有此生成功能:
def generate(self, num_of_passwords: int):
word_url = "http://svnweb.freebsd.org/csrg/share/dict/words?view=co&content-type=text/plain"
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64)'}
req = urllib.request.Request(word_url, headers=headers)
response = response = urllib.request.urlopen(req)
long_txt = response.read().decode()
words = long_txt.splitlines()
for i in range(num_of_passwords):
password = ''
two_words = ''
for i in range(2):
two_words += secrets.choice(words).title()
password = two_words
if self.numbers == True:
for i in range(random.randint(3, 4)):
password += secrets.choice(string.digits)
output.append(password)
return output
上面的生成函数将生成的每个密码附加到一个名为“输出”的列表中。输出如下所示:output = []
. 问题是所有密码都被附加到完全相同的列表中。我知道这可以通过为每个班级设置3 个不同的列表来轻松解决,但是有更好的方法吗?感谢:D
完整代码:(GitHub ----> https://github.com/lunAr-creator/pw-gen)
import random
import secrets
import string
import urllib.request
output = []
class Simple():
def __init__(self, length: int, characters = None):
self.length = length
self.characters = characters
def generate(self, num_of_passwords: int):
characters = ''
if self.characters is None:
characters = string.ascii_letters + string.digits
else:
characters = self.characters
for i in range(num_of_passwords):
password = ''
for c in range(self.length):
password += secrets.choice(characters)
output.append(password)
return output
def return_result(self, index: int):
return output[index]
def clear_results(self):
output.clear()
class Complex(Simple):
def __init__(self, length, string_method, numbers=True, special_chars=False):
characters = ''
methods: dict = {
"upper": string.ascii_uppercase,
"lower": string.ascii_lowercase,
"both": string.ascii_letters,
}
characters += methods[string_method]
if numbers:
characters += string.digits
if special_chars:
characters += string.punctuation
super().__init__(length=length, characters=characters)
class Memorable(Simple):
def __init__(self, numbers=True):
self.numbers = numbers
def generate(self, num_of_passwords: int):
word_url = "http://svnweb.freebsd.org/csrg/share/dict/words?view=co&content-type=text/plain"
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64)'}
req = urllib.request.Request(word_url, headers=headers)
response = response = urllib.request.urlopen(req)
long_txt = response.read().decode()
words = long_txt.splitlines()
for i in range(num_of_passwords):
password = ''
two_words = ''
for i in range(2):
two_words += secrets.choice(words).title()
password = two_words
if self.numbers == True:
for i in range(random.randint(3, 4)):
password += secrets.choice(string.digits)
output.append(password)
return output
# Test
c = Memorable()
print(c.generate(3))
print(c.return_result(1))
解决方案
您可以通过这种方式将生成的密码附加到输出中:
key = self.__class__.__name__
output.append({key:password}
这样,key
将是所引用的类的名称self
。如果 Complex 继承自 Simple,Complex 对象仍将打印“Complex”,而 Simple 对象将明显打印“Simple”。
然后,您的密码集将类似于:
[{
'Simple' : 'toto',
'Complex': 'E341rfeq1zqer'
}]