Python Lab, Build a User Configuration Manager

I have this code for add_setting()

def add_setting(settings_dic, req_tuple): 
   
    #unpacking a dic and defining new lowered k:v members
    settings_dic = {k.lower(): v.lower() for k, v in settings_dic.items()}

    #unpacking a tuple
    k, v = req_tuple  
    print(f'k, v in req_tuple= {k}, {v}')
    req_tuple = (k.lower(), v.lower())
    req_key, req_value = req_tuple 
    print(f'k, v in req_tuple= {req_key}, {req_value}')


    if req_key in settings_dic.keys():
        return f"Setting '{req_key}' already exists! Cannot add a new setting with this name."
    else:
        settings_dic[req_key] = req_value
        print(f'\ninside_func:\nsettings_dic after added things={settings_dic}')
        return f"Setting '{req_key}' added with value '{req_value}' successfully!"

I also have written a test:

def test_this_file():
    print('\n======\nTests:\n')
    print("\n add_setting() output 1:\n", add_setting(test_settings , ('volume', 'high')))
    print("\n add_setting() output 2:\n", add_setting({'tHeme': 'light'}, ('Volume', 'high')))

    #print(f'\noutside_func:\nsettings_dic after added things={settings_dic}')
    return

which outputs this:

======
Tests:

k, v in req_tuple= volume, high
k, v in req_tuple= volume, high

 add_setting() output 1:
 Setting 'volume' already exists! Cannot add a new setting with this name.
k, v in req_tuple= Volume, high
k, v in req_tuple= volume, high

inside_func:
settings_dic after added things={'theme': 'light', 'volume': 'high'}

 add_setting() output 2:
 Setting 'volume' added with value 'high' successfully!

Now my questions:

Q0. Should have I opened a new topic or what?

Q1. I get the following message

> Failed: 4. add_setting should convert the key to lowercase.

However my tests output tells me I have already done it. (Or I see it like this)

Q2. in the same way I get the message below

> Failed: 5. add_setting should convert the value to lowercase.

Even 7 and 8 (to me) seems passed when I see my test results:

Failed 7. add_setting({'theme': 'light'}, ('volume', 'high')) should add a new key-value pair and return the success message Setting 'volume' added with value 'high' successfully!.

Failed: 8. add_setting should correctly add the given key-value pair to the dictionary.


:eight_spoked_asterisk: I wanna take the most of these precious projects. So it would be nice to know if:

Q4) Does the tester in website works in the way I am explaining? It gets my file. the tester is another file running as main function which reads my py-file, finds the desired defined function and calls it with plenty of its own test cases and checks if desired outputs are there.

Q5 ) Can we see inside those test files (if there is any open) to get better understanding of writing the test or even knowing what exactly are we supposed to provide?

Q6) when I come across this part of my own code:

    #unpacking a tuple
    k, v = req_tuple  
    print(f'k, v in req_tuple= {k}, {v}')
    req_tuple = (k.lower(), v.lower())
    req_key, req_value = req_tuple 
    print(f'k, v in req_tuple= {req_key}, {req_value}')

It seems to me that I changed a tuple! Am I wrong? if not, wasn’t a tuple meant to be unchangeable? Please let me know where my problem is.
I know this was not asked in the lab project. But the answer is important to me.

can you please share the link to this lab?

but I would ask you, are you actually adding the new setting to the dictionary?

my question of above stands

oh, fast replies :grinning_face_with_smiling_eyes: I checked.
No, I thought i was. I re-wrote my test and understood it did not change.

However, I expected dictionaries to change because of this concept:

The code
settings_dic = ...
just makes the local name point to a new object. The caller’s variable still points to the original object.
Dictionaries are mutable, so you can modify them in place:

settings_dic["new_key"] = "new_value"

The caller will see all these changes because it’s the same object.

after you write settings_dic = {k.lower(): v.lower() for k, v in settings_dic.items()}, the dictionary assigned to settings_dlc is a new one, not the one used here:

so test_settings is not being changed

Can I, from this point of view you speak, say that in this code:

#unpacking a tuple
req_tuple = ('voluMe', 'hiGh')
k, v = req_tuple  
print(f'k, v in req_tuple= {k}, {v}')
req_tuple = (k.lower(), v.lower())
req_key, req_value = req_tuple 
print(f'k, v in req_tuple= {req_key}, {req_value}')


that gives this output:

#unpacking a tuple
req_tuple = ('voluMe', 'hiGh')
k, v = req_tuple  
print(f'k, v in req_tuple= {k}, {v}')
req_tuple = (k.lower(), v.lower())
req_key, req_value = req_tuple 
print(f'k, v in req_tuple= {req_key}, {req_value}')


the req_tuple is also a new one? So I did not change a tuple?

Hi @reThinker,

I didn’t understand what you were trying to communicate in your last post, but you can use a direct dictionary assignment like this:

Hope that helps.

Hi @dhess
I explain more:
I managed to solve add_setting()
I also managed to solve update_setting() with the very thing you write here:
settings_dic["new_key"] = "new_value"

What I asked, was another related issue to make sure I understand things exactly as they are. I’ll be glad if you could feel free to answer it separately.

You can find out yourself by testing it:

Let’s say you’ve assigned your global variable test_settings like this:
test_settings = { "theme": "light", "notifications": "enabled"}

Below your code, create another global variable called new_setting and assign it a key/value pair that’s not in test_settings. Add uppercase letters in the key/value pair so you can compare later, like this:
new_setting = ('Volume', 'hiGh')

Change your function call to:
print("\\n add_setting() output 1:\\n", add_setting(test_settings , new_setting))

And add:

print(test_settings)
print(new_setting)

If you have actually changed the global variables, you will see the result in the final prints.

Hope that helps.

I ran it:

def test_this_file():
    test_settings = { "theme": "light", "notifications": "enabled"}
    new_setting = ('Volume', 'hiGh')
    print("\n\n add_setting() output 1:\n", add_setting(test_settings , new_setting))
    print('\ntest_settings=',test_settings)
    print('new_setting=',new_setting)
    return

outputs:

 add_setting() output 1:
 Setting 'volume' added with value 'high' successfully!
test_settings= {'theme': 'light', 'notifications': 'enabled', 'volume': 'high'}
new_setting= ('Volume', 'hiGh')

So, the tuple is unchanged.

Now, I passed this lab. Thanks for the help alongside.
One thing remained: for some the questions here in the first post of the topic, which do not have answers yet, am I supposed to open new topics for each?

we ask you open a topic when you need help with a new challenge, and that you don’t open multiple topics for the same challenge

We don’t use a standard python environment, we use an engine that allows running Python in the browser called Pyodide, so there aren’t files involved. But it is true that the tests call your functions and check outputs and side effects (in this case the dictionary change).

the tests are visible for each challenge in the freeCodeCamp github repository, which is open source.
You will find the tests in a markdown file, but then the markdown files are parsed to create the challenge page and its tests.