-1

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.

samtoddler
  • 8,463
  • 2
  • 26
  • 21
Piduna
  • 609
  • 12
  • 28
  • What is your expected output? – Cute Panda Mar 06 '21 at 21:55
  • I showed `{'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'}` – Piduna Mar 06 '21 at 22:00
  • I have to receive the same, but with additional element in JSON `'dedicatedHostIdentifier': 'h-02d121f9u77d7a123'` – Piduna Mar 06 '21 at 22:00
  • @Piduna length of both dicts varies. `len(blueprint) => 24` and `len(blueprint_value) => 25` and they are being compared one to one. So in one case there is no corresponding key in one of the dict named `blueprint`. Hence the error. – samtoddler Mar 06 '21 at 22:05

1 Answers1

1

use dict.get(key) if the key is not present it will simply return None which is default value and wont error out, so this condition if blueprint_value.get(key) != blueprint.get(key) won't result in KeyError . In case you have default value for dedicatedHostIdentifier you can provide that as well.

>>> x= {}
>>> x.get("ok")
>>> y = x.get("ok")
>>> print(y)
None
>>> y = x.get("ok", "not")
>>> y
'not'
>>> import json
...
... blueprint={'byolOnDedicatedInstance': False, 'disks': [{'iops': 46600, 'name': 'c:0', 'throughput': 0, 'type': 'PROVISIONED_SS
... D'}], 'forceUEFI': False, 'iamRole': '', 'id': '755c7877-0855-4b5b-a777-8e5544e81ea5', 'instanceType': 'COPY_ORIGIN', 'machine
... Id': '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': '', 'staticIpAc
... tion': '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-085
... 5-4b5b-a777-8e5544e81ea5', 'instanceType': 'm5.xlarge', 'machineId': 'b20a601a-84a1-4d77-8bwe-we123456ce4c8', 'networkInterfac
... e': '', 'placementGroup': '', 'privateIPAction': 'CREATE_NEW', 'privateIPs': [], 'publicIPAction': 'DONT_ALLOCATE', 'recommend
... edInstanceType': 'c4.4xlarge', 'recommendedPrivateIP': '192.168.1.1', 'region': '71fbc55-a3a8-4111-45gb-3db3353a53ea', 'runAft
... erLaunch': True, 'securityGroupIDs': ['sg-0aab4ed55ae11f8ec'], 'staticIp': '', 'staticIpAction': 'DONT_CREATE', 'subnetIDs': [
... 'subnet-0c1bawae4656e777d'], 'subnetsHostProject': '', 'tags': [], 'tenancy': 'HOST'}
... print(blueprint_value)
...
... patch = { key: blueprint_value.get(key)
...             for key in blueprint_value.keys()
...             if blueprint_value.get(key) != blueprint.get(key)
...         }
... print(patch)
{'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'}
{'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'}
{'dedicatedHostIdentifier': 'h-02d121f9u77d7a123', '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'}
>>>

Why dict.get(key) instead of dict[key]?

samtoddler
  • 8,463
  • 2
  • 26
  • 21