crossing the streams (all code sucks) #12

  Рет қаралды 4,033

anthonywritescode

anthonywritescode

Күн бұрын

Пікірлер: 29
@senpos
@senpos 4 күн бұрын
"All Code Sucks" is a lot of fun! One-liners are so tempting. There's always that itch inside to make the solution fancy and perfect and smart, but maybe writing simple and straightforward code is better after all. :D
@jtw-r
@jtw-r 4 күн бұрын
Yes I agree 100% about one liners. If you find yourself writing a one liner often, abstract it to a function and name it accordingly so it “reads” easier to the other developers. Something as simple as inline sorting logic can be extracted to a named function to make it even clearer (i.e. sort_by_page_title_descending() ) Tangentially, I’m of the camp that the longer the variable names are, the better documented and easier to read your codebase is. Definitely not a 1to1 but it’s always a good sign.
@rednafi
@rednafi 4 күн бұрын
ExitStack is magic. I'm yet to encounter something so elegant in any of the few languages that I write alongside Python--TS, Go, Elixir, etc.
@vasiliigulevich9202
@vasiliigulevich9202 4 күн бұрын
It is RAII concept from C++ and Ada. Python and Java use libraries to achieve the effect.
@QckSGaming
@QckSGaming 4 күн бұрын
Go has context and defer for example. I've never seen a valid use case for ExitStack. Just write proper code in the first place and don't wrap it in some conditionally closed context.
@vasiliigulevich9202
@vasiliigulevich9202 4 күн бұрын
@QckSGaming it is needed to track out-of-stack lifetimes. For example a log-lived socket connection that can't be closed after method returns.
@QckSGaming
@QckSGaming 4 күн бұрын
@@vasiliigulevich9202 Huh? It runs all exit callbacks when you exit the scope. Are you talking about something else? In any case, that does not sound like a place where you would want to combine several context managers into a single with statement..
@vasiliigulevich9202
@vasiliigulevich9202 4 күн бұрын
@QckSGaming yes, but the scope is not limited to a method or a with keyword. Asking ExitStack to na member and manage lifetimes of other members or dependencies. I usually implement C++ RAII with it by disposing all members together with their container. But it is a bit more flexible than just that.
@b_0rk
@b_0rk 3 күн бұрын
"it's still in production" industry standards bite again
@preocts4086
@preocts4086 4 күн бұрын
The number of times I've seen or left PR comments to the tune of: "This is some clever code. Can't find the bug, but I know it's there somewhere" is more than one. That's too many. xD
@BabakFiFoo
@BabakFiFoo 4 күн бұрын
Anthony, you are the best. Please keep up with such content!
4 күн бұрын
For a simple case like this, I think I would use "context = open(file) if file else contextlib.nullcontext(sys.stdin)"
@ArielVolovik
@ArielVolovik 4 күн бұрын
would this properly close the file?
4 күн бұрын
​ @ArielVolovik Yes, but there's a chance the process terminates between the `open` and the place where you use the context
@prosodyspeaks4036
@prosodyspeaks4036 4 күн бұрын
how about file, cm = (file, open) if file else (sys.stdin, nullcontext) with cm(file) as stream: return next(iter(stream)) handles the cases anthony uses in the vid
@PanduPoluan
@PanduPoluan Күн бұрын
​@@prosodyspeaks4036What if you want to specify parameters to the open() call?
@MakeDataUseful
@MakeDataUseful 4 күн бұрын
Very clever, thanks for sharing
@mrswats
@mrswats 4 күн бұрын
That's a dirty bug!
@yorailevi6747
@yorailevi6747 4 күн бұрын
I don’t think a lot of people would understand why the new code is written that specific way, I rather do the if outside and conditionally enter the context with a plainer looking code and 2 duplicate returns
@PanduPoluan
@PanduPoluan Күн бұрын
That's why code comments is important. Just describe the "why" to prevent others from doing the same one-liner mistake.
@kamurashev
@kamurashev 4 күн бұрын
One of the only in python videos 😂
@prosodyspeaks4036
@prosodyspeaks4036 4 күн бұрын
surprised to hear about perpetual issue of conditional contexts - isn't this what nullcontext is for? i've used it successfully to conditionally manage seek-offsets and temporary-filters for db cursor management. smth like this? i dont like doing two checks for file=None, but maybe there's another implementation and in any case definitely think nullcontext should be mentioned! import sys from contextlib import nullcontext def read_first(file: str | None = None) -> str: cm = open if file is not None else nullcontext with cm(file) as stream: stream = stream or sys.stdin return next(iter(stream)) edit: def read_first(file: str = None) -> str: file, cm = (file, open) if file else (sys.stdin, nullcontext) with cm(file) as stream: return next(iter(stream))
@PanduPoluan
@PanduPoluan Күн бұрын
You lose the ability to specify different parameters if you simply use nullcontext() What Anthony showed is a generalized solution. Sure you can use functools.partial() but that adds too much "cleverness" to the code.
@qexat
@qexat 4 күн бұрын
ohh neat trick
@iliya-malecki
@iliya-malecki 4 күн бұрын
Define a function that works on pre-opened file descriptors and manage opening outside? Why switch between very different and fragilely stateful types - I mean it's simple for me to say that once I see the important difference between the two :)
Creating Your Own Programming Language - Computerphile
21:15
Computerphile
Рет қаралды 111 М.
typing the untype-able with mypy plugins (advanced) anthony explains #574
25:06
Как Я Брата ОБМАНУЛ (смешное видео, прикол, юмор, поржать)
00:59
Натурал Альбертович
Рет қаралды 3,9 МЛН
When Cucumbers Meet PVC Pipe The Results Are Wild! 🤭
00:44
Crafty Buddy
Рет қаралды 58 МЛН
JavaScript runs on literally everything now
12:17
Theo - t3․gg
Рет қаралды 30 М.
my computer is sick in a very suspicious way
6:55
anthonywritescode
Рет қаралды 7 М.
they found another backdoor.
15:26
Low Level
Рет қаралды 265 М.
New divisibility rule! (30,000 of them)
26:51
Stand-up Maths
Рет қаралды 327 М.
simplifying sorting! (all code sucks) #09
6:36
anthonywritescode
Рет қаралды 7 М.
Programming Is Cooked
9:30
ThePrimeTime
Рет қаралды 166 М.
Why Didn't He Get the Job? Let's Find Out! // Code Review
27:25
The Cherno
Рет қаралды 149 М.