Home > OS >  How do I store an object in a dictionary in Python?
How do I store an object in a dictionary in Python?

Time:01-21

In my password manager prroject, I am trying to code a login function. In this functtion, if the user's username and password match an account stored in this dictionary, it allows access to their object which has the following attributes: username, password, password_coll. (The password_coll is a dictionary/collection of the users passwords as values to the website of use as keys). So as a little stem from my original question, how would I also reference my This is my first time using OOP approach and it is really frying my brain hahaha.

So I thought of using usernames as keys and the object as the value. But how do I structure this in code? Any examples would be greatly appreciated. I did try checking existing questions but they didn't answer my question closely enough. So here we are haha:) The code block at the bottom is my attempt at testing the output of those methods to see if they return the data in the object. But the result was this message: "<bound method User.return_pass of <main.User object at 0x0000023419597940>>"

import random
import secrets
import string

class User:
    def __init__(self, username, password, password_dict=None) -> None:
        self.username = username
        self.password = password
        self.password_dict = {}
    
    def return_pass(self, password):
        return self.password   
    def __str__(self, password) -> str:
        return self.password


    def get_creds(self, username, password):
        usern = input('Enter username:   ')
        pwd = input('Enter password:   ')
        self.username = usern
        self.password = pwd
    def passGen(self, password_dict): # random password generator
        n = int(input('Define password length. Longer passwords are safer.'))

        source = string.ascii_letters   string.digits
        password = ''.join((secrets.choice(source)) for i in range(n))
        print('Password has been generated!')

        print('Would you like to save this password? Type y or n: ')
        yon = input()

        if yon == 'y':
            site = input('Please enter the site password is to be used:')
            self.password_dict[site] = password

        return self.password_dict



u1 = User('dave', 'pass', {})
user_logins = {'dave': u1}
print(user_logins['dave'].return_pass)

CodePudding user response:

User.return_pass is a function, it has to be called: print(user_logins['dave'].return_pass("password")) where the text "password" is the arg required in the function.

Hope this helps

CodePudding user response:

def login(username, password, user_logins):
    if username in user_logins:
        user = user_logins[username]
        if user.password == password:
            return user.password_dict
        else:
            return "Incorrect password"
    else:
        return "Username not found"

print(login('dave', 'pass', user_logins))

In your code, you're trying to print the output of a function, but you forgot to actually run the function by adding parentheses at the end. So instead of just printing the function, you need to run it by adding () at the end. Also, the str method in the User class should not take any input, and it should return the value of self.password instead of just 'password' print(user_logins['dave'].return_pass())

  • Related