From 32bc8421d46eb8595b709e40f17ead6727207b88 Mon Sep 17 00:00:00 2001 From: Laura Mason <123578090+Lmason14@users.noreply.github.com> Date: Tue, 28 Feb 2023 12:23:53 +0000 Subject: [PATCH] Add files via upload --- password_manager.py | 72 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 password_manager.py diff --git a/password_manager.py b/password_manager.py new file mode 100644 index 0000000..e8eac8a --- /dev/null +++ b/password_manager.py @@ -0,0 +1,72 @@ +# == INSTRUCTIONS == +# +# Purpose: Manage a user's (valid) passwords +# +# Methods: +# 1. Name: __init__ +# Arguments: none +# 2. Name: add +# Purpose: add a password for a service IF it is valid, otherwise do nothing +# Arguments: one string representing a service name, +# one string representing a password +# Returns: None +# 3. Name: get_for_service +# Arguments: one string representing a service name +# Returns: the password for the given service, or None if none exists +# 4. Name: list_services +# Arguments: none +# Returns: a list of all the services for which the user has a password +# +# A reminder of the validity rules: +# 1. A password must be at least 8 characters long +# 2. A password must contain at least one of each of the following: +# - It must be longer than 7 characters (8 is fine) +# - It must contain at least one of the following special characters: +# `!`, `@`, `$`, `%` or `&` +# +# We recommend that you store the passwords in a dictionary, where the keys are +# the service names and the values are the passwords. +# +# Example usage: +# > password_manager = PasswordManager() +# > password_manager.add('gmail', '12ab5!678') # Valid password +# > password_manager.add('facebook', '$abc1234') # Valid password +# > password_manager.add('twitter', '12345678') # Invalid password, so ignored +# > password_manager.get_for_service('facebook') +# '$abc1234' +# > password_manager.get_for_service('not_real') +# None +# > password_manager.get_for_service('twitter') +# None +# > password_manager.list_services() +# ['gmail', 'facebook'] +# + +# == YOUR CODE == + +class PasswordManager(): + + def __init__(self): + self.password_store = {} + + def add(self, service_name, password): + self.service_name = service_name + self.password = password + specials = ["!", "@", "$", "%", "&"] + if len(password) > 7: + if any(c in specials for c in password): + self.password_store[service_name] = password + else: + return False + else: + return False + + def get_for_service(self, service_name): + if service_name in self.password_store: + return self.password_store.get(service_name, self.password) + else: + return None + + + def list_services(self): + return self.password_store.keys() \ No newline at end of file