r/badcode • u/[deleted] • May 03 '21
python step 1: write an unefficient code step 2: explode
317
May 03 '21 edited May 03 '21
import os
while True:
os.fork()68
u/givemeagoodun May 04 '21
:(){ :|:& };:48
39
May 04 '21
In human language: This declares in sh a function called : which internally calles itself 2 times in parallel. Afterwards : gets called.
4
85
u/ATE47 May 03 '21
Is using fork() actually a thing in Python? Won’t it weirdly clone the VM?
16
u/nickcash May 04 '21
Yeah, but it's not really that weird. fork()ing in bash clones the bash interpreter too.
7
u/R3D3-1 May 04 '21
Not if you want the code to be able to run on Windows.
Don't use fork for anything you plan to put on PYPI.
3
May 04 '21
PYPI last I checked allows you to upload packages that only support one os. There is no requirement to support windows
3
u/R3D3-1 May 04 '21
Good to know.
Sadly, I've been bitten a few times by packages that don't declare that they don't work on Windows, so there's a risk of unix developers accidentally making their packages incompatible with Windows.
Weirdest example was a Scientific package, that would have worked perfectly fine on Windows; But the installation had to be done manually, as the setup.py for some reason invoked os.fork().
2
May 04 '21
Actually you need to declare what operating systems you support not the other way around
0
u/R3D3-1 May 04 '21
Where though?
When I installed https://pypi.org/project/sh/ on Windows, nothing indicated incompatibility until I tried to import it, at which point the POSIX-only fnctl package was missing.
On PYPI I can't see any indication that it is POSIX-only.
3
u/cheerycheshire May 04 '21
This literally shows that it allows sh stuff using python. How did you expect to use sh in Windows anyways?
Also, it has no OSs marked as compatible. So don't say it says it works on Windows, it doesn't declare itself in any way.
2
u/R3D3-1 May 04 '21
The sh package primarily is represented as a way to execute shell commands. Windows has those; From the description there's no reason why sh.rsync("dir1", "dir2") would fail on Windows, if an rsync.exe is available. When something like MSYS is installed, it is entirely reasonable to expect sh.cp("from", "to") to work.
It makes sense, that signaling aspects would fail on Windows, but that even import sh will fail was definitely outside my expectations.
1
May 04 '21
Unless a author has specifically mentioned a operating system is supported even if it does work. It means its not officially supported
4
-73
u/AutoModerator May 03 '21
It looks like this comment contains a code block delimited with triple backticks. Unfortunately reddit does not have universal support for this syntax and your comment will not render correctly on old reddit and most mobile apps.
For the benefit of people on old reddit, this link will take you to a correct rendering of the comment.
/u/Reschivon, it would be appreciated, but not required, if you could edit your comment to use the more compatible four space indention format. For single lines or inline code you can use single backticks.
You can find some examples in the reddit help documentation.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
35
u/Bobbbay May 03 '21
Not sure why this is downvoted, but let's join the downvote party 🚀🚀🚀
18
3
u/cheerycheshire May 04 '21
It gets downvoted because it's always there. Instead of saying "this valid markdown doesn't work for everyone", Reddit should just fix itself.
I browse Reddit mainly on my phone. Backtics are way easier to write than lots of spaces - especially since a lot of keyboards default to putting a period if you hit space twice fast enough...
2
u/Bobbbay May 04 '21
I mean, Reddit doesn't send those messages, r/badcode does :D.
I agree that Reddit should fix itself, but instead of taking it out on poor automod people should actually do something more productive, such as asking Reddit directly to fix this, upvoting u/backtickbot (if I recall correctly) - but not downvoting poor automod every single time.
2
u/cheerycheshire May 04 '21
I know, but people take their frustration on what they can - because Reddit doesn't listen. How long it's been like that? Too long.
And tbh backtickbot does the thing more annoyingly than your automod. :x It says "oh, you should write it like this instead", while your automod does it in "here's the version so you can read it, person above me doesn't have to change anything, it's Reddit that gets it wrong". After I replied I actually read what your automod says and how it says it, it's great in wording - but people probably got used to annoying wording of backtickbot and downvote out of habit.
3
u/Bobbbay May 04 '21
I agree.
Re: automod comment: Yep, Vusys put time into that I believe. It just gets on my nerves that such a great comment has to get downvoted, especially since it's really providing a link for old Reddit users.
Re: Reddit not listening: Maybe we'll put this subreddit on strike :P
4
u/selplacei May 04 '21
Why the hell do people keep downvoting this? Just to be asshats for the sake of it?
-1
u/SirFireball May 04 '21
Because Automod is annoying.
10
u/selplacei May 04 '21
You're annoyed by a system that helps people because it exists? Sounds like you're an asshat
4
u/CorrenteAlternata May 04 '21
[DISCLAIMER: what follow is NOT my opinion or my pov, I'm just stating the point of view of those who hate the automod.]
People are annoyed because Reddit's formatting is basically a modified version of Markdown, which is standardised.
The triple backticks are the standard way of representing blocks of codes whilst Reddit's four spaces things is very non standard.
People are annoyed with the automod because it looks like it is putting the blame on the user, for writing using the standard, unsupported by Reddit, way, when, in their opinion, Reddit should be blamed for not adhering to the standard.
Their idea is basically, we should all continue using backticks, as most third party apps support them, and that eventually will force Reddit's own app and website to adapt.
[Disclaimer: personal opinions follow]
I mean, I don't personally think that's going to work, so I am glad that the automod tries to give you a suggestion, even though I would probably have preferred a bot that simply replies with the correctly formatted block and a link to a wiki page on which you explain the issue. As it is, the bot is too verbose, imo.
6
u/Poddster May 04 '21 edited May 04 '21
The triple backticks are the standard way of representing blocks of codes whilst Reddit's four spaces things is very non standard.
This is completely untrue. The original Markdown implementation was 4-spaces only. Thus every implementation of Markdown after that supported it as well. Thus it is the "standard".
I think it was "Common Markdown", pushed by Stackoverflow, Reddit, and GitHub that added the triple-backticks.
Reddit should be blamed for not adhering to the standard.
Reddit should be blamed for having a new UI which uses their new Markdown interpreter which uses the triple-backticks without updating their old one.
Their idea is basically, we should all continue using backticks, as most third party apps support them, and that eventually will force Reddit's own app and website to adapt.
Reddit's "current" website already supports it. The problem is there's like 6 UIs for reddit.
1
u/CorrenteAlternata May 04 '21
Thank you for the corrections, you appear to be way more informed than me on the topic! :)
1
u/selplacei May 04 '21
But the bot does provide a link to the correct formatting. That's the whole point
97
73
u/Spitfire1900 May 03 '21 edited May 03 '21
While you’re at it upgrade to 64-bit python
6
0
May 04 '21
[deleted]
1
u/Thesecondiss May 04 '21
32-bit python is limited to 4Gb of RAM. to use anymore you're gonna have to use the 64-bit version
25
23
May 03 '21
looks like my work laptop, just replace python with carbon black and 4 other BS endpoint security scanners
17
u/Wu_Fan May 03 '21
Is it doing some sort of thread hungry recursive thing?
17
May 03 '21
OP said it’s a prime number calculator
13
u/Wu_Fan May 04 '21
“A prime number calculator” and “a thread hungry recursive thing” could be the same or different because one is a purpose and one is an engineering approach.
10
13
7
u/Pacm3ns May 03 '21
Did you know if you let a laptop run for a week on 100% cpu it will get fried. After that it might so slow that you cant wven double click. Totally didnt learn that from experience.
5
10
10
u/HildartheDorf May 03 '21
All those registers and other niceties of x64 you could also be exploding if you used 64-bit python.
YMMV but even though 64-bit code should be slower (more memory usage), x64 has so many other advantages over x86 that uses worth upgrading.
7
6
3
2
2
u/zombarista May 04 '21
I did a research project evaluating python, jython and Scala for ease of threading and scala was so effortlessly parallel it was my favored choice. You can make any collection parallel by calling collection.par and all of the functional methods (map, for each , flat map) were implemented to automatically go parallel. It made it easy and SAFE to go parallel and there’s a lot to be said for easy and safe being built right in.
Hope OP found the primes he was looking for!
1
1
1
1
u/kuronekonova Aug 16 '21
from tkinter import *
def main(): root = Tk() main() root.mainloop() main()
0
81
u/deleted1357 May 03 '21
wtf is this program even supposed to do?