Parsing Minecraft recipes in one line 🤪

There is a sacred ritual that every programmer must go through at least once in their life: writing an absolutely unreadable but working one-liner with complex logic inside. Not because you have to, but because you can.

A guy from Reddit wanted to parse recipes from vanilla Minecraft, and here's what came out:

out2in = {k: ([("#" + x["tag"] if x.get("tag") else x["item"]) if x.class.name == "dict" else [("#" + y["tag"] if y.get("tag") else y["item"]) for y in x] for x in v]) for (k, v) in {(y["result"]["item"] if y["result"].class.name == "dict" else y["result"]): ((y["ingredients"] if y["ingredients"].class.name == "list" else ([y["ingredients"]]) if y.get("ingredients") else y["ingredient"]) if y.get("ingredients") else [z for z in y["key"].values()]) for y in filter(lambda x: x.get("result") and (x.get("ingredients") or x.get("key")), [json.load(open(x, "rt")) for x in Path("recipes").glob("*.json")])}.items()}


What do we see here? The Zen of Python, which went out for a smoke and never came back. In one bottle, we have mixed:
- Nested dict comprehensions
- Nested list comprehensions
- Ternary operators as the main logic
- filter with a lambda function
- And all this for parsing JSON of varying structure.

The author honestly admitted: "this took ages to debug...". And he did it, quoting: "just because i can(and am bored)".

A work of art that should hang in a frame with the caption "NEVER DO THIS".

#code_smoker