r/badcode • • Sep 12 '20

python Thanks, i hate you.

Post image
712 Upvotes

50 comments sorted by

118

u/Kazumara Sep 12 '20

This is python right?

Don't ask how I know but if you are using CPython then any function that is nested beyond 20 levels of depth will throw a syntax error.

Edit: Although now that I think about I only know that's the limit for loops, not sure whether conditionals count towards the same

81

u/Brekkjern Sep 12 '20

Don't ask how I know

This deserves a story!

32

u/Kazumara Sep 13 '20

A friend and I were tasked with creating our own flow control algorithm in a simulator. I think the only input signal we had was the current round trip latency and our solution was evaluated on different network traces. This was for our studies.

So we had some ideas and cobbled together our code in python, but then we didn't know what parameters to choose for various threshholds and what multipliers for different scoring functions.

So we started using a little driver script to run scoring with our current solution with the cross combination of some of the parameters.

Instead of doing it properly we just nested some for-loops, but then we had more ideas which added more paramers and we parameterized things we had taken for granted as constant in the beginning.

In the end we had extended our ghetto grid search algorithm to 36 dimensions, and in order to satisfy the compiler constraint we had to separate it into two functions, within the eighteenth layer of loops we called a function with eighteen parameters, which then contained another eighteen loops and finally called our solution file with all 36 parameters.

We had it running overnight with even just a few parameter combinations but it did help us a bit in optimizing our solution. We probably overfitted the traces we were evaluated against, but then who cares if it's just for a class :)

1

u/[deleted] Sep 13 '20

Was this for a computational linear algebra class? Your approach reminds me of one of my solutions.

5

u/Kazumara Sep 13 '20 edited Sep 13 '20

It was the Future Internet course by Prof. Dr. Ankit Singla. He said it used to be called Advanced Computer Networking but the clickbait course name has better signup rates.

Edit: looking at the project descriptions again, it might also have been for the adaptive bitrate video streaming assignment.

Edit2: One project description says they are using hidden trace sets now and to avoid overfitting, lol

20

u/cherry_professional Sep 13 '20 edited Sep 13 '20

It’s similar to how you can’t nest a list ([[[…]]]) more than 100 levels deep:

>>> eval("[" * 101 + "]" * 101)
s_push: parser stack overflow
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
MemoryError

I don’t know of any use case for such a deeply nested data structure, but I feel like the language shouldn’t pose such arbitrary restrictions on you.

17

u/rathmere Sep 13 '20

"Arbitrary restrictions", eh? Scala's 22 max tuple size would like a word. 😜

1

u/KingDarkBlaze Sep 16 '20

Ah yes, because each size of tuple is a separate hard-coded object.

20

u/younes_mlik Sep 13 '20

but I feel like the language shouldn’t pose such arbitrary restrictions on you.

I personally think it should. If the language doesn't allow it, there's probably a better was of doing it.

7

u/qh4os Sep 13 '20

It’s not really an “arbitrary restriction”, like the error says, having that many nested things causes a stack overflow. This makes sense because a stack is a great tool for reclusive structures, but like everything else, it can run out, and when it does, that’s not good.

1

u/[deleted] Sep 13 '20

write a better parser then, works in javascript

eval("[".repeat(1000) + "]".repeat(1000))
> [Array(1)]

1

u/Kazumara Sep 13 '20

Yeah that's what I said too. "I know this is ugly and I should be using zip and permutations or something, but this is a throw away script, so let me nest my loops just for a minute here"

1

u/qh4os Sep 13 '20

The issue isn’t a language restriction, it’s that the stack only has limited space, if your computer had infinite memory, then it would “let” you

1

u/Kazumara Sep 13 '20

I know, that's why I said

if you are using CPython

I looked at the source back then and saw the statically allocated stack.

1

u/qh4os Sep 13 '20

Regardless of where it is stored, it’s still not arbitrary, and parsing recursive structures isn’t easy with it recursion

1

u/[deleted] Sep 13 '20

Not an arbitrary restriction, you could programmatically nest a list with a loop. There's just restrictions on evaluating a hardcoded list.

1

u/[deleted] Sep 13 '20

I don't think that's the case for conditionals, but could be wrong and i'm not about to type one out. I worked on a codebase once where someone had done something like this to check existence of nested properties of a dict and i'm pretty sure it had conditionals that deep. They were over 2 screens wide.

144

u/MurdoMaclachlan public boolean isInt(int i) { return true; } Sep 12 '20

Image Transcription: Code


[The last four lines in the following snippet of code are cut off in the screenshot:]

