![](/img/trans.png)
[英]Creating a class the have a “list creation” method and an “append to the list” method?
[英]How can I have a function/method append passwords to a different list depending on what class is using it?
我正在制作一个密码生成器,作为我在 python 中的 OOP 的第一次尝试。 我有 3 个类Simple
、 Complex
和Memorable
。 Simple是Complex
的父级,而 Memorable 也是Simple的子级 class。 Simple
和Complex
都有相同的generate方法来生成密码:
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 有这个生成 function 代替:
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 看起来像这样: output = []
。 问题是所有密码都被附加到完全相同的列表中。 我知道这可以通过为每个class设置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))
你可以 append 你生成的密码 output 这样:
key = self.__class__.__name__
output.append({key:password}
这样, key
将是 class self
引用的名称。 如果 Complex 继承自 Simple,Complex object 仍将打印“Complex”,而 Simple 对象将明显打印“Simple”。
然后,您的密码集将类似于:
[{
'Simple' : 'toto',
'Complex': 'E341rfeq1zqer'
}]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.