r/badcode • • Dec 13 '20

python import os as o

Post image
1.1k Upvotes

66 comments sorted by

80

u/WS8SKILLZ Dec 13 '20

How can something so small be so wrong.

20

u/TheSoldierCat Dec 13 '20 edited Dec 13 '20

Although I want to I cannot make such mistakes.

7

u/Isvara Dec 14 '20

It's not wrong, just a bit pointless.

12

u/loomynartylenny Dec 14 '20

nah, you only need to use o 6 times and then you'll be able to save lots of typing time and disk space by not using that incredibly pointless s that usually comes at the end of os :^)

3

u/Astrokiwi Dec 14 '20

One-character variables are a little bit wrong, just because they're harder to search for. That's why some people recommend xx,yy,zz for cartesian coordinates and ii,jj,kk for indices.

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_like

172

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 macos

91

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 macos

For good measures, I'd suggest doing os = windows or linux or macos just 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

u/DabsJeeves Dec 13 '20

This is commonly done in javascript too

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 to

result = foo()
if not result:
    result = bar()
if not result:
    result = baz()
return result

3

u/drcforbin Dec 14 '20

This is a thing of beauty.

1

u/[deleted] Feb 02 '21

import sys as system_of_operation

7

u/pink-ming Dec 14 '20

I don't think I've ever seen a reverse abstraction before, bravo

-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

u/ZoleeHU Dec 13 '20

I can't import you without getting any closer.

42

u/Ponyboy-Curtis Dec 13 '20

o o o then come as close as you’d like

24

u/MatthewRPG576 Dec 13 '20

o-ho! Then import me as much as you'd like

59

u/Glaciant Dec 13 '20

Wait, echo? In Python?

23

u/[deleted] 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

u/OriginalTyphus Dec 13 '20

Yeah, thats what i thought.

3

u/searchingfortao Dec 13 '20

There's no getEnv() either. It's getenv().

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

u/[deleted] 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

u/pinguluk Dec 13 '20

Yeah but if you use the os library multiple times in the code?

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

u/jakkoha Dec 13 '20

good human

14

u/[deleted] Dec 13 '20

Sacrificing 3 characters to save 1

2

u/[deleted] Dec 14 '20

Did you save one character?

Yes

What did it cost?

Three characters

10

u/pantlesspatrick Dec 13 '20

Not bad enough, I'd do

import os as OperatingSystemGuys

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

u/ZoleeHU Dec 13 '20

Yeah my bad, it actually might be Nim

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

u/RevRagnarok Dec 17 '20

It's flaired as Python.

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

u/MCWizardYT Dec 13 '20

All great points

1

u/sn3kgos Dec 14 '20

python code golfers \s

2

u/Derangedteddy Dec 13 '20

import os as operating_system

2

u/fshpsmgc Dec 14 '20

Ah, I see a man dedicated to the cleanest code possible. Every symbol counts, after all

2

u/m7priestofnot Dec 14 '20

import os as pandas

1

u/HumanCondition1312 Dec 13 '20

The true story of O

1

u/BlackDog2017 Dec 14 '20

Two-character variable names are just too damn long.

1

u/ignassew Dec 14 '20

People who write k instead of ok:

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

u/xlevidi Dec 14 '20

Wow this code is so bad

Mods please ban OP

3

u/TheSoldierCat Dec 14 '20

the command is also wrong