Skip to main content
3 of 3
edited title
JThao
  • 57
  • 1
  • 9

What is the pythonic way to update my nested dictionary?

So I need to update my nested dictionary where the key is "compensationsDeltaEmployee". I created a string cost_perhour to hold the value of my conditional statements. Now that I am done with the string. What is the best or pythonic way to update my nested dictionary with a new key "costPerHour" with the value of my string cost_perhour? What I did was I created an empty dictionary cost_per_hour_dic then add the string then ran update. Is this okay or can I clean it up more?

def add_cost_per_hour(json_dic):
    """
    Add new dictionary value costPerHour to our data
    """
    
    cost_perhour = ""
    cost_per_hour_dic = {}
    try:
        # Find key compensationsDeltaEmployee.
        for keys, values in json_dic.items():
            if str(keys) == "compensationsDeltaEmployee":
                if "payBasis" in values:
                    # If payBasis equal 9, 0, P, cost_per_hour field should be blank.
                    if str(values["payBasis"]) in ("9", "0", "P"):
                        cost_perhour = ""

                    # If payBasis equal 1, A, B, C, D, H, J, 3, cost_per_hour equals salaryPayable divide by 2080.
                    elif str(values["payBasis"]) in ("1", "A", "B", "C", "D", "H", "J", "3"):
                        # Check if our value for salaryPayable is empty or None
                        if values["salaryPayable"] == "" or values["salaryPayable"] is None:
                            raise Exception("salaryPayable field is empty")
                        else:
                            cost_perhour = round(float(values["salaryPayable"]) / 2080, 2)

                    # If payBasis equal 2, 4, 5, 7, E, F, X, cost_per_hour should match the salaryPayable field.
                    elif str(values["payBasis"]) in ("2", "4", "5", "7", "E", "F", "X"):
                        if values["salaryPayable"] == "" or values["salaryPayable"] is None:
                            raise Exception("salaryPayable field is empty")
                        else:
                            cost_perhour = round(float(values["salaryPayable"]), 2)

                    # If there are any unexpected values, the cost_per_hour field should be blank.
                    else:
                        cost_perhour = ""
                else:
                    raise Exception("Could not find key payBasis")
        if cost_perhour is "":
            raise Exception("cost_per_hour is empty")

        cost_per_hour_dic["costPerHour"] = str(cost_perhour)
        json_dic["compensationsDeltaEmployee"].update(cost_per_hour_dic)

        return json_dic

    except Exception as e:
        print("Exception in add_cost_per_hour: ", e)

The json_dict

"compensationsDeltaEmployee": {
    "interPersonnelAgree": "N",
    "payRateDeterminant": "0",
    "payPlan": "1",
    "properPayPlan": null,
    "retainedGrade": null,
    "payBasis": "2",
    "gradeCode": "06",
    "step": "03",
    "basePayChangeYypp": null,
    "physicalCompAllowance": 0,
    "withinGradeEligibilityCode": "1",
    "salaryPayable": 21
    },
JThao
  • 57
  • 1
  • 9