To manage a user list that can be modified and saved to a text file. Inputs: Out
ID: 3581647 • Letter: T
Question
To manage a user list that can be modified and saved to a text file.
Inputs:
Output:
List of user accounts, error messages when appropriate
Output text file consisting of pairs of username and passwords, separated by a colon (:) without any spaces
Specification:
The program maintains a list of user accounts and passwords supplied by a system administrator.
Any inputted username should be stripped of any non-alphanumeric characters (special characters such as ! @ # $ % ^ & * ( ) _ + ; : ’ ”) using: username = re.sub(r'W+','',username). In order to use the re.sub command, you need to import the appropriate library with "import re" at the top of your code.
Any inputted password should be stripped of the apostrophe character ' using: password = password.replace(''', '')
Each option should be implemented as a separate function within a Package that will be imported, so that its functions can be called. Valid options will be stored in a list to test for invalid options:
function = [‘i’, ‘m’, ‘r’, ‘g’, ‘q’]
The usernames and passwords will be loaded from an input file and stored in a dictionary (Python’s version of a hash). The dictionary will be passed as a parameter from the Python script file to the appropriate function in the module file and updated according to the function. The script file should import the functions from the module file to call the appropriate function.
Initially, the program prompts for an input text file to read from. If the file does not exist, it should be created. The program should then loop until the user chooses to quit (selects status as ‘q’). If the user enters an illegal status, the program will prompt again for the status input. Upon quitting, the program prompts the user to save the list to an output file with the same name as the original input text file.
Input text file consisting of pairs of usernames and passwords, separated by a colon (:) without any spaces
User choice: (‘a’-add user account, ‘m’-modify existing user account, ‘r’- remove existing user account, ‘g’-
generate list of user accounts, ‘q’-quit)
Sample Output:
Explanation / Answer
import re
class UserManager(object):
def __init__(self):
self.user_db = {}
self.file_path = ''
def read_db(self):
self.file_path = input('Enter file to open: ')
try:
lines = [line.rstrip(' ') for line in open(self.file_path, 'r')]
self.user_db = dict(user.split(':', 1) for user in lines)
except FileNotFoundError:
self.user_db = dict()
def help_msg(self):
print('')
print('User accounts')
print('-------------')
print('a = Add user account')
print('m = Modify existing user account')
print('r = Remove existing user account')
print('g = Generate list of user accounts')
print('q = Quit')
print('')
def save_file(self):
save = input('Save contents? (y/n): ')
if not save.lower() == 'y':
return 1
with open(self.file_path, 'w+') as f:
for user, passwd in self.user_db.items():
print('{0}:{1}'.format(user, passwd), file=f)
return 1
def show_list(self):
for user, passwd in self.user_db.items():
print('{0}:{1}'.format(user, passwd))
return 0
def add_user(self):
username = self._get_sanitized_username('Enter username: ')
if username in self.user_db:
print('Username already exists!')
return 0
password = self._get_sanitized_password('Enter password: ')
self.user_db[username] = password
return 0
def mod_user(self):
username = self._get_sanitized_username('Enter username to modify: ')
if not username in self.user_db:
print('Username does not exist!')
return 0
c_pass = self._get_sanitized_password('Enter current password: ')
if not c_pass == self.user_db[username]:
print('Incorrect password!')
return 0
n_pass = self._get_sanitized_password('Enter new password: ')
self.user_db[username] = n_pass
return 0
def del_user(self):
username = self._get_sanitized_username('Enter username to delete: ')
if not username in self.user_db:
print('User does not exist')
return 0
del self.user_db[username]
print('User removed')
return 0
def _get_sanitized_username(self, message):
username = input(message)
return re.sub(r'W+', '', username)
def _get_sanitized_password(self, message):
password = input(message)
return password.replace(''', '')
file.txt
newton:force=mass*accelaration
einstein:e=mc^2
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.