Trying to grow a nested dictionary by adding more key:value pairs

AKKO

I am facing some trouble with trying to add more key:value pairs to a dictionary object that is itself nested within another dictionary object. Also, the usual way of doing dict[key] = value to assign additional key:value pairs to the dictionary is not suitable for my case here (I'll explain why later below), and thus this makes my objective a lot more challenging to achieve.

I'll illustrate what I'm trying to achieve with some statements from my source code.

First, I have a dictionary object that contains nesting:

environment = { 'v' : 
                  {
                   'SDC_PERIOD':'{period}s'.format(period = self.period),
                   'FAMILY':'{legup_family}s'.format(legup_family = self.legup_family),
                   'DEVICE_FAMILY':'"{fpga_family}s"'.format(fpga_family = self.fpga_family)
                   }
              }

and then following this line, I will do an if test that, if passed, will require me to add this other dictionary:

environment_add = { 'v' : {'LM_LICENSE_FILE' : '1800@adsc-linux'} , 
                    'l' : 'quartus_full' }

to ultimately form this complete dictionary:

environment = { 'v' : 
                  {
                   'SDC_PERIOD':'{period}s'.format(period = self.period),
                   'FAMILY':'{legup_family}s'.format(legup_family = self.legup_family),
                   'DEVICE_FAMILY':'"{fpga_family}s"'.format(fpga_family = self.fpga_family),
                   'LM_LICENSE_FILE' : '1800@adsc-linux'
                   } ,
                'l' : 'quartus_full'
              }

As you can see, if I were to try and assign a new key:value pair using the dict[key] = value syntax, it would not work for me because it would end up either creating an new key:value pair for me, or overwrite the existing dictionary object and the key:value pairs that are nested under the 'v' key.

So far, in order to accomplish the creation of the dictionary, I've been using the following:

environment = """{ v: {'SDC_PERIOD':'%(period)s','FAMILY':'%(legup_family)s','DEVICE_FAMILY':'"%(fpga_family)s"'}}""" % self
if self.require_license:  # this is the if statement test that I was referring to earlier
  environment = environment.replace('}', '')
  environment += """ ,'LM_LICENSE_FILE':'1800@adsc-linux'}, 'l': 'quartus_full'}"""

and then obtaining the dictionary object later with:

import ast
env_dict = ast.literal_eval(environment)

which gives effectively converts the environment string into a dictionary object stored under a new variable name of env_dict.

My teammates think that this is much too overkill, especially since the environment or env_dict object will be parsed in 2 separate modules later on. In the first module, the key-value pairs will be broken up and reconstructed to form strings that look like '-v SDC_PERIOD=2500s, LM_LICENSE_FILE=1800@adsc-linux' , while in the second module, the dictionary nested under the 'v' key (of the environment/env_dict dictionary object) will be extracted out and directly fed as an argument to a function that accepts a mapping object.

So as you can see, there is quite a lot of precise parsing required to do the job, and although my method fulfills the objective, it is not accepted by my team and they think that there must be a better way to do this directly from environment being a dictionary object and not a string object.

Thank you very much for studying my detailed post, and I will greatly appreciate any help or suggestions to move forward on this!

John Zwinck
for k,v in environment_add.iteritems(): # .items() in Python 3
    if k in environment:
        environment[k].update(v)
    else:
        environment[k] = v

That is, for each item to add, check if it exists, and update it if so, or simply create it. This assumes the items being added, if they exist, will be dicts (you can't update a string like quartus_full).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Trying to Sum Key Value Pairs in a Dictionary

Flatten nested dictionary to key/value pairs with Ansible

Producing key:value pairs from existing pairs in nested dictionary

Nested dictionary with key: list[key:value] pairs to dataframe

Adding multiple key value pairs to a python dictionary and then checking for duplicate values

Get Only Nested Key-Value Pairs From Dictionary?

Search for key, value pairs in complex nested dictionary/list

Getting common key value pairs from nested dictionary

Adding more than one value to a Key in a Dictionary with Tuple Values

ansible dictionary key value pairs

Adding new (key,value) pair in nested dictionary - python3

Appending list of key:value pairs without adding more curly braces in Python

Trying to print nested dictionary value

how can I create nested dictionary keys and assign them values from a list of namespaced key value pairs?

Why does Python(3.7) keep overriding key:value pairs in my nested dictionary?

loop through nested dictionary/list in Python and save key-value pairs

Nested dictionary incorrectly populating all top level key-value pairs with same values

Adding a Key/Value to a Dictionary in Redux

Loop Through Two Dataframe Columns to Create Key-Value Pairs For Value Dictionaries Nested Inside A Dictionary Nested Inside Another Dictonary

Using a Volt::Model as a dictionary of Key/Value pairs

passing dictionary to view ,with multiple key:value pairs

Sort dictionary of lists by key value pairs

Convert string with alternating key value pairs to a dictionary

Swapping key-value pairs in a dictionary

Updating dictionary key/value pairs as dataframe changes

Printing dictionary key value pairs in c#

Searching and counting dictionary key value pairs

key/value pairs by name in Dictionary in Swift

Swift Dictionary Multiple Key Value Pairs - Iteration