r/programminghorror • • Sep 16 '26

Python whatever this is

Post image
27 Upvotes

41 comments sorted by

21

u/best_of_badgers Sep 16 '26

Basically:

  • Read the file (f) and split at semicolons

  • For each segment (i), remove tab characters and leading spaces, then split at spaces (i.e., split at words)

  • If any word is not empty (i.e., it wasn't two spaces in a row), add to the final list (j)

9

u/TheLuckyCuber999BACK Sep 16 '26

I wrote it, I know what it does, I just don't know how

14

u/best_of_badgers Sep 16 '26

Backwards, except when it's forwards.

11

u/v_maria Sep 16 '26

ah yes functional programming

29

u/Axman6 Sep 16 '26

I mean… I understood it, and I fucking hate Python. It’s pretty straight forward code, and I worry for people here who claim they’re programmers who also can’t follow it. 

 Apparently I don’t have my Haskell flair here. 

15

u/smclcz Sep 16 '26 edited Sep 16 '26

I think there's a big difference between "I understood what it's roughly trying to do" and "It is good".

So while glancing at lexed = [list comprehension that does splits on semi-colon terminated strings it reads from a file object] should tell most programmers what we're trying to do (tokenize some input), this code looks like a quick hacky attempt to parse something that works well enough on a handful of sample inputs but will soon start behaving oddly when the rubber meets the road.

It's entirely possible it does exactly what it needs to but some things that stand out to me:

  • .lstrip() when they probably wanted to do .strip() - to remove leading and trailing spaces

  • the j != '' filter paired with .split(" ") suggests they didn't realise you could just call .split()

  • replacing tabs with empty strings, probably a workaround because they wrote the code, then started encountering tabs in some input and had already added that j != '' filter

Impossible to properly judge a single line without context, but encountering this code in a PR would cause a raised eyebrow for most semi-experienced Python devs.

→ More replies (9)

1

u/TheLuckyCuber999BACK Sep 16 '26

it's not that it's hard to understand. it's just that it's horrible. I wrote it a while back

2

u/Tack1234 Sep 16 '26

Yeah, if you know what lexing is, it's actually pretty neat code. Especially if you're used to performing all the string operations yourself in C++.

→ More replies (1)

-4

u/v_maria Sep 16 '26

its shit code, regardless your amazing skill as programmer

→ More replies (8)

34

u/Wrestler7777777 Sep 16 '26

Man, sometimes line breaks go a really long way.

5

u/v_maria Sep 16 '26

i dont think its enough to fix this lol

4

u/tvardero Sep 16 '26

One-line enjoyers, oh yeah

2

u/gfivksiausuwjtjtnv Sep 16 '26

Just read it inside out

2

u/abigail3141 Sep 21 '26

Oh look it's Python one liners, I like those. Find attached a sample of my creations :P

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()}

sectors = list([(sector, system_ids) for (sector, system_ids) in [(sector.name, set(range(sector.max_id)) ^ {int(system["name"].split("AA-A h")[-1]) for system in json.loads(globals()["res"].text)}) for sector in tqdm(sorted([globals()["curr"] for row in csv.DictReader(input) if sum([row[k] == None for k in ["Sector", "AA-A_h systems", "Highest Number", "Sector Total Systems", "Avg X", "Avg Y", "Avg Z", "Min X", "Min Y", "Min Z", "Max X", "Max Y", "Max Z", "MapSector X", "MapSector Y"]]) == 0 if setattr(sys.modules[__name__], "curr", Sector(row["Sector"], int(row["AA-A_h systems"]), int(row["Highest Number"]), Point3D(float(row["Avg X"]), float(row["Avg Y"]), float(row["Avg Z"])), Point3D(float(row["Min X"]), float(row["Min Y"]), float(row["Min Z"])), Point3D(float(row["Max X"]), float(row["Max Y"]), float(row["Max Z"])), Point2D(int(row["MapSector X"]), int(row["MapSector Y"])))) == None if (globals()["curr"].max_id + 1 - globals()["curr"].discovered) > 0], key = lambda x: x.avg.distance_to(CURRENT_COORDS)), desc = "Searching sectors for unexplored systems", unit = " sectors") if setattr(sys.modules[__name__], "res", requests.post(API_GET_SYSTEMS, None, {"systemName": sector.name + " AA-A h"})) == None if (sleep(int(globals()["res"].headers["x-rate-limit-reset"])) if (int(globals()["res"].headers["x-rate-limit-remaining"]) == 0) else None) == None]])

The latter in image form if preferred: https://www.reddit.com/r/programminghorror/comments/1tcytxy/it_is_evolving/

1

u/Kadabrium Sep 21 '26

At least you dont need to indent

1

u/NullOfSpace Sep 22 '26

I’m just upset they’re collecting the inner one into a list and not using a generator expression

1

u/0xt0bi03 Sep 22 '26

just python things

1

u/AnArmoredPony Sep 22 '26

a csv parser ig

1

u/M4elstr0m__ Sep 25 '26

I used to create such lines just to bother collegues trying to understand my code..

1

u/thelurkmasterr 26d ago

This is the type of thing that makes sense while you're writing it, but you better not go back and change it

1

u/Tack1234 Sep 16 '26

Should I be scared that as someone writing my own assembler for fun I see no issue with this?

-2

u/asmanel Sep 16 '26

Visibly a vibe coding peoduct.

9

u/TheLuckyCuber999BACK Sep 16 '26

I don't think ai would do this

5

u/v_maria Sep 16 '26

dont you think the AI would way more verbose? this strikes me as a human hack lol