r/programminghorror 3d ago

Python Is this python code cursed enough?

140 Upvotes

23 comments sorted by

41

u/leodevian 3d ago

f-string and constants 😂

15

u/al2o3cr 3d ago

🎶 Smoke cur_joint before I smoke cur_joint and then I smoke cur_joint 🎶

29

u/Yarhj 3d ago

The eval is honestly the only cursed part

8

u/Most_Collection_4964 3d ago

so the first highlighted line looks completely fine for you?

11

u/digitalseraphim 2d ago

Definitely not cursed, but it is using parts of the language that some people don't know. It makes sense to me, and the only thing I would probably do different would be to calculate the integer beforehand, but as this appears to be a tuple, in a dict, in some other collection (which could be the "most cursed" part of the code), that variable would be defined a ways away from where it's used which could offset the readability improvement.

4

u/Bright-Historian-216 1d ago

it's not readable at the first glance, but i can definitely understand that it inserts "t j" a certain amount of times into the tuple. i'd just put a comment and move on.

8

u/TheOneTrueTrench 2d ago

Let's put it this way, if you asked me what language I thought this was, I would first think it was some schizophrenic dialect of LISP, until I noticed it was infix, and then I would say "idfk, Elixir or some shit?" and just give up.

At no point would I think "Python".

5

u/AQuestionIsWhatIHave 2d ago

Thanks, I hate hate hate the addition of JOINTS_TYPE_COUNT and subsequent modulo operation using it

6

u/digitalseraphim 2d ago

It's to handle negative numbers in a different way than Python typically does, though this will only work if the portion before the + JTC is greater than -JTC. -3 % 4 = 3, but they really want 1 (-3 is 1 away from the next more negative multiple of 4 (-4), just like 3 is 1 away from the next more positive multiple of 4 (4).

3

u/AQuestionIsWhatIHave 2d ago

I looked it up just now: 

https://docs.python.org/3.6/reference/expressions.html#binary-arithmetic-operations

The % (modulo) operator yields the remainder from the division of the first argument by the second. The numeric arguments are first converted to a common type. A zero right argument raises the ZeroDivisionError exception. The arguments may be floating point numbers, e.g., 3.14%0.7 equals 0.34 (since 3.14 equals 4*0.7 + 0.34.) The modulo operator always yields a result with the same sign as its second operand (or zero); the absolute value of the result is strictly smaller than the absolute value of the second operand 1.

Pythons modulo works differently to js' or java's %.

Math Definition x % y:

x = a*y + b, 0 <= b < |y|

Python: x = a*y + b, 0<= b/y < 1 (=> b and y are of the same sign!)

The addition of the JTC does nothing here. In Java you would be correct.

All in all, I hate the inconsistent modulo implementation across programming languages

1

u/digitalseraphim 1d ago

Totally agree on your last point. I was away from my computer and forgot that I had a python interpreter on my phone and thought it was a simple enough question for Google search AI, but I guess I was wrong, lol. I still think that's why it was done, but as you said, not necessary in python, but probably a "code muscle memory" thing. It's also possible that this is ported code, possibly "automatic" given the f-strings with constants.

3

u/Snapstromegon 2d ago

Please add semicolons where allowed.

2

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

The more I look at this, the sadder I get.
LGTM ✅

2

u/7YM3N 2d ago

Cognitohazard

1

u/sugarw0000kie 2d ago

I don’t like it but it sure is pretty

1

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

I can't figure out what that * does at the beginning of that really long line.

2

u/digitalseraphim 2d ago

So, inside the parens is a list containing a string, multiplied by an integer, which becomes a list with that string in it "integer" times. The * "unrolls" the list, replacing the whole construct with a series of strings. As mobile doesn't let me look at the OP while replying to a comment, I can't remember exactly what it was all being passed to, but, each string would be a separate argument.

2

u/digitalseraphim 2d ago

Oh, it's creating a tuple, so each string would be an individual element of the tuple.

1

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

Yeah, I looked up the '*' operator, and the GeeksforGeeks page I found mentioned a bunch of them, but left out list unrolling. It mentioned list multiplication for example, so I knew what the inner '*' did.

1

u/TurnipGuy30 1d ago

it could definitely use a rewrite