Advertisement
Guest User

Untitled

a guest
Sep 14th, 2017
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.99 KB | None | 0 0
  1. """
  2. Homework:
  3. def getUsers() updated to your version! and make sure its in the correct file path.
  4. Or just copy the text in to the file.
  5.  
  6. Then implement create user.
  7. After that if you have time comment all the existing code explaining how it works.
  8. """
  9. import getpass
  10. import os
  11. ALPHABET = ['A', 'Q', 'W', 'S', 'Z', 'X', 'C', 'D', 'E', 'R', 'F', 'V', 'B', 'G', 'T', 'Y', 'H', 'N', 'M', 'J', 'K',
  12. 'L', 'O', 'I', 'U', 'P', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0']
  13.  
  14.  
  15. USRERS = "users-new.csv"
  16.  
  17.  
  18. def getUserLogin(userMsg="user: ", pswMsg="password: "):
  19. """
  20. this will ask the user for a user name and password
  21. """
  22. return [raw_input(userMsg), getpass.getpass(pswMsg)]
  23.  
  24.  
  25. def getUsers():
  26. """
  27. this will open the csv file and split it at the commas to create a list of tow element lists
  28. """
  29. # REPLACE THIS WITH THE CSV FILE!!!
  30. # and if you get to the stage in your textbook about the csv module then have
  31. # a go at implementing it. Otherwise thats what we plan on doing next week.
  32.  
  33. fileRead = File(USRERS, "r")
  34. for i in range(len(fileRead)):
  35. fileRead[i] = fileRead[i].replace("\n", "")
  36. fileRead[i] = fileRead[i].split(",")
  37. return fileRead
  38.  
  39.  
  40. def login():
  41. """
  42. this function will call for a login
  43. then get a list of user logins
  44. and then call to check if any of the users where real
  45. and then return true
  46. """
  47. user = getUserLogin()
  48. users = getUsers() # [["user", "psw"], []]
  49. user_index = findUser(users, user[0])
  50. if user_index != -1:
  51. if user[1] == users[user_index][1]:
  52. return True
  53. return False
  54.  
  55.  
  56. def findUser(users, username):
  57. """
  58. this function will look through the list of users
  59. and then return if any were found
  60. """
  61. userFound = False
  62. count = 0
  63. while not userFound and count < len(users):
  64. if users[count][0] == username:
  65. userFound = True
  66. else:
  67. count += 1
  68. if not userFound:
  69. count = -1
  70. return count
  71.  
  72.  
  73. def UserExists(user):
  74. """
  75. this will go through a list of users and check if any of the users are already the same
  76. """
  77. # how can we use findUser with this?
  78. OpenUsers = File(USRERS, "r")
  79. for i in range(len(OpenUsers)):
  80. OpenUsers[i] = OpenUsers[i].replace("\n", "").split(",")
  81. if OpenUsers[i][0] == user:
  82. print("Username already used")
  83. return False
  84. return True
  85.  
  86.  
  87. def createUser():
  88. """
  89. check usernames doesn't already exist
  90. check passwords inputs are the same
  91. and basic password strength test e.g. length, numbers, specail chars
  92. invalid chars not in data? might not be needed
  93. """
  94. passwordSafe = False
  95.  
  96. running = 1
  97. while running == 1:
  98. newUser = raw_input("Type your user name >>>")
  99. if UserExists(newUser):
  100. Password = getpass.getpass("Type your password >>>")
  101. PasswordC = getpass.getpass("Confirm your password >>>")
  102. if Password == PasswordC:
  103. For = len(Password) # Bad varaible name with upper case but also meaningful?
  104. while not passwordSafe and For >= 0:
  105. For -= 1
  106. if Password[For] in ALPHABET:
  107. passwordSafe = True
  108.  
  109. if passwordSafe:
  110. UsernamePsw = str("\n" + newUser + "," + Password)
  111. File(USRERS, "a", UsernamePsw)
  112. running = 0
  113. main()
  114.  
  115.  
  116. def main():
  117. """
  118. this function will ask the user for an option either Login or New Account
  119. then it will call ether login or createUser
  120. """
  121. # good meaningful varaible names, but meaningful data types? 1 True and 0 False would be a lot better
  122. choosing = 1
  123. running = True
  124. if not os.path.isfile(USRERS):
  125. newFile=open(USRERS,"wb")
  126. newFile.write("")
  127. newFile.close()
  128. while running:
  129. choose = raw_input("Type 1 for Login \nInput 2 for a new acount\n\n>>>")
  130. if choose == "1":
  131. choosing = False
  132. trys = 3
  133. quitLoop = False
  134. while not login() and trys > 0 and not quitLoop:
  135. trys-=1
  136. print("try agian")
  137. running = False
  138. print("access granted")
  139. if choose == "2":
  140. choosing = False
  141. createUser()
  142. elif choosing:
  143. print("plese type 1 or 2")
  144.  
  145.  
  146. def File(fileName, task, usernamePsw=""):
  147. """
  148. this segment will open a file under the variable task and then
  149. either read the file or write to it.
  150. """
  151. if task == "r":
  152. fileOpen = open(USRERS, task)
  153. fileRead = fileOpen.readlines()
  154. # close is never called here
  155. fileOpen.close()
  156. return fileRead
  157. if task == "a" or "w":
  158. # filename should be used here instead of USRERS
  159. # even better use a global constant for file
  160. # also we want to work on the logic here but don't worry about that
  161. fileOpen = open(USRERS, task)
  162. fileOpen.writelines(usernamePsw)
  163. fileOpen.close()
  164.  
  165.  
  166.  
  167. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement