Home > Enterprise >  Not finding element in a list causing program to crush
Not finding element in a list causing program to crush

Time:03-31

I have .json file, but because its too big I will only paste part of it:

[
{
    "name": {
        "common": "Aruba",
        "official": "Aruba",
        "native": {
            "nld": {
                "official": "Aruba",
                "common": "Aruba"
            },
            "pap": {
                "official": "Aruba",
                "common": "Aruba"
            }
        }
    },
    "tld": [
        ".aw"
    ],
    "cca2": "AW",
    "ccn3": "533",
    "cca3": "ABW",
    "cioc": "ARU",
    "independent": false,
    "status": "officially-assigned",
    "unMember": false,
    "currencies": {
        "AWG": {
            "name": "Aruban florin",
            "symbol": "\u0192"
        }
    },
    "idd": {
        "root": " 2",
        "suffixes": [
            "97"
        ]
    },
    "capital": [
        "Oranjestad"
    ],
    "altSpellings": [
        "AW"
    ],
    "region": "Americas",
    "subregion": "Caribbean",
    "languages": {
        "nld": "Dutch",
        "pap": "Papiamento"
    },
    "translations": {
        "ces": {
            "official": "Aruba",
            "common": "Aruba"
        },
        "deu": {
            "official": "Aruba",
            "common": "Aruba"
        },
        "est": {
            "official": "Aruba",
            "common": "Aruba"
        },
        "fin": {
            "official": "Aruba",
            "common": "Aruba"
        },
        "fra": {
            "official": "Aruba",
            "common": "Aruba"
        },
        "hrv": {
            "official": "Aruba",
            "common": "Aruba"
        },
        "hun": {
            "official": "Aruba",
            "common": "Aruba"
        },
        "ita": {
            "official": "Aruba",
            "common": "Aruba"
        },
        "jpn": {
            "official": "\u30a2\u30eb\u30d0",
            "common": "\u30a2\u30eb\u30d0"
        },
        "kor": {
            "official": "\uc544\ub8e8\ubc14",
            "common": "\uc544\ub8e8\ubc14"
        },
        "nld": {
            "official": "Aruba",
            "common": "Aruba"
        },
        "per": {
            "official": "\u0622\u0631\u0648\u0628\u0627",
            "common": "\u0622\u0631\u0648\u0628\u0627"
        },
        "pol": {
            "official": "Aruba",
            "common": "Aruba"
        },
        "por": {
            "official": "Aruba",
            "common": "Aruba"
        },
        "rus": {
            "official": "\u0410\u0440\u0443\u0431\u0430",
            "common": "\u0410\u0440\u0443\u0431\u0430"
        },
        "slk": {
            "official": "Aruba",
            "common": "Aruba"
        },
        "spa": {
            "official": "Aruba",
            "common": "Aruba"
        },
        "swe": {
            "official": "Aruba",
            "common": "Aruba"
        },
        "urd": {
            "official": "\u0627\u0631\u0648\u0628\u0627",
            "common": "\u0627\u0631\u0648\u0628\u0627"
        },
        "zho": {
            "official": "\u963f\u9c81\u5df4",
            "common": "\u963f\u9c81\u5df4"
        }
    },
    "latlng": [
        12.5,
        -69.96666666
    ],
    "landlocked": false,
    "borders": [],
    "area": 180,
    "flag": "\ud83c\udde6\ud83c\uddfc",
    "demonyms": {
        "eng": {
            "f": "Aruban",
            "m": "Aruban"
        },
        "fra": {
            "f": "Arubaise",
            "m": "Arubais"
        }
    }
},
{
    "name": {
        "common": "Afghanistan",
        "official": "Islamic Republic of Afghanistan",
        "native": {
            "prs": {
                "official": "\u062c\u0645\u0647\u0648\u0631\u06cc \u0627\u0633\u0644\u0627\u0645\u06cc \u0627\u0641\u063a\u0627\u0646\u0633\u062a\u0627\u0646",
                "common": "\u0627\u0641\u063a\u0627\u0646\u0633\u062a\u0627\u0646"
            },
            "pus": {
                "official": "\u062f \u0627\u0641\u063a\u0627\u0646\u0633\u062a\u0627\u0646 \u0627\u0633\u0644\u0627\u0645\u064a \u062c\u0645\u0647\u0648\u0631\u06cc\u062a",
                "common": "\u0627\u0641\u063a\u0627\u0646\u0633\u062a\u0627\u0646"
            },
            "tuk": {
                "official": "Owganystan Yslam Respublikasy",
                "common": "Owganystan"
            }
        }
    },
    "tld": [
        ".af"
    ],
    "cca2": "AF",
    "ccn3": "004",
    "cca3": "AFG",
    "cioc": "AFG",
    "independent": true,
    "status": "officially-assigned",
    "unMember": true,
    "currencies": {
        "AFN": {
            "name": "Afghan afghani",
            "symbol": "\u060b"
        }
    },
    "idd": {
        "root": " 9",
        "suffixes": [
            "3"
        ]
    },
    "capital": [
        "Kabul"
    ],
    "altSpellings": [
        "AF",
        "Af\u0121\u0101nist\u0101n"
    ],
    "region": "Asia",
    "subregion": "Southern Asia",
    "languages": {
        "prs": "Dari",
        "pus": "Pashto",
        "tuk": "Turkmen"
    },
    "translations": {
        "ces": {
            "official": "Afgh\u00e1nsk\u00e1 isl\u00e1msk\u00e1 republika",
            "common": "Afgh\u00e1nist\u00e1n"
        },
        "cym": {
            "official": "Gweriniaeth Islamaidd Affganistan",
            "common": "Affganistan"
        },
        "deu": {
            "official": "Islamische Republik Afghanistan",
            "common": "Afghanistan"
        },
        "est": {
            "official": "Afganistani Islamivabariik",
            "common": "Afganistan"
        },
        "fin": {
            "official": "Afganistanin islamilainen tasavalta",
            "common": "Afganistan"
        },
        "fra": {
            "official": "R\u00e9publique islamique d'Afghanistan",
            "common": "Afghanistan"
        },
        "hrv": {
            "official": "Islamska Republika Afganistan",
            "common": "Afganistan"
        },
        "hun": {
            "official": "Afganiszt\u00e1ni Iszl\u00e1m K\u00f6zt\u00e1rsas\u00e1g",
            "common": "Afganiszt\u00e1n"
        },
        "ita": {
            "official": "Repubblica islamica dell'Afghanistan",
            "common": "Afghanistan"
        },
        "jpn": {
            "official": "\u30a2\u30d5\u30ac\u30cb\u30b9\u30bf\u30f3\u00b7\u30a4\u30b9\u30e9\u30e0\u5171\u548c\u56fd",
            "common": "\u30a2\u30d5\u30ac\u30cb\u30b9\u30bf\u30f3"
        },
        "kor": {
            "official": "\uc544\ud504\uac00\ub2c8\uc2a4\ud0c4 \uc774\uc2ac\ub78c \uacf5\ud654\uad6d",
            "common": "\uc544\ud504\uac00\ub2c8\uc2a4\ud0c4"
        },
        "nld": {
            "official": "Islamitische Republiek Afghanistan",
            "common": "Afghanistan"
        },
        "per": {
            "official": "\u062c\u0645\u0647\u0648\u0631\u06cc \u0627\u0633\u0644\u0627\u0645\u06cc \u0627\u0641\u063a\u0627\u0646\u0633\u062a\u0627\u0646",
            "common": "\u0627\u0641\u063a\u0627\u0646\u0633\u062a\u0627\u0646"
        },
        "pol": {
            "official": "Islamska Republika Afganistanu",
            "common": "Afganistan"
        },
        "por": {
            "official": "Rep\u00fablica Isl\u00e2mica do Afeganist\u00e3o",
            "common": "Afeganist\u00e3o"
        },
        "rus": {
            "official": "\u0418\u0441\u043b\u0430\u043c\u0441\u043a\u0430\u044f \u0420\u0435\u0441\u043f\u0443\u0431\u043b\u0438\u043a\u0430 \u0410\u0444\u0433\u0430\u043d\u0438\u0441\u0442\u0430\u043d",
            "common": "\u0410\u0444\u0433\u0430\u043d\u0438\u0441\u0442\u0430\u043d"
        },
        "slk": {
            "official": "Afg\u00e1nsky islamsk\u00fd \u0161t\u00e1t",
            "common": "Afganistan"
        },
        "spa": {
            "official": "Rep\u00fablica Isl\u00e1mica de Afganist\u00e1n",
            "common": "Afganist\u00e1n"
        },
        "swe": {
            "official": "Islamiska republiken Afghanistan",
            "common": "Afghanistan"
        },
        "urd": {
            "official": "\u0627\u0633\u0644\u0627\u0645\u06cc \u062c\u0645\u06c1\u0648\u0631\u06cc\u06c1 \u0627\u0641\u063a\u0627\u0646\u0633\u062a\u0627\u0646",
            "common": "\u0627\u0641\u063a\u0627\u0646\u0633\u062a\u0627\u0646"
        },
        "zho": {
            "official": "\u963f\u5bcc\u6c57\u4f0a\u65af\u5170\u5171\u548c\u56fd",
            "common": "\u963f\u5bcc\u6c57"
        }
    },
    "latlng": [
        33,
        65
    ],
    "landlocked": true,
    "borders": [
        "IRN",
        "PAK",
        "TKM",
        "UZB",
        "TJK",
        "CHN"
    ],
    "area": 652230,
    "flag": "\ud83c\udde6\ud83c\uddeb",
    "demonyms": {
        "eng": {
            "f": "Afghan",
            "m": "Afghan"
        },
        "fra": {
            "f": "Afghane",
            "m": "Afghan"
        }
    }
}]

