Dec 18, 2016

Simulate Login and Registration of New User using Python data structures

Python comes in very handy when dealing with data, for data crunching, machine learning,
interactions on the web.
Today we aim to make a authentication mechanism using Python.
Using python dictionaries to simulate database for user information.
Give the user two options,
a. Add new User
b. Log In with existing credentials
a. For new user registration in system, input parameters are
NAME,ROLL,EMAIL,PASSWORD(comma separated values).
Validate entries as-
- email Id – email id acceptable(SOMEONE@students.devinline.ac.in)
- Password length at least 8 characters
- Roll number should be a integer [optional if you do not know python regex]
If valid entry- print “Success”
Else print - “Registration Failed”
b. For logging in, input parameters are ROLL/EMAIL,PASSWORD
Input can be roll number or email, both are valid.
If authentication is successful print “Login Success”
Else print “Login Fails”

Sample code - Simulate Login and Registration using Dict


import re

class Students:
    def __init__(self, name, rollNo, email, password):
        self.name = name
        self.rollNo = rollNo
        self.email = email
        self.password = password 


def doDBOperation(choice,studentDict1,studentDict2):
    if choice == 1:
        print "Enter name, rollNo, password, email(Separated by comma)"
        inputArr = map(str,raw_input().strip().split(','))
        name=inputArr[0]
        rollNo=inputArr[1]
        email=inputArr[2]
        password=inputArr[3]
        #print name, rollNo, password, email
        stat = adduser(name, rollNo, email,password) 
        if stat == "SUCCESS":
            student = Students(name, rollNo, email, password)
            studentDict1[rollNo] = student
            studentDict2[email] = student
            print "Success"
        else:
            print "Registration Failed"
    elif choice == 2:
        print "Enter loginId(Roll or Email) and password"
        inputArr = map(str,raw_input().strip().split(','))
        rollorEmail=inputArr[0]
        password=inputArr[1]
        stat=loginService(studentDict1,studentDict2, rollorEmail, password)
        if stat == "SUCCESS":
            print "Login Success!!"
        else:
            print "Login Fails !!"
    print "Enter your choice(1|2)"
def adduser(name, rollNo, email, password):
    if validateEmail(email) and validatePassword(password) and validateRollNo(rollNo) :
        return "SUCCESS"
    else:
        return "FAILURE"

def loginService(studentDict1,studentDict2,rollorEmail,pwd):
    if studentDict1.has_key(rollorEmail) or studentDict2.has_key(rollorEmail):
        if studentDict1.has_key(rollorEmail):
            studentObj = studentDict1[rollorEmail]
        else:
            studentObj = studentDict2[rollorEmail]
        if studentObj.password == pwd:
            return "SUCCESS"
        else:
            return "FAILURE"

def validateEmail(email):
    pattern = "^[a-z]+[.]?[a-z]*@students.devinline.ac.in"
    prog = re.compile(pattern)
    result = prog.match(email)
    if result != None:
        return True
    else:
        return False
    

def validateRollNo(rollNo):
    rollNo = str(rollNo)
    return rollNo.isdigit()

def validatePassword(password):
    if len(password) >= 8:
        return True
    else:
        return  False



if __name__ == "__main__" :
    studentDict1 = { }
    studentDict2 = { }
    print "Welcome to Devinline authentication system"\
        "\nChoose:\
        \n1-Register New User\
        \n2-Log In \
        \nAny other - Exit program"
    while True:
        choice=int(input())
        if choice == 1  or choice == 2: 
            doDBOperation(choice,studentDict1,studentDict2)
        else :
            print "OK Bye"
            break


Sample Output:-

>>>
Welcome to Devinline authentication system
Choose:      
1-Register New User      
2-Log In      
Any other - Exit program
1
Enter name, rollNo, password, email(Separated by comma)
alia,20160047,alia@students.devinline.ac.in,**alia**
Success
Enter your choice(1|2)
2
Enter loginId(Roll or Email) and password
20160047,**alia**
Login Success!!
Location: Hyderabad, Telangana, India