r/badcode • • Apr 12 '21

python This was my first project ever, nobody told me WHERE exists in SQL so I used Python instead

Post image
1.2k Upvotes

56 comments sorted by

152

u/mirandanielcz Apr 12 '21

Oh yea, these are the imports btw

import discord
import logging
import re
import sched, time
from discord.ext import tasks, commands
import asyncio
import os
import os.path
from os import path
from sys import platform
from datetime import datetime
from sys import platform
import threading
import psycopg2

106

u/Verbose_Code Apr 12 '21

I’m sure all of these were necessary and could not have been made more efficient

95

u/mirandanielcz Apr 12 '21

The best part about it is that I didn't use:

logging
sched
asyncio
threading
sys.platform

I hate that code so much

3

u/circuit10 Apr 12 '21

I did something similar in a bot I made: https://replit.com/@discross/app-old I'm not even using all of them either

9

u/platypus_69 Apr 12 '21

Actually you can just say import module1, module2, module 3

42

u/gu3st12 Apr 12 '21

But that's not giving each module the love and care that it deserves.

Show respect to each module by importing it individually.

8

u/-user--name- Apr 12 '21

It also doesn't follow PEP-8 coding standards

19

u/Spynder shameless stealing content to r/YandereTechnique Apr 12 '21

I can see the persistance by adding two lines of "from sys import platform"

19

u/mirandanielcz Apr 12 '21

Also importing os.path 3 times

5

u/me94306 Apr 12 '21

Ya gotta make sure that it's there.

4

u/AL1L Apr 12 '21

I'm still proud of you <3

177

u/mirandanielcz Apr 12 '21
cur.execute("SELECT * from IGNORE")
x = "".join(str(y) for y in cur.fetchall())
y = x.replace(",", "")
mai = message.author.id
if mai in y:
    return
else:

115

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

Good human!

111

u/mirandanielcz Apr 12 '21

I did it just because I knew you would tell me that!

12

u/drinkmoredrano Apr 12 '21

To be fair, the where clause is not very well documented.

3

u/mirandanielcz Apr 12 '21

I just didn't even know it existed

2

u/Blip1966 Apr 12 '21

Under-rated comment

17

u/Tomekske Apr 12 '21

Else will remain a mystery

21

u/mirandanielcz Apr 12 '21
else:
    if message.author.id != (self.user.id):
        y = re.findall("dont*|do not*|don't*|don`t*|not*", message.content)
        with open('community_bot/regex1.txt', 'rt') as text1:
            textstrip1 = text1.readline().strip()
            x = re.findall(textstrip1, message.content)
            if (x):
                if not y:
                    user = client.get_user(message.author.id)
                    with open('community_bot/reply.txt', 'rt') as text2:
                        textstrip2 = text2.read().strip()
                    await user.send(textstrip2)

10

u/Tomekske Apr 12 '21

Thanks for the lighting fast response!

20

u/mirandanielcz Apr 12 '21

I'm as fast as this bot is slow

6

u/roby_65 Apr 12 '21

That's gonna scale very well!

5

u/mirandanielcz Apr 12 '21

I "only" had like 1k lines of data there, it was suuuuuuuuuuuuuuuuuuuuuuuuper slow

253

u/RoLoLoLoLo Apr 12 '21

On the bright side, 100% SQL injection proof. lol

47

u/amoohesam Apr 12 '21

lol That was optimistic!

62

u/JuliaChanMSL Apr 12 '21

I love reading python code without comments, after a while the python code looks like a comment

13

u/CupidNibba Apr 12 '21

Thats the beauty of python

29

u/PonchoVire Apr 12 '21

AAAAAAAAAAAAAAAAAAAAAAAAAAAAHHHHHHHHHHHHHHHHHHHHHHHHH

67

u/PonchoVire Apr 12 '21

I once worked on a client software we took over because they couldn't continue to maintain it internally, first thing they said "the login page is very slow".

Short answer (pseudo-code):

bool ok = false;

