Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Implement a Password Manager Class in Python

Implement a Password Manager Class in Python

Document Object Reference Manual Python3 Examples

We will implement a simple password manager class in Python. This class lets users store, retrieve, and delete passwords. Passwords are stored in a dictionary where the key is the account name and the value is the password. For simplicity the passwords are stored in plain text, but in a real application encryption should be used to protect them.

Example

class

PasswordManager:

   

def

init

(

self

)

:

       

self

.

passwords

=

{

}

   

def

add_password

(

self

,

account

,

password

)

:

       

self

.

passwords

[

account

]

=

password

       

print

(

f

"Password for {account} added successfully."

)

   

def

get_password

(

self

,

account

)

:

       

if

account

in

self

.

passwords

:

           

return

self

.

passwords

[

account

]

       

else

:

           

return

f

"No password found for {account}."

   

def

delete_password

(

self

,

account

)

:

       

if

account

in

self

.

passwords

:

           

del

self

.

passwords

[

account

]

           

print

(

f

"Password for {account} deleted successfully."

)

       

else

:

           

print

(

f

"No password found for {account}."

)

   

def

list_accounts

(

self

)

:

       

if

self

.

passwords

:

           

print

(

"Accounts with stored passwords:"

)

           

for

account

in

self

.

passwords

:

               

print

(

account

)

       

else

:

           

print

(

"No accounts with stored passwords."

)

Code analysis:

  1. __init__ The method initializes an empty dictionary passwords, used to store accounts and passwords.
  2. add_password The method takes two parameters:account and password, and adds them to the passwords dictionary.
  3. get_password The method takes one parameter account, and returns the password for that account. If the account does not exist, it returns a corresponding message.
  4. delete_password The method takes one parameter account, and removes passwords the account and its password from the dictionary. If the account does not exist, it returns a corresponding message.
  5. list_accounts The method lists all accounts with stored passwords. If no account has a stored password, it returns a corresponding message.

Output result:

Example

Example usage

pm

=

PasswordManager

(

)

pm.

add_password

(

"email"

,

"myemailpassword"

)

pm.

add_password

(

"bank"

,

"mybankpassword"

)

print

(

pm.

get_password

(

"email"

)

)

 

Output: myemailpassword

pm.

delete_password

(

"bank"

)

pm.

list_accounts

(

)

 

Output: Accounts with stored passwords: email

This simple password manager class helps you manage passwords for multiple accounts and can be extended as needed, for example by adding encryption or persistent storage.

Document Object Reference Manual Python3 Examples