1

Simple command line program to create login

raw_input to enter username and password

username and password stored in a dictionary

class UserRegistration(object):

    username_and_password = {}
    def __init__(self, username, password):
        self.username = username
        self.password = password

    username = raw_input("Choose username>")
    password = raw_input("Choose password>")

    username_and_password[username] = password  

Does this module follow convention / is it best practice? How can I optimize it? Is there a better standard to create logins/command line logins?

MattDMo
  • 96,286
  • 20
  • 232
  • 224
z82n
  • 125
  • 2
  • 10
  • For the password you would better use `getpass`, see http://stackoverflow.com/questions/9202224/getting-command-line-password-input-in-python. – alecxe Aug 25 '14 at 00:28
  • @alecxe Thanks, by the way, how do I get it to run on demand? Do I have to create an object and invoke it or something? – z82n Aug 25 '14 at 00:42
  • 1
    You should never save a password only the [**hash**](https://docs.python.org/2/library/hashlib.html) value. I would also validate the input before you store it. – Dan Aug 25 '14 at 01:49
  • @Monacraft because I am learning through tutorials and everybody disrecommends using 3.0 for beginners – z82n Aug 25 '14 at 02:37

1 Answers1

0

You don't need to set username_and_password as a class variable so that it can't be accessed by other class. The same problem exists with variable username and password. And you don't use self.username/self.password so there is no need to assign them. Meanwhile, getpass() is also an optimization. So I think the best practice is:

class UserRegistration(object):
    def __init__(self):
        self.username_and_password = {}
        username = raw_input("Choose username>")
        password = raw_input("Choose password>")

        self.username_and_password[username] = password 

EDIT: If you want to read content of dict, you can do this:

myInfo = UserRegistration() # in this line, user will be required to enter username and password
username = raw_input('please enter username whose password you want to know: ')
print myInfo.username_and_password[username]
Stephen Lin
  • 4,776
  • 1
  • 10
  • 26