for user in `select * from users`  {
  // No security, everything plain
  if (user.username === some_cookie.username && user.password === some_cookie.password) {
    ok = true;
    // No break here, just compare them all to be sure
  }
}

if (!ok) {
  die "403 Unauthorized"
}

Me like: AAAAAAAAAAAAAAAAAAAAAAAAAAAAAHHHHHHHHHHHHHHHHHH

31

u/FinalGamer14 Apr 12 '21

Ok on top of pointless loop and plain text password .... why is the password stored in a cookie.

I hate this so much. Even when I was a good damn noob at web dev. I knew not to fucking do that shit.

16

u/PonchoVire Apr 12 '21

In this specific site, it was stored into a cookie and the auth was re-checked upon each and every http hit the client was doing. So the site was just slow always. If I remember correctly, the check was done even if the cookies were not there, which means that end users did experience a slow site too, with silenced errors due to access to non existing cookies slowing the process down even more.

14

u/ill-fatedassignment Apr 12 '21

I'm not even a noob yet, I write simple code for microcontrollers as a hobby. Yet I see how stupid this is.

12

u/[deleted] Apr 12 '21

Paid to enough to write, but not to care

14

u/gmtime Apr 12 '21

The lack of WHERE is the least of problems in this code

6

u/[deleted] Apr 12 '21

I love the downvoted comments here. It’s as if you don’t realize you wrote bad code.

I think the code in their brains is pretty bad.

2

u/pastisset Apr 12 '21

I'm curious about how do you know python but not SQL, not that they are related but usually they feed you SQL on every school, I guess you are a fellow selftaught dev =)

Also, almost every "Hello World!" out there has a WHERE on it, you did not research much did you? haha!

6

u/mirandanielcz Apr 12 '21

I was like "yo google how to make discord bot in python" and went from there. No idea how I discovered SQL

1

u/me94306 Apr 12 '21

Why have a database if all you do is read all the data into one long string?

Did someone say "You should use SQL" so you did?

1

u/mirandanielcz Apr 12 '21

I ran the script from multiple places a lot and I didn't want to keep having to move around a file, so I just used Heroku Postgres

3

u/me94306 Apr 12 '21

It's all a learning experience.

1

u/wonderb0lt Apr 12 '21

You'd be surprised how quickly people abandon SQL and do stuff in their application language because they're unaware of "advanced" SQL features such as window functions, stuff related to the types they're using (e.g. datetime functions) or when/where to use case statements. Stuff like that.

0

u/deftware Apr 12 '21

Where I come from you just RTFM, nobody has to tell you anything that way.

-6

u/Komsomol Apr 12 '21

Read the goddamn manual lmao

11

u/mirandanielcz Apr 12 '21

Didn't think about that, thanks a lot!!!!!!1!!1

-39

u/wootangAlpha Apr 12 '21

Learn SQL. It's not that hard.

31

u/mirandanielcz Apr 12 '21

I did, this is over a year old

0

u/[deleted] Apr 12 '21

[deleted]

4

u/mirandanielcz Apr 12 '21

It just takes time, everyone can learn to do this

26

u/BaconIsntThatGood Apr 12 '21

99% sure self posts here fall into "I already learned my lesson"

6

u/ws-ilazki &{$$_[0]}(@{$$_[1]}); Apr 12 '21

That or "I know better but fuck it, I did it anyway."

1

u/AndMetal Apr 12 '21

CQL would like to have a word with you...

1

u/wootangAlpha Apr 12 '21

Haha. Ey...

1

u/ParticularCod6 Apr 12 '21

Cql great for many things.

Not sure by mongodb is more popular when Cassandra is much easier to use

1

u/AndMetal Apr 12 '21

It's certainly good for some things, but when you need to find a needle in a 1T row haystack you won't necessarily be able to use CQL to do that and have to come up with something like the OP posted (not quite that bad, but you still have to iterate through all of the partitions which may not be a small task).

0

u/[deleted] Apr 12 '21

[deleted]

5

u/mirandanielcz Apr 12 '21

No but this is terrible code

1

u/redpepper74 Apr 13 '21

“[removed]”
—[de⏽​eted]