简体   繁体   中英

How can I have a function/method append passwords to a different list depending on what class is using it?

I am making a password generator as my first stab at OOP in python. I have 3 classes Simple , Complex and Memorable . Simple is the parent to Complex and Memorable is also a child class of Simple . Both Simple and Complex have the same generate method to generate a password:

    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 has this generate function instead:

    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

The generate functions above appends each password generated to a list called 'output'. output looks like this: output = [] . The problem is all passwords are being appended to the exact same list . I know this could be solved easily by just having 3 different lists for each class but is there a better way to do it? Thanks:D

Full code: (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))

You could append your generated passwords to output this way:

key = self.__class__.__name__
output.append({key:password}

This way, key would be the name of the class self is refering. If Complex herits from Simple, a Complex object will still prints "Complex" and the Simple objects will print "Simple" obviously.

Then, your set of passwords would be something like:

[{
   'Simple' : 'toto',
   'Complex': 'E341rfeq1zqer'
}]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM