Home > Enterprise >  Nested dictionary value acts as a counter
Nested dictionary value acts as a counter

Time:11-02

I have the below code and am trying to generate a new dictionary of parameters. I would like the new dictionary to be a nested dictionary, and one of the nested keys be the most recent value, while the second nested key to be a counter that increases each time a value is updated.

options = {'threads' : '1', 'tolerance' : '1'}
options_2 = {'threads' : '2', 'timeout': True}

over_params = {}

def overrides_tracker(options, over_params):
    print(options)
    for k, v in options.items():
        over_params[k]['count']  = 1
        over_params[k]['value'] = v
        
    return over_params
        

over_params = overrides_tracker(options, over_params)
over_params = overrides_tracker(options_2, over_params)
        
print(over_params)

Right now my code is returning a KeyError. Is there an easier way to implement this sort of counter. Note that the value does not matter if the count of times that parameter is overridden is greater than 1. (That's why I don't mind just updating the value and replacing it.)

I would like for the resulting dictionary to look like this with the given inputs and function calls. (Note that the 'threads' key has a count value of 2.

{'threads': {'count': 2, 'value': '2'}, 
'tolerance': {'count': 1, 'value': '1'}, 
'timeout': {'count': 1, 'value': True}}

CodePudding user response:

over_params[k]['count'] = 1 won't work if over_params[k] isn't already a dict; you can fix this like follows:

options = {'threads' : '1', 'tolerance' : '1'}
options_2 = {'threads' : '2', 'timeout': True}

over_params = {}

def overrides_tracker(options, over_params):
    print(options)
    for k, v in options.items():
        if k in over_params:
            over_params[k]['count']  = 1
            over_params[k]['value'] = v
        else:
            over_params[k]={'count':1,'value':v}
        
    return over_params
        

over_params = overrides_tracker(options, over_params)
over_params = overrides_tracker(options_2, over_params)
        
print(over_params)
# {'threads': {'count': 2, 'value': '2'}, 'tolerance': {'count': 1, 'value': '1'}, 'timeout': {'count': 1, 'value': True}}

CodePudding user response:

Try something like this:

def overrides_tracker(options, over_params):
    print(options)
    for k, v in options.items():
        if k in over_params:
            over_params[k]['count']  = 1
        else:
            over_params[k] = {'count': 1}
        over_params[k]['value'] = v
    return over_params

CodePudding user response:

You need to instantiate values for your keys if they don't exist in the over_params dict and in the nested dicts within:

options = {'threads' : '1', 'tolerance' : '1'}
options_2 = {'threads' : '2', 'timeout': True}

over_params = {}

def overrides_tracker(options, over_params):
    print(options)
    for k, v in options.items():
      if k not in over_params:
        over_params[k] = {}
      if 'count' not in over_params[k]:
        over_params[k]['count'] = 1
      else:
        over_params[k]['count']  = 1
      over_params[k]['value'] = v
        
    return over_params
        

over_params = overrides_tracker(options, over_params)
over_params = overrides_tracker(options_2, over_params)
        
print(over_params)

CodePudding user response:

That happens because you are looking for a dict inside keys that are not existent yet.

Try this instead:

def overrides_tracker(_options, _over_params):
    for key, value in _options.items():
        
        try:
            _over_params[key]['count']  = 1
        except KeyError:
            _over_params[key] = {'count': 0, 'value': None}
        _over_params[key]['value'] = value
        
    return _over_params


def test_over_params():
    options = {'threads' : '1', 'tolerance' : '1'}
    options_2 = {'threads' : '2', 'timeout': True}
    
    over_params = {}

    over_params = overrides_tracker(options, over_params)
    over_params = overrides_tracker(options_2, over_params)
    
    print(over_params)
    
    
if __name__ == '__main__':
    test_over_params()

Results in:

{'threads': {'count': 1, 'value': '2'}, 'tolerance': {'count': 0, 'value': '1'}, 'timeout': {'count': 0, 'value': True}}

Also, i highly recommend using long variable names to avoid confusions

  • Related