269
u/farlangben Dec 13 '20
import os as OperatingSystem
170
u/Dan6erbond Dec 13 '20
I prefer...
import os as windows_linux_osx_or_whatever_you_like172
u/Mr2-1782Man Dec 13 '20
Better yet:
import sys if sys.platform == 'win32': import os as windows elif sys.platform == 'linux': import os as linux elif sys.platform == 'darwin': import os as macos91
u/Dan6erbond Dec 13 '20
Now if this was Js... Sure, but in Python? You'd have to extend that to this, and then create a helper function:
import sys windows = None linux = None macos = None if sys.platform == 'win32': import os as windows elif sys.platform == 'linux': import os as linux elif sys.platform == 'darwin': import os as macos def get_os(): return windows or linux or macosFor good measures, I'd suggest doing
os = windows or linux or macosjust to be safe.64
u/neverFoundBetterNick Dec 13 '20
What kind of fuckery is this
6
u/_default_username Dec 14 '20
Basically the in-house framework my company uses for QA testing. We're a hardware company and have several products so our python code does all sorts of hacky shit like this to run on the different products with a common interface for testers. I fucking hate it. It's also done with classes and uses inheritance. It's so hard to figure what code is actually running and the IDE will throw errors, because it doesn't understand what's going on.
26
u/kodicraft4 Dec 13 '20
Gotta love the 'or' value returns lmao. Python will never cease to amaze me.
11
9
u/TinyBreadBigMouth Dec 14 '20
This isn't a special language feature, just the short-circuiting OR operator, similar to
||in C-like languages.
return foo() or bar() or baz()is roughly equivalent toresult = foo() if not result: result = bar() if not result: result = baz() return result3
1
7
-36
u/AutoModerator Dec 13 '20
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/Dan6erbond, 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.
215
u/MatthewRPG576 Dec 13 '20
o? You're importing me?
76
106
59
u/Glaciant Dec 13 '20
Wait, echo? In Python?
23
Dec 13 '20
That might be Nim
12
u/searchingfortao Dec 13 '20 edited Dec 13 '20
What's nim?
Edit: Nevermind, I figured it out. It looks pretty neat!
18
u/wikipedia_answer_bot Dec 13 '20
Nim is a mathematical game of strategy in which two players take turns removing (or "nimming") objects from distinct heaps or piles. On each turn, a player must remove at least one object, and may remove any number of objects provided they all come from the same heap or pile.
More details here: https://en.wikipedia.org/wiki/Nim
This comment was left automatically (by a bot). If something's wrong, please, report it.
Really hope this was useful and relevant :D
If I don't get this right, don't get mad at me, I'm still learning!
8
3
23
u/pinguluk Dec 13 '20
Just saving some filesize
24
u/Lysdal Dec 13 '20
except it isnt lol, the ' as o' takes up more space than the 'o.' as compared to 'os.'
20
Dec 13 '20
I guess if you use it enough it's saving something
10
u/Terrain2 Dec 13 '20
yeah, you only need to use it 6 times to save space (5 times is no difference, less than 5 is wasting space)
3
u/Lysdal Dec 13 '20
in general you do not care about filesize though, understandability and analyzabikity is way more important.
1
36
u/Tim3303 Please don't tell Murdo that I transcribed this Dec 13 '20
Image Transcription: Code
Example
import os as o
echo "USER = ", o.getEnv("USER")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!
18
14
10
19
u/ZoleeHU Dec 13 '20
The funny thing is that os doesn't even have a getEnv() method, it does however have a getenv() method, so this code is double bad.
Edit: Also the echo, lmao
20
u/Mycroft2046 Dec 13 '20
Have you considered the possibility that this mightn't be Python?
8
3
u/R3D3-1 Dec 14 '20
I wonder if the incorrect flair is r/badcode in itself.
I mean, imagine if someone would program C instead of C++, just because the compiler accepts it.
1
6
u/MCWizardYT Dec 13 '20
This isn't really bad code. The code itself works as intended, and they could call os whatever they want (import os as OperatingSystemLibraryWrittenInPython).
Its just kind of lazy in a weird way
16
u/pananana1 Dec 13 '20
Code working as intended does not mean it can't be bad code. Literally all bad code "works as intended".
Making unreadable code is bad code. This is awful. There's basically no time ever that giving a variable name of one letter is a good thing. Imagine all your variables were one letter.
5
1
2
2
u/fshpsmgc Dec 14 '20
Ah, I see a man dedicated to the cleanest code possible. Every symbol counts, after all
2
1
1
1
1
1
u/AGI_69 Dec 14 '20
The hard question is, what will you do with the extra time, that you will save by typing "o" instead of "os".
-5
80
u/WS8SKILLZ Dec 13 '20
How can something so small be so wrong.