sagr
sagr

Reputation: 31

A more elegant way to write dictionary assignments

Because of the use of nested dictionaries, the key will be longer, so it seems redundant and inefficient. I want to ask if there is a more elegant, high-performance implementation.

Nested dictionaries

group = {'_meta': {'hostvars': {}}, 'all': []}

for i in range(length):
    ah = value[i]
    group["_meta"]["hostvars"][ah["service_deploy_name"] + ah["env"] + ah["ip"]] = {}
    group["all"].append(ah["service_deploy_name"] + ah["env"] + ah["ip"])

    group["_meta"]["hostvars"][ah["service_deploy_name"] + ah["env"] + ah["ip"]]["ansible_host"] = ah["ip"]
    group["_meta"]["hostvars"][ah["service_deploy_name"] + ah["env"] + ah["ip"]]["project_name"] = ah["project"]
    group["_meta"]["hostvars"][ah["service_deploy_name"] + ah["env"] + ah["ip"]]["type_env"] = ah["env"]

    if i > 0:
        if value[i]["ip"] == value[i - 1]["ip"]:
            if value[i]["type"] == "thrift":
                group["_meta"]["hostvars"][ah["service_deploy_name"] + ah["env"] + ah["ip"]]["delay_time"] = "5"
            else:
                group["_meta"]["hostvars"][ah["service_deploy_name"] + ah["env"] + ah["ip"]]["delay_time"] = "20"
        else:
            group["_meta"]["hostvars"][ah["service_deploy_name"] + ah["env"] + ah["ip"]]["delay_time"] = "0"
    else:
        group["_meta"]["hostvars"][ah["service_deploy_name"] + ah["env"] + ah["ip"]]["delay_time"] = "0"

Upvotes: 0

Views: 46

Answers (2)

dlask
dlask

Reputation: 9002

Use a temporary variable so the original code

group["_meta"]["hostvars"][ah["service_deploy_name"] + ah["env"] + ah["ip"]]["ansible_host"] = ah["ip"]
group["_meta"]["hostvars"][ah["service_deploy_name"] + ah["env"] + ah["ip"]]["project_name"] = ah["project"]
group["_meta"]["hostvars"][ah["service_deploy_name"] + ah["env"] + ah["ip"]]["type_env"] = ah["env"]

becomes

t = group["_meta"]["hostvars"][ah["service_deploy_name"] + ah["env"] + ah["ip"]]
t["ansible_host"] = ah["ip"]
t["project_name"] = ah["project"]
t["type_env"] = ah["env"]

Upvotes: 1

xkcdjerry
xkcdjerry

Reputation: 983

You can pull the sames out,thus making it shorter and more readable.
And because dictionaries are mutable,the changes you make to the slice alse changes the main dictionary:

group = {'_meta': {'hostvars': {}}, 'all': []}

for i in range(length):
    ah = value[i]
    key=ah["service_deploy_name"] + ah["env"] + ah["ip"]
    sliced=group["_meta"]["hostvars"][key]
    group["_meta"]["hostvars"][key] = {}
    group["all"].append(key)
    sliced["ansible_host"] = ah["ip"]
    sliced["project_name"] = ah["project"]
    sliced["type_env"] = ah["env"]

    if i > 0:
        if value[i]["ip"] == value[i - 1]["ip"]:
            if value[i]["type"] == "thrift":
                sliced["delay_time"] = "5"
            else:
                sliced["delay_time"] = "20"
        else:
            sliced["delay_time"] = "0"
    else:
        sliced["delay_time"] = "0"

Upvotes: 2

Related Questions