Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- Homework:
- def getUsers() updated to your version! and make sure its in the correct file path.
- Or just copy the text in to the file.
- Then implement create user.
- After that if you have time comment all the existing code explaining how it works.
- """
- import getpass
- import os
- ALPHABET = ['A', 'Q', 'W', 'S', 'Z', 'X', 'C', 'D', 'E', 'R', 'F', 'V', 'B', 'G', 'T', 'Y', 'H', 'N', 'M', 'J', 'K',
- 'L', 'O', 'I', 'U', 'P', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0']
- USRERS = "users-new.csv"
- def getUserLogin(userMsg="user: ", pswMsg="password: "):
- """
- this will ask the user for a user name and password
- """
- return [raw_input(userMsg), getpass.getpass(pswMsg)]
- def getUsers():
- """
- this will open the csv file and split it at the commas to create a list of tow element lists
- """
- # REPLACE THIS WITH THE CSV FILE!!!
- # and if you get to the stage in your textbook about the csv module then have
- # a go at implementing it. Otherwise thats what we plan on doing next week.
- fileRead = File(USRERS, "r")
- for i in range(len(fileRead)):
- fileRead[i] = fileRead[i].replace("\n", "")
- fileRead[i] = fileRead[i].split(",")
- return fileRead
- def login():
- """
- this function will call for a login
- then get a list of user logins
- and then call to check if any of the users where real
- and then return true
- """
- user = getUserLogin()
- users = getUsers() # [["user", "psw"], []]
- user_index = findUser(users, user[0])
- if user_index != -1:
- if user[1] == users[user_index][1]:
- return True
- return False
- def findUser(users, username):
- """
- this function will look through the list of users
- and then return if any were found
- """
- userFound = False
- count = 0
- while not userFound and count < len(users):
- if users[count][0] == username:
- userFound = True
- else:
- count += 1
- if not userFound:
- count = -1
- return count
- def UserExists(user):
- """
- this will go through a list of users and check if any of the users are already the same
- """
- # how can we use findUser with this?
- OpenUsers = File(USRERS, "r")
- for i in range(len(OpenUsers)):
- OpenUsers[i] = OpenUsers[i].replace("\n", "").split(",")
- if OpenUsers[i][0] == user:
- print("Username already used")
- return False
- return True
- def createUser():
- """
- check usernames doesn't already exist
- check passwords inputs are the same
- and basic password strength test e.g. length, numbers, specail chars
- invalid chars not in data? might not be needed
- """
- passwordSafe = False
- running = 1
- while running == 1:
- newUser = raw_input("Type your user name >>>")
- if UserExists(newUser):
- Password = getpass.getpass("Type your password >>>")
- PasswordC = getpass.getpass("Confirm your password >>>")
- if Password == PasswordC:
- For = len(Password) # Bad varaible name with upper case but also meaningful?
- while not passwordSafe and For >= 0:
- For -= 1
- if Password[For] in ALPHABET:
- passwordSafe = True
- if passwordSafe:
- UsernamePsw = str("\n" + newUser + "," + Password)
- File(USRERS, "a", UsernamePsw)
- running = 0
- main()
- def main():
- """
- this function will ask the user for an option either Login or New Account
- then it will call ether login or createUser
- """
- # good meaningful varaible names, but meaningful data types? 1 True and 0 False would be a lot better
- choosing = 1
- running = True
- if not os.path.isfile(USRERS):
- newFile=open(USRERS,"wb")
- newFile.write("")
- newFile.close()
- while running:
- choose = raw_input("Type 1 for Login \nInput 2 for a new acount\n\n>>>")
- if choose == "1":
- choosing = False
- trys = 3
- quitLoop = False
- while not login() and trys > 0 and not quitLoop:
- trys-=1
- print("try agian")
- running = False
- print("access granted")
- if choose == "2":
- choosing = False
- createUser()
- elif choosing:
- print("plese type 1 or 2")
- def File(fileName, task, usernamePsw=""):
- """
- this segment will open a file under the variable task and then
- either read the file or write to it.
- """
- if task == "r":
- fileOpen = open(USRERS, task)
- fileRead = fileOpen.readlines()
- # close is never called here
- fileOpen.close()
- return fileRead
- if task == "a" or "w":
- # filename should be used here instead of USRERS
- # even better use a global constant for file
- # also we want to work on the logic here but don't worry about that
- fileOpen = open(USRERS, task)
- fileOpen.writelines(usernamePsw)
- fileOpen.close()
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement