We have a configuration file that is read and stored as a dictionary, e.g.
config = {'type': A, 'input': {'color': 'red', 'border': 'thick', 'shape': 'square'}}
Now we want to use this config object to create an instance of a class, say, Drawing_Class with the condition that if the type is 'A', the colour is always blue, and if the type is 'B' the shape is always a circle.
I have written a function to achieve this which seems very unpythonic, and was wondering if there is a better way to achieve my goal? Of course, in the example I've given one could just input the desired colour and shape in the configuration file, but the actual problem is more complex and that can't be achieved.
Here is my function.
def drawing_dictionary(type):
if type == 'A':
value = config['inputs'].pop('color')
output = drawing_function(**config['inputs'], colour='blue')
config['input']['color'] = value
if type == 'B':
value = config['inputs'].pop('shape')
output = drawing_function(**config['inputs'], shape='circle')
config['input']['shape'] = value
return output
It achieves what I need it to achieve, i.e. creating custom instances based on this configuration file, but in a way that seems very inelegant. Help would be much appreciated.