Building a User Configuration Manager - several problems / questions

Tell us what’s happening:

Sorry for the large confused block of code.

I have several problems:

  1. I don’t understand why my lower_case(dictionary) function breaks the validation of making the key lower case. I’m not actually sure which key it’s telling me to change the case of, I assumed it was the argument-key being passed to the function so I don’t know why my changing of the original test dictionary would make a difference.

  2. I get a Keyerror when attempting to delete either my beginning settings, Theme or Notifications, or some variation of the text, ie. the or them. But, if I update the settings then it all seems to work. I tried del dictionary[key] which didn’t work. I tried the .get() method but that didn’t work either.

  3. Depending on how I toggle my settings and change the str() function and .lower() method I either go all the way to the beginning with “add setting should change the key to lower case” or to the current problem of producing a Keyerror and/or simply unable to match the desired key to the key_set.

  4. Now I’m completely lost in something that I’ve done because I’m back to failing the first validation of changing the add setting key to lower case when all of my function calls output correctly. Owell, off to work.

Your code so far

test_settings = {"THEME": "Light", "notifiCations": "enaBled"}

print(f"{test_settings}\n\n")

def add_setting(dictionary, settings):
    #dictionary = lower_case(dictionary)
    key = settings[0].lower()
    value = settings[1].lower()
    key_set = str(dictionary.keys()).lower()
    
    if key not in key_set:
    #if str(key) not in str(key_set):
    #if str(key).lower() not in str(key_set).lower():
        return f"Setting '{key}' already exists! Cannot add a new setting with this name."

    dictionary[key] = value
    print(f"{view_settings(dictionary)}\n")
    return f"Setting '{key}' added with value '{value}' successfully!"


def update_setting(dictionary, settings):
    #dictionary = lower_case(dictionary)
    key = settings[0].lower()
    value = settings[1].lower()
    key_set = str(dictionary.keys()).lower()
    
    if key not in key_set:
    #if str(key) not in str(key_set):
    #if str(key).lower() not in str(key_set).lower():
        return f"Setting '{key}' does not exist! Cannot update a non-existing setting."

    dictionary[key] = value
    print(f"{view_settings(dictionary)}\n")
    return f"Setting '{key}' updated to '{value}' successfully!"

def delete_setting(dictionary, settings):
    key = settings[0].lower()
    value = settings[1].lower()
    key_set = str(dictionary.keys()).lower()
    #print(f"key set: {key_set}\n")
    #dictionary = lower_case(dictionary)

    #print(f"key change: {key}\n")
    #print(f"dictionary: {dictionary}\n")
    
    if key not in key_set:
        #print(f"{view_settings(dictionary)}\n")
        return f"Setting not found!"

    dictionary.pop(key)
    #print(f"{view_settings(dictionary)}\n")
    return f"Setting '{key}' deleted successfully!"    

def view_settings(dictionary):
    dictionary = lower_case(dictionary)
    if not dictionary:
        return "No settings available."
    return f"\nCurrent User Settings: {dictionary}"

def lower_case(dictionary):
    lowercase_dictionary = {}
    for key, value in dictionary.items():
        lowercase_dictionary[key.lower()] = value
    for key,value in lowercase_dictionary.items():
        lowercase_dictionary[key] = value.lower()
    dictionary = lowercase_dictionary
    return dictionary

print(f"{add_setting((test_settings), ('Volume', 'High'))}\n")

#print(f"{add_setting((test_settings), ('Theme', 'Light'))}\n")

print(f"{update_setting(test_settings, ('Theme', 'Dark'))}\n")
print(f"{update_setting(test_settings, ('Screen', 'Large'))}\n")
print(f"{update_setting(test_settings, ('Notifications', 'Disable'))}\n")

print(f"{delete_setting(test_settings, ('volume', 'Low'))}\n")

print(f"{delete_setting(test_settings, ('Theme', 'Dark'))}\n")
print(f"{delete_setting(test_settings, ('Notifications', 'Enabled'))}\n")

print(f"{view_settings(test_settings)}\n")

Lesson URL (copy - paste from your browser’s address bar)

Hi @snowbv,

Is this code implementing this instruction correctly?

  • If the key setting exists, return Setting '[key]' already exists! Cannot add a new setting with this name.

If add_setting always lowers the case of the key and value, does it make sense for your test_settings dictionary to have items that are not lowercased?

Happy coding

Well, uh. That help wasn’t really all that helpful. I appreciate the response though!

I ended up re-writing the whole thing and either I’ve re-written and re-thought about it so many times that my final solution seems rather simple.

I had a lot of trouble with the final steps for the view_settings() function. I learned / figured out that whatever I was doing was iterating over the same variable and so my output kept over writing itself and all I would get is the last key:value pair.

I can understand now why my attempt to change the entire dictionary to lower case was wreaking havoc in the validation checks. It wanted to output the settings values as input.

My code block that output the settings certainly wasn’t as short or elegant as other’s, but I’m more happy that I came up with a solution that worked on my own (mostly, after I saw what other answers were doing and puzzled my way through getting it to do the same).