Then I have a following python code to open and load this .json file. The idea is that when user pass a parameter via terminal or command line- program should check it and if its equal to one of the keys- it will print all the official country names under that key. By keys I mean ces, deu,cym, which are attributes of translation. Here is the code I wrote:

import json
import sys
from sys import argv
import json

#access .json file and load it's insides
with open('countries.json') as f:
countries = json.load(f)

#create a list of translation keys from .json file
official_keys = {}
for c in countries:
  official_keys[c['name']['official']] = c['translations']

if len(sys.argv) == 1 or len(sys.argv) > 2:
   print("Incorrect parameter")
   sys.exit()
else:
   key = argv[1]

#return countries names if CLI pAarameter matches one of the keys
for official, keys  in official_keys.items():
  if (key in keys):
      print(keys[key]["official"])
  else : 
      print ("Translation key is not supported")
      break

It is working fine for keys that are appear in every translation, but as you can see, cym key is not appearing in the first translations, that's why when I execute python3 main.py cym it returns "Translation key is not supported". I wrote the same project in Javascript before and it was easier there by using Optional Chaining. It would allow me to return Translation key is not supported for the first country, but instead of crushing, program would go search and return those elements that actually appear. This is the part of Javascript code:

try {
for (const {translations} of data) {
  const key = translations[enteredKey]?.official;
  if (key) {
    console.log(key);
  }
}
} catch (error) {
console.log('Cannot translate variable');
}

Can anyone help me modify this python code not to crush after not finding key element in one of the translations?

CodePudding user response:

Instead of replacing the translations dictionary each time, merge them using the update() method.

official_keys = {}
for c in countries:
    official_keys.setdefault(c['name']['official'], {}).update(c['translations'])
  • Related