# Adds a reaction if a message contains all letters of the alphabet
        if 'a' in message.content.lower():
                if 'b' in message.content.lower():
                        if 'c' in message.content.lower():
                                if 'd' in message.content.lower():
                                        if 'e' in message.content.lower():
                                                if 'f' in message.content.lower():
                                                        if 'g' in message.content.lower():
                                                                if 'h' in message.content.lower():
                                                                        if 'i' in message.content.lower():
                                                                                if 'j' in message.content.lower():
                                                                                        if 'k' in message.content.lower():
                                                                                                if 'l' in message.content.lower():
                                                                                                        if 'm' in message.content.lower():
                                                                                                                if 'n' in message.content.lower():
                                                                                                                        if 'o' in message.content.lower():
                                                                                                                                if 'p' in message.content.lower():
                                                                                                                                        if 'q' in message.content.lo
                                                                                                                                                 f 'r' in message.co
                                                                                                                                                        if 's' in me
                                                                                                                                                                if '

I'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!

30

u/NullBrowbeat Sep 13 '20

You're a madman

23

u/igotanewmac Sep 12 '20

If only I had more than one upvote to give! :D

10

u/MurdoMaclachlan public boolean isInt(int i) { return true; } Sep 12 '20

❤️

7

u/--B_L_A_N_K-- if (true == true) {return true;} else {return true;} Sep 13 '20 edited Jul 01 '23

This comment has been removed in protest of Reddit's API changes. You can view a copy of it here.

3

u/fuj1n sadistic Sep 13 '20

Have platinum mate, that was a lot of effort you put in there.

2

u/MurdoMaclachlan public boolean isInt(int i) { return true; } Sep 13 '20

Oh, wow! Thank you so much! :D

108

u/uttamo Sep 12 '20
from string import ascii_lowercase

if set(ascii_lowercase) <= set(message.content.lower()):
    pass

6

u/earlobe7 Sep 13 '20

TIL python less than/greater than operators on sets test for subsets/supersets. Cool

47

u/earlobe7 Sep 12 '20

shitfuck = all(c in message for c in [ chr(97+i) for i in range(26) ])

20

u/glebbash Sep 12 '20

but it was message.content.lower()

46

u/earlobe7 Sep 12 '20

Oof you right, I didn't follow specs...i guess im in the right sub.
Not even gonna correct it out of solidarity.

9

u/XDracam Sep 13 '20

That response made me laugh you shit

1

u/0x564A00 Sep 13 '20

And what's missing in both this and the original code is to apply unicode normalisation NFC or something similar to make sure accented letters don't match even when decomposed.

6

u/xigoi Sep 13 '20

Why not just all(chr(c) in message for c in range(97, 97+26))?

1

u/earlobe7 Sep 13 '20

Yeah, I do like that better.

10

u/[deleted] Sep 12 '20

why dont you try to make it all like x="alphabet" if x bla bla bla im not good at texts

32

u/NeveryEvermore Sep 12 '20

You can already compare the sets in python. so you can just do

set_a = set("The quick brown fox jumps over the lazy dog")

set_b = set("abcdefghijklmnopqrstuvwxyz")

set_b.issubset(set_a)

10

u/mcmc331 Sep 13 '20

The classical discord bot, i could've seen that from a mile away

9

u/[deleted] Sep 13 '20

Oh God this makes me uncomfortable. My teeth itch, my skin is retracting toward my spine, and I'm smelling burnt toast.

1

u/BackgroundChar Sep 13 '20

I'm smelling burnt toast.

Please call an amberlamps

3

u/haykerman Sep 13 '20

Is this what they call AI?

4

u/XDracam Sep 13 '20

At least it's indented...

2

u/maxduskwalker Sep 13 '20

Wouldn't it be better approach to go for regex here ? It's always a good idea to avoid nested loops or conditions!

1

u/lucdewit Sep 13 '20

The most cursed thing about this, is the indentation on the first if statement

1

u/oberguga Sep 13 '20

Also inefficient but doing the exactly the same as code above.

charlist can be ('a','b','c',...) of a string

def sillyfunc(message,charlist) For char in charlist: If char in message.content.lower() return True return False

1

u/[deleted] Sep 13 '20

won't this just return True if the message has an 'a' in it?

I think you mean:

def sillyfunc(message,charlist):

for char in charlist:

if not char in message:

return False

return True

1

u/_haema_ Sep 13 '20

this is so nested birds want to lay eggs on it

1

u/Kit_Saels Sep 12 '20

What is on the picture?

3

u/TSM- Sep 13 '20

Someone had transcribed it 2 hours before your comment

edit: The link is https://www.reddit.com/r/badcode/comments/irjn6p/thanks_i_hate_you/g4zc25p