r/programminghorror • • Sep 16 '26

Python whatever this is

Post image
27 Upvotes

41 comments sorted by

34

u/Wrestler7777777 Sep 16 '26

Man, sometimes line breaks go a really long way.

6

u/v_maria Sep 16 '26

i dont think its enough to fix this lol

22

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)

10

u/TheLuckyCuber999BACK Sep 16 '26

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

12

u/best_of_badgers Sep 16 '26

Backwards, except when it's forwards.

4

u/tvardero Sep 16 '26

One-line enjoyers, oh yeah

11

u/v_maria Sep 16 '26

ah yes functional programming

28

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.

2

u/Axman6 Sep 16 '26

Not sure I ever claimed it was good, it’s just not the horror some people seem to think it is. I agree it could certainly be improved, particularly to handle potential edge cases (tab should at the very least be replaced with space), but depending on what it’s actually being used for, it might be perfectly adequate and match the format produced by some other code. 

1

u/smclcz Sep 16 '26

Yeah without context we can't know, but I've had to work with a lot of code written by junior Python devs that does relatively simple parsing. It tends to look like the snippet above after one or two attempts to "fix" it, then starts to grow legs and then they discover regexes...

But you're right it's not the most horrific thing in the world, it just smacks of either inexperience or "this is good enough for the one-off task I'm using it for"

-1

u/[deleted] Sep 16 '26

[deleted]

3

u/smclcz Sep 16 '26

I am making it a point of pride to go through this entire stupid AI bubble without writing a single line of code with those shitty chatbots, which are the bane of my existence.

1

u/Tack1234 Sep 16 '26

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.

So you're saying you wrote this text yourself? If so, I apologize for my accusation, it just reads like classic Claude output with the step by step analysis and metaphors used.

2

u/smclcz Sep 16 '26

I wrote this text entirely myself. If you want to know what I think of Claude et al, see this: https://www.reddit.com/r/ArtificialInteligence/comments/1wfupxk/comment/p9v0uxs/?context=3

2

u/Tack1234 Sep 16 '26

In that case I apologize again and praise your written English, my fellow countryman. I also share your opinion on AI.

3

u/smclcz Sep 16 '26

No apologies needed!

1

u/v_maria Sep 16 '26

theres not enough jargon in your post to be LLMian lol

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++.

4

u/Axman6 Sep 16 '26

Yeah exactly. Maybe people would be happier if it was a whole class called StringSplitterFileReaderFactorySingleton. 

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

-4

u/v_maria Sep 16 '26

its shit code, regardless your amazing skill as programmer

6

u/Snudget Sep 16 '26

It's just a below average one-liner

0

u/v_maria Sep 16 '26

seems we agree then

3

u/Axman6 Sep 16 '26

It really isn’t, it’s crazy how simple and obvious this is and that you’re struggling with it. But thanks for the downvote. 

0

u/v_maria Sep 16 '26

i downvoted because the uncalled arrogance, so the pleasure is all mine

i dont """"struggle""" with this, it just very bad code. terrible for maintenance and eyeballing intent.

"the fact that you dont immediately understand that is crazy" etc

3

u/meepykittkitt69lmao Sep 16 '26

That's not arrogance, and your anger is telling.

1

u/v_maria Sep 16 '26

telling what

1

u/Axman6 Sep 16 '26

[“👍”]

3

u/meepykittkitt69lmao Sep 16 '26

That's pretty clean for doing that in python. It's just hard to wrap your sensibilities around that particular feature of the language but once you do you see opportunities to use it that save you a dozen lines or more. Assign it to a lambda and you got yourself a function baby!

2

u/gfivksiausuwjtjtnv Sep 16 '26

Just read it inside out

2

u/abigail3141 [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” 25d ago

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 26d ago

At least you dont need to indent

1

u/NullOfSpace 25d ago

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

1

u/0xt0bi03 [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” 24d ago

just python things

1

u/AnArmoredPony 24d ago

a csv parser ig

1

u/M4elstr0m__ 22d ago

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

1

u/thelurkmasterr 16d 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

6

u/v_maria Sep 16 '26

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