I have simple python script.
When I am making this, everything with my dictionary OK
:
import json
blueprint={'byolOnDedicatedInstance': False, 'disks': [{'iops': 46600, 'name': 'c:0', 'throughput': 0, 'type': 'PROVISIONED_SSD'}], 'forceUEFI': False, 'iamRole': '', 'id': '755c7877-0855-4b5b-a777-8e5544e81ea5', 'instanceType': 'COPY_ORIGIN', 'machineId': 'b20a601a-84a1-4d77-8bwe-we123456ce4c8', 'networkInterface': '', 'placementGroup': '', 'privateIPAction': 'COPY_ORIGIN', 'privateIPs': [], 'publicIPAction': 'ALLOCATE', 'recommendedInstanceType': 'c4.4xlarge', 'recommendedPrivateIP': '192.168.1.1', 'region': '71fbc55-a3a8-4111-45gb-3db3353a53ea', 'runAfterLaunch': True, 'securityGroupIDs': [], 'staticIp': '', 'staticIpAction': 'DONT_CREATE', 'subnetIDs': [], 'subnetsHostProject': '', 'tags': [], 'tenancy': 'SHARED', 'myinstancetype': 'None'}
print(blueprint)
blueprint_value={'byolOnDedicatedInstance': False, 'myinstancetype': 'None', 'disks': [{'iops': 0, 'name': 'c:0', 'throughput': 0, 'type': 'SSD'}], 'forceUEFI': False, 'iamRole': '', 'id': '755c7877-0855-4b5b-a777-8e5544e81ea5', 'instanceType': 'm5.xlarge', 'machineId': 'b20a601a-84a1-4d77-8bwe-we123456ce4c8', 'networkInterface': '', 'placementGroup': '', 'privateIPAction': 'CREATE_NEW', 'privateIPs': [], 'publicIPAction': 'DONT_ALLOCATE', 'recommendedInstanceType': 'c4.4xlarge', 'recommendedPrivateIP': '192.168.1.1', 'region': '71fbc55-a3a8-4111-45gb-3db3353a53ea', 'runAfterLaunch': True, 'securityGroupIDs': ['sg-0aab4ed55ae11f8ec'], 'staticIp': '', 'staticIpAction': 'DONT_CREATE', 'subnetIDs': ['subnet-0c1bawae4656e777d'], 'subnetsHostProject': '', 'tags': [], 'tenancy': 'HOST'}
print(blueprint_value)
patch = { key: blueprint_value[key]
for key in blueprint_value.keys()
if blueprint_value[key] != blueprint[key]
}
print(patch)
Everything is OK, I am receiving needed text:
{'disks': [{'iops': 0, 'name': 'c:0', 'throughput': 0, 'type': 'SSD'}], 'instanceType': 'm5.xlarge', 'privateIPAction': 'CREATE_NEW', 'publicIPAction': 'DONT_ALLOCATE', 'securityGroupIDs': ['sg-0aab4ed55ae11f8ec'], 'subnetIDs': ['subnet-0c1bawae4656e777d'], 'tenancy': 'HOST'}
When, I am adding to second value (blueprint_value
) additional element ('dedicatedHostIdentifier': 'h-02d121f9u77d7a123'
), I am receiving error:
Traceback (most recent call last):
File "/home/test/test/1.py", line 8, in <module>
patch = { key: blueprint_value[key]
File "/home/test/test/1.py", line 10, in <dictcomp>
if blueprint_value[key] != blueprint[key]
KeyError: 'dedicatedHostIdentifier'
Full text of not working version:
import json
blueprint={'byolOnDedicatedInstance': False, 'disks': [{'iops': 46600, 'name': 'c:0', 'throughput': 0, 'type': 'PROVISIONED_SSD'}], 'forceUEFI': False, 'iamRole': '', 'id': '755c7877-0855-4b5b-a777-8e5544e81ea5', 'instanceType': 'COPY_ORIGIN', 'machineId': 'b20a601a-84a1-4d77-8bwe-we123456ce4c8', 'networkInterface': '', 'placementGroup': '', 'privateIPAction': 'COPY_ORIGIN', 'privateIPs': [], 'publicIPAction': 'ALLOCATE', 'recommendedInstanceType': 'c4.4xlarge', 'recommendedPrivateIP': '192.168.1.1', 'region': '71fbc55-a3a8-4111-45gb-3db3353a53ea', 'runAfterLaunch': True, 'securityGroupIDs': [], 'staticIp': '', 'staticIpAction': 'DONT_CREATE', 'subnetIDs': [], 'subnetsHostProject': '', 'tags': [], 'tenancy': 'SHARED', 'myinstancetype': 'None'}
print(blueprint)
blueprint_value={'byolOnDedicatedInstance': False, 'myinstancetype': 'None', 'dedicatedHostIdentifier': 'h-02d121f9u77d7a123', 'disks': [{'iops': 0, 'name': 'c:0', 'throughput': 0, 'type': 'SSD'}], 'forceUEFI': False, 'iamRole': '', 'id': '755c7877-0855-4b5b-a777-8e5544e81ea5', 'instanceType': 'm5.xlarge', 'machineId': 'b20a601a-84a1-4d77-8bwe-we123456ce4c8', 'networkInterface': '', 'placementGroup': '', 'privateIPAction': 'CREATE_NEW', 'privateIPs': [], 'publicIPAction': 'DONT_ALLOCATE', 'recommendedInstanceType': 'c4.4xlarge', 'recommendedPrivateIP': '192.168.1.1', 'region': '71fbc55-a3a8-4111-45gb-3db3353a53ea', 'runAfterLaunch': True, 'securityGroupIDs': ['sg-0aab4ed55ae11f8ec'], 'staticIp': '', 'staticIpAction': 'DONT_CREATE', 'subnetIDs': ['subnet-0c1bawae4656e777d'], 'subnetsHostProject': '', 'tags': [], 'tenancy': 'HOST'}
print(blueprint_value)
patch = { key: blueprint_value[key]
for key in blueprint_value.keys()
if blueprint_value[key] != blueprint[key]
}
print(patch)
Main idea of script to compare two JSON-s and return elements, that presented in blueprint_value
and in blueprint
variables.