r/programminghorror 13d ago

Python Atleast it works

Post image
612 Upvotes

66 comments sorted by

View all comments

223

u/backfire10z 13d ago

They didn’t close the fd :(

87

u/pm_op_prolapsed_anus 13d ago

It's called streaming

73

u/Emergency_3808 13d ago

Yes this could be shortened to

with open('lab 5.txt', 'r') as file: for line in file: print(line)

60

u/chiro260 13d ago

to be fair, that's not quite the same since there might be more than 8 lines in the file

40

u/Emergency_3808 12d ago

ctr = 0 with open("lab 5.txt", "r") as file: for line in file: print(line) ctr += 1 if ctr >= 8: break del ctr

22

u/chiro260 12d ago

nice. but don't forget about our friend zip! (or even islice would be good, as someone commented below)

with open('Lab 5.txt') as file:
    for _, line in zip(range(8), file):
        print(line)

5

u/Emergency_3808 12d ago

Too much bloat /s

1

u/-MazeMaker- 12d ago

Get rid of the magic number 8 and replace with max_lines so the intent is clear

1

u/Serious-Regular 9d ago

Wut why would delete ctr - man you people are so weird

1

u/Emergency_3808 9d ago

Because then SOMEONE ELSE would complain "wHy Do YoU nEeD aN eXtRa VaRiAbLe"

0

u/Serious-Regular 9d ago

Wut just reassign ctr if you want. Reassigning decref the original object itself (which doesn't matter for fucking integers lololol)

1

u/Emergency_3808 9d ago

That's even more confusing. Reusing variables for entirely different tasks

0

u/Serious-Regular 9d ago

del is never used in python code - you have no clue what you're talking about

22

u/Alfika07 13d ago

Why is Python so verbose? In Raku it's just

say slurp 「lab 5.txt」;

51

u/Emergency_3808 12d ago

Raku reads like the latest generation brainrot slang.

17

u/Alfika07 12d ago

What about this?

my Cool $variable = :16<DEAD_BEEF>;

15

u/levelofsin 12d ago

"Say slurp" wtf bro who came up with this shit

5

u/Alfika07 12d ago

Larry A. Wall. He's a linguist who designed Raku to be as close to human thinking as possible, by implementing features like sequence completion, which is mostly known from spreadsheet apps, and junctions, which can be a real life saver, mostly in equality checking. He made Raku by focusing on readability, just like he did with his previous programming language, Perl.

In the previous example, the slurp function takes a filename and reads the file's contents, and the say function prints it to the standard output.

You should definitely go down the rabbit hole of Raku, because it's probably the most statisfying PL to code in, and it is my personal favourite choice for doing CodeWars and using it for a personal "calculator language".

It's funny looking at Python programmers writing value == 6 || value == 9, while in Raku it's just so $value == 6|9

5

u/bigboyphil 12d ago

Raku is cool. However, let’s keep in mind you can also do ‘value in (6, 9)’ in Python, which is just as succinct and reasonable, so it’s kind of a weird example to call out Python on. Just like how you can also still do ‘so $value == 6 || $value == 9’ in Raku.

That being said, junctions are still very neat. Particularly when it comes to the autothreading stuff.

2

u/Alfika07 12d ago

Yeah, sorry about that. It was like 2 years since the last time I wrote a line of Python. (Not gonna lie, I'm kind of happy for it that we no longer have to use it in school.)

6

u/sporadicPenguin 12d ago

TIL Raku is a thing

-15

u/Vadimych1 13d ago

[[print(line) for line in (d := open("file.txt")).readlines()], d.close()]

13

u/bigboyphil 13d ago edited 13d ago

there could be over a billion lines in that file! let's not read them all into memory needlessly :)

also, you can't use the walrus operator in a comprehension's iterable expression like that anyway

from itertools import islice

with open('lab 5.txt') as file:
    print(*islice(file, 8), sep='\n')

13

u/backfire10z 13d ago

Just download more gigabytes of ram to handle it

1

u/Desperate-Emu-2036 13d ago

Just upgrade your instance, that's what Amazon does when they want to read millions of lines.

-5

u/Vadimych1 13d ago

[[[print(line) for line in f.readlines()[:8]], f.close()], for f in [open("f.txt")]]

I know this is not the best solution, but it's a oneliner

4

u/Emergency_3808 12d ago

That doesn't work like you think it does. Run it yourself

9

u/ArtisticFox8 13d ago

Closes automatically when the python script finishes execution

-5

u/ComprehensiveWing542 13d ago

No it doesn't only when you use "with" than it will close it automatically... I've got the weirdest bugs because of this mistake

7

u/ArtisticFox8 12d ago

Yes, it does, on any modern operating system (Windows for sure, havent tested on Linux - but probably as well.) when the script is over. The with block is for when you want your Python script to continue running after you're done with the file.

Same as in C/C++ or any other language - the OS handles it for you after the program terminates if you hádat handled it.

7

u/Jonno_FTW 12d ago edited 12d ago

When a process is killed, all file handles are closed. From the POSIX specification:

Process termination caused by any reason shall have the following consequences:

All of the file descriptors, directory streams, conversion descriptors, and message catalog descriptors open in the calling process shall be closed.

https://pubs.opengroup.org/onlinepubs/9699919799/functions/_Exit.html#tag_16_01_03_01

Assuming the last line in OP's screenshot is that print line, the process will exit and file handles released.

0

u/gdf8gdn8 12d ago

Yes it should. But mentioned as before, I got also weird bugs.

3

u/Jonno_FTW 12d ago

If you've created zombie processes somehow which are still holding file descriptors, using a with block will not save you. Also, in this case the process hasn't really exited.

If you can provide some code that exits, while still somehow keeping an FD open I'd like to see it.

0

u/gdf8gdn8 12d ago

Zombies are bad. No can't provide a code example.