'Python - creating multiple dictionaries with some common keys, some unique

I am trying to set up a config file for each of the clients I need to run a program for. Each client uses multiple sources. Each source has some attributes that are consistent across all clients and some attributes are unique to each client. I have a method for doing this currently that involves building 2 dictionaries - one client specific that only contains client-specific attributes and one that contains all-client attributes, then merging the 2 by either using something like mergedeep or by iterating over each and merging each source dict within each client dict.

This feels clunky and I'm wondering if there is a better way to achieve this goal - creating a class for each source, then passing in client-specific options on creation of each class?

Any ideas are welcome :)

Example inputs below for my current methodology:

default_config = {
    'source1':{
        'attr1d':'a'
        ,'attr2d':'b'
    }
    ,'source2':{
        'attr3d':'c'
        ,'attr4d':'d'
    }
    ,'source3':{
        'attr5d':'e'
        ,'attr6d':'f'
    }
}

client_1_config = {
    'source1':{
        'attr1s':'x'
    }
    ,'source2':{
        'attr2s':'y'
    }
    ,'source3':{} #source3 has no client specific parameters but is still needed so is created as an empty dict to inherit default attributes
}

client_2_config = {
    'source1':{
        'attr1s':'l'
    }
    ,'source3':{
        'attr3s':'m'
    } 
}


desired output:
client_1_output = {
    'source1':{
        'attr1d':'a'
        ,'attr2d':'b'
        ,'attr1s':'x'
    } #this inherited default attributes from default_config, plus specific attributes from client_1_config
    ,'source2':{
        'attr3d':'c'
        ,'attr4d':'d'
        ,'attr2s':'y'
    } #this inherited default attributes from default_config, plus specific attributes from client_1_config
    ,'source3':{
        'attr5d':'e'
        'attr6d':'f'
    } #this has no specific attributes so ends up just being default attributes
}

client_2_output = {
    'source1':{
        'attr1d':'a'
        ,'attr2d':'b'
        ,'attr1s':'l'
    } #this inherited default attributes from default_config, plus specific attributes from client_1_config
#no source 2 for this client in client_config so it doesn't exist in the output
    ,'source3':{
        'attr5d':'e'
        ,'attr6d':'f'
        ,'attr3s':'m'
    } #this has specific attributes for this client so includes default attributes plus specific attributes
}

Is there any better/more elegant way of doing this than merging dictionaries, either using a library or by iterating over client_x_config and merging each source with default_config[source]?

Thanks in advance



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source