HaibaneReki
HaibaneReki

Reputation: 63

Python: Dynamically update a dictionary with varying variable "depth"

I have a dictionary with various variable types - from simple strings to other nested dictionaries several levels deep. I need to create a pointer to a specific key:value pair so it can be used in a function that would update the dictionary and could be called like so:

dict_update(my_dictionary, value, level1key, *level2key....)

Data coming from a web request like this:

data {
    'edited-fields': ['level1key-level2key-level3key', 'level1key-level2key-listindex', 'level1key'], 
    'level1key-level2key-level3key': 'value1', 
    'level1key-level2key-listindex': 'value2',
    'level1key': 'value3' 
}

I can get to the original value to read it like this:

for field in data["edited-fields"]:
    args = field.split("-")
    value = my_dictionary
    for arg in args:
        if arg.isdigit():
            arg = int(arg)
        value = value[arg]
    print(value)

But have no idea how to edit it using the same logic. I can't search and replace by the value itself as there can be duplicates and having several if statements for each possible arg count doesn't feel very pythonic.

EXAMPLE:

data {
    'edited-fields': ['mail-signatures-work', 'mail-signatures-personal', 'mail-outofoffice', 'todo-pending-0'], 
    'mail-signatures-work': 'I'm Batman', 
    'mail-signatures-personal': 'Bruce, Wayne corp.',
    'mail-outofoffice': 'false',
    'todo-pending-0': 'Call Martha'
}

I'd like to process that request like this:

for field in data['edited-fields']:
    update_batman_db(field, data[field])

def update_batman_db(key-to-parse, value):
    # how-to?
    # key-to-parse ('mail-signatures-work') -> batman_db pointer ["mail"]["signatures"]["work"] 
    # etc:
    batman_db["mail"]["signatures"]["work"] = value
    batman_db["mail"]["outofoffice"] = value # one less level
    batman_db["todo"]["pending"][0] = value # list index

Upvotes: 1

Views: 1755

Answers (1)

Serge Ballesta
Serge Ballesta

Reputation: 148890

The hard part here is to know whether an index must be used as a string form a mapping of as an integer for a list.

I will first try to process it as an integer index on a list, and revert to a string index of a mapping in case of any exception:

def update_batman_db(key, value):
    keys = key.split('-')  # parse the received key
    ix = batman_db         # initialize a "pointer" to the top most item
    for key in keys[:-1]:  # process up to the last key item
        try:               #  descending in the structure
            i = int(key)
            ix = ix[i]
        except:
            ix = ix[key]
    try:                   # assign the value with last key item
        i = int(keys[-1])
        ix[i] = value
    except:
        ix[keys[-1]] = value

Upvotes: 1

Related Questions