There are no errors or problems with the below code I want to "connect" this script to an external file or database so that that "my_dict" dictionary can have its contents stored and used after the script is executed. I am looking for ideas on how to make this happen. I am not asking for the code to be fixed as it is functional. It is merely there to provide context.
my_dict = {}
class password(object):
def __init__(self):
pass
def username_input(self):
self.user = str(input("Please enter the username you would like to use: "))
return self.user
#gets the input for a username
def password_input(self):
self.passw = str(input("Please enter the password you would like to use: "))
return self.passw
#gets the input for a password
def username_creator(self):
password.username_input(self)
return self.user
def password_creator(self):
password.password_input(self)
return self.passw
def swap_username(self, new_pass, old_pass):
my_dict[new_pass] = my_dict.pop(old_pass)
def swap_password(self, new_pas, old_pas):
my_dict[old_pas] = new_pas
def username_change(self):
self.d = input("please enter your username: ")
for key in my_dict:
if key == self.d:
self.x = input("enter a new username: ")
password().swap_username(self.x, self.d)
print("your username has been updated!")
break
else:
print("the username you entered does not exist in our records")
password().username_change()
def password_change(self):
self.e = input("please enter your username: ")
self.q = input("please enter your password: ")
for key, value in my_dict.items():
if key == self.e and value == self.q:
self.z = input("enter a new password: ")
password().swap_password(self.z, self.e)
break
else:
print ('the combination entered does not exist in our records')
password().password_change()
make_a_username = password()
my_dict.update({(make_a_username.username_creator()): (make_a_username.password_creator())})
One way of achieving this is by storing my_dict
in a file. So basically, whenever you start the app, you'd be loading my_dict
from the file, and before closing, storing the updated my_dict
in the file again. Since it's a dictionary, you could store it in JSON.
So, what you're ultimately doing is:
// to save
json_string = json.dumps(datastore)
// now write this string to a file
// to load, first read json_string from file. Then,
my_dict = json.loads(json_string)
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.