{
"$type": "site.standard.document",
"bskyPostRef": {
"cid": "bafyreiecovfvuhcgrk2pohixhm7hlbld6od5aet37y6snymw4nus7dwqwe",
"uri": "at://did:plc:vyjlfm46mfv6u4vjp6qtrfx2/app.bsky.feed.post/3mmf24h7ovvu2"
},
"coverImage": {
"$type": "blob",
"ref": {
"$link": "bafkreidg2k33tiuupnstmiowh7hyyvtsbe3gyhfygiytrzynysnhoctrxi"
},
"mimeType": "image/jpeg",
"size": 81260
},
"path": "/articles/in-the-know",
"publishedAt": "2026-05-21T06:30:00.000Z",
"site": "https://thedailywtf.com",
"tags": [
"CodeSOD",
"Download Free Guide Now!"
],
"textContent": "**Delilah** works in a Python shop. Despite Python's \"batteries included\" design, that doesn't stop people from trying to make their own batteries from potatoes. For example, her co-worker wrote this function:\n\n\n def key_exists(element, key):\n if isinstance(element, dict):\n try:\n element = element[key]\n except KeyError:\n return False\n return True\n\n\nPython, of course, has an `in` operator. `key in dictionary` is an _extremely_ common idiom. There's no reason to implement your own. Certainly, there's no reason to re-implement it by catching and throwing exceptions.\n\nThis is ugly, stupid, and bad. It gets worse, though, when you see how it gets used.\n\n\n for key in old_yaml_data:\n if key in new_yaml_data:\n if old_yaml_data[key] != new_yaml_data[key]:\n temp = new_yaml_data[key]\n new_yaml_data[key] = merge(new_yaml_data[key], old_yaml_data[key])\n\n if key_exists(new_yaml_data[key], 'image') and key_exists(old_yaml_data[key], 'image'):\n new_yaml_data[key]['image'] = temp['image']\n elif key == \"databases\":\n revert_db_tags(new_yaml_data[key], temp)\n\n\nThis code is attempting to upgrade \"old\" YAML data with \"new\" data. So it's basically merging dictionaries, which is a great case for the `in` operator.\n\nAnd _they use the correct idiom_ on the second line there! This was written by one developer! They do the standard `key in new_yaml_data` check. And they _also_ use `key_exists`. I can only assume that they had a stroke between starting and finishing this script, which I'll note is, in total, 48 lines long.\n\nHere's the whole short script, which is just generally a _mess_. Slapped together Python code that's trying to be a \"smarter\" shell script, but is definitely written with the elegance of hacked-together-bash.\n\n\n import sys\n import yaml\n from jsonmerge import merge\n\n appHomePath = sys.argv[1]\n oldValuesYAML = appHomePath + \"values.yaml\"\n newValuesYAML = appHomePath + \"/upgrade_version/values.yaml\"\n with open(newValuesYAML, 'r') as f:\n new_yaml_data = yaml.load(f, Loader=yaml.loader.FullLoader)\n with open(oldValuesYAML, 'r') as f:\n old_yaml_data = yaml.load(f, Loader=yaml.loader.FullLoader)\n def key_exists(element, key):\n if isinstance(element, dict):\n try:\n element = element[key]\n except KeyError:\n return False\n return True\n\n def revert_db_tags(old_yaml_data, new_yaml_data):\n dbList = [\"mongoDB\", \"postgresDB\"]\n mongoDbTagsToRevert = [\"mongoRestore\"]\n mongodbKeysToDelete = []\n postgresDbTagsToRevert = []\n\n\n for db in dbList:\n old_yaml_data[db]['image'] = new_yaml_data[db]['image']\n for mongoDbTag in mongoDbTagsToRevert:\n old_yaml_data['mongoDB'][mongoDbTag]['image'] = new_yaml_data['mongoDB'][mongoDbTag]['image']\n for mongoDbTag in mongoKeysToDelete:\n del old_yaml_data['mongoDB'][mongoDbTag]\n\n for postgresDbTag in postgresDbTagsToRevert:\n old_yaml_data['postgresDB'][postgresDbTag]['image'] = new_yaml_data['postgresDB'][postgresDbTag]['image']\n\n for key in old_yaml_data:\n if key in new_yaml_data:\n if old_yaml_data[key] != new_yaml_data[key]:\n temp = new_yaml_data[key]\n new_yaml_data[key] = merge(new_yaml_data[key], old_yaml_data[key])\n\n if key_exists(new_yaml_data[key], 'image') and key_exists(old_yaml_data[key], 'image'):\n new_yaml_data[key]['image'] = temp['image']\n elif key == \"databases\":\n revert_db_tags(new_yaml_data[key], temp)\n\n with open(newValuesYAML, 'w') as f:\n data = yaml.dump(new_yaml_data, f, sort_keys=False)\n\n\n[Advertisement] **Plan Your .NET 9 Migration with Confidence**\nYour journey to .NET 9 is more than just one decision.Avoid migration migraines with the advice in this free guide. **Download Free Guide Now!**",
"title": "CodeSOD: In the Know"
}