stop making giant changesets!
14:12
python 3.13 release highlights
49:56
my thoughts on ruff
3:41
9 ай бұрын
Пікірлер
@Yayoxd19
@Yayoxd19 14 сағат бұрын
Thank you so much. It was so understandable :)
@Ivoire512
@Ivoire512 Күн бұрын
awesome keyboard😁
@PanduPoluan
@PanduPoluan Күн бұрын
I forgot where exactly but I did use ExitStack when developing aiosmtpd
@mrmultitask5921
@mrmultitask5921 3 күн бұрын
one of the points that sticks out to me is that sometimes it reports a time, sometimes its 0, where the actual time is actually a fraction of a second when it is measured. This smells of SMT/Hyperthreading taking away execution time and then suddenly popping everything into the foreground for your particular thread. Maybe try disabling SMT and see if things change?
@b_0rk
@b_0rk 3 күн бұрын
"it's still in production" industry standards bite again
@spencer3752
@spencer3752 3 күн бұрын
Oh man. For internal calls to user-facing APIs that may invoke a warning, I've been doing things like `warnings.catch_warnings` and re-emitting warnings to correct the stack level. This seems a lot more convenient.
@yorailevi6747
@yorailevi6747 3 күн бұрын
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.
@MakeDataUseful
@MakeDataUseful 4 күн бұрын
Very clever, thanks for sharing
@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 :)
@kamurashev
@kamurashev 4 күн бұрын
One of the only in python videos 😂
@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
@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
@ironmonkey1990
@ironmonkey1990 4 күн бұрын
thank you!
@BabakFiFoo
@BabakFiFoo 4 күн бұрын
Anthony, you are the best. Please keep up with such content!
@mCoding
@mCoding 4 күн бұрын
The performance counters in most VM software are simulated. At the moment the VM should simulate a virtual timer interrupt, it may not be running because your OS is running another thread. This can cause a backlog of timer interrupts, causing the guest OS to perceive substantially less time passage than the host, which can cause many weird behaviors, including duplicate values returned from monotonic clocks on operations you'd expect to take longer (e.g. Python code). Some VM software allows to pass through the host performance counters, look for the option "virtualize performance counters" or similar, although it requires hardware support. However, when using this option, you are now also perfing the VM software in addition to your Python code, so times would be artificially inflated, the best way to get accurate timing information is to use bare-metal dedicated hardware with nothing else running and a single CPU. Also, it's not much of an issue in Python, but you can observe monotonic clocks returning duplicate values when operations actually take less than the clock resolution amount of time to pass. E.g. if you try to benchmark malloc in C, which can in nice cases take ~20ns, you will find the same issue of duplicate monotonic clock values not because of some weird error but because not enough time has passed to increase the perf counter since the last call (it might take 200ns). The way we typically get accurate measurements of events that take less than clock resolution time is using statistics, e.g. by running the operation millions of times then dividing the time taken by a million. More advanced statistics are required when you can't simply run the operation in a loop, e.g. if you are perfing "execute trade on stock market", in which case you can still collect millions of samples of adjacent clock values just not in a loop and a proportion of them happened to be between counter increments, from which you can estimate the real time taken.
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?
@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.
@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.
@mrswats
@mrswats 4 күн бұрын
That's a dirty bug!
@jake115577
@jake115577 5 күн бұрын
This is something I should have learned when I was just starting. Thanks as always, Anthony!
@tylermfdurden
@tylermfdurden 5 күн бұрын
when you figure this stuff out, can you come fix my computer? It also has several nonsensical issues that I can't even explain.
@PeterBenjamin-KH
@PeterBenjamin-KH 6 күн бұрын
Ah, yes. It's a classic problem with the inverse reactive current used in unilateral phase detractors and synchronization of cardinal gram-meters. See, the power used to be generated by the relative motion of conductors and fluxors, but in your VM, it is likely generated by the modial interaction of magneto-reluctance and capacitive deractance. I would check the base plate of prefamulated amulyte that is surmounted by a maleable logarithmic casing and make sure the 2 sperving barings run a direct line with the parametric fan. The Rockwell Retro Encabulator YT video goes into deep detail on this.
@spencer3752
@spencer3752 6 күн бұрын
Would you be willing to change hypervisor? Can you try creating the VM (or converting your disk image to a vhd) with Hyper-V and see if it has the same behavior? Hyper-V is a type-1 hypervisor and will probably give a much better experience overall... not to mention is included with Windows. As a stab in the dark... some of the weirdest issues I've experienced have often been related to a disk partition being full. Double check all your partitions are mapped appropriately and have free space. Dynamically expanding storage can also do weird things because the machine basically stops very briefly when that occurs, which causes weird behavior sometimes.
@thiemofischer2230
@thiemofischer2230 6 күн бұрын
don't cite me on this, but for the time being: just omit the outliers.
@anthonywritescode
@anthonywritescode 6 күн бұрын
that seems like a terrible idea. if you know there's time inaccuracy then all of your data is trash
@ColopoptizPlays
@ColopoptizPlays 6 күн бұрын
Someone already mentioned the time stamp counter but maybe try removing and checking the cmos battery?
@thiemofischer2230
@thiemofischer2230 6 күн бұрын
there is no algorithm that can solve --warn-unreachable. it's the halting problem
@anthonywritescode
@anthonywritescode 6 күн бұрын
for a strict subset of the language it's fairly straightforward actually (you of course need to make some basic assumptions about various constructs but at least for the subset mypy targets it's not that unreasonable to "solve")
@thiemofischer2230
@thiemofischer2230 5 күн бұрын
​@@anthonywritescode maybe. i don't know that much about how mypy would go about it, when just looking at types. take the str vs Sequence[str] example from the other video. make an expression to check if the elements of the Sequence are all one character strings. put that in an "if !expession: pass". use --warn-unreachable on that. This should "solve" for the type exclusion (just in theory). just wondering what assumptions would go in the algorithm... straightforward enough to make my brain melt at least
@MynecraftCZ
@MynecraftCZ 6 күн бұрын
You can write a simple C program which store time to array in a loop as fast as possible and then plot it to see if there are some stairs, spikes or whatever. It can give you better understanding what is happening. You can also experiment with using clock_realtime and clock_monotonic.
@DDlol01
@DDlol01 6 күн бұрын
manual linux kernel replacement might fix it (just use the same version as installed first, then upgrade to something newer) that fixed it on an arm based system for me (a single core 400 MHz crappy softfp one, but hey, maybe) But that would have been a problem from the first boot. Firmware Update for BIOS/CPU would be better to try (mostly fixes the fixable things above). If its just in specific software idk, reinstall that software and things it depends on? custom build systemd : D (the last one was a joke, systemd is not so fun to replace, or at least it was when it became standard a decade ago) run a live cd and check times from there. If it is messed up still it might be the internal highprecission counter from the cpu it self is somewhat broken/buggy. Barely recoverable depending on the actual cause, CMOS/BIOS reset might fix that, another CPU (a crappy celeron-esque will do) can even help. there are wild suggestions online: Vaporizing the PC might do : D Would not reccomend checking that. Took me days on a laptop to figure this one out. And I could not fix it. My PC turned so slow it got to 50MHz tops for some reasons and I gave up. Usually this means your CPU is slowly turning insane. I am just taking wild guesses...
@YonatanAvhar
@YonatanAvhar 7 күн бұрын
I'd try booting a live USB to see if it happens bear metal Additionally, I'd try a different hypervisor like VMWare or MS Hyper-V
@LewisCowles
@LewisCowles 7 күн бұрын
I mean... First try it outside of a VM, and then try a different OS (windows can run in trial mode for 30-90 days)
@TheShadow06320
@TheShadow06320 7 күн бұрын
Looks like KZbin deleted my original comment (likely due to it containing the commands I used), so I am commenting again. FWIW, I cannot reproduce the 0 ns thing. My CPU is Ryzen 3900X. I tried to reproduce it in a VM (Virtualbox with the same CPU settings; with Ubuntu 18.04, Lubuntu 20.04, Ubuntu 24.04 and PopOS 22.04) and on bare metal (Debian 12). Hope this data point would be useful in figuring it out. If I had to guess the source of the problem, it would be in the part of either the NT kernel or the firmware, which is responsible for reading the real-time clock. You could try to narrow it down by testing on the host, while it is running Linux (a live usb should suffice).
@nevo408
@nevo408 7 күн бұрын
Not sure exactly how `time` is implemented under the hood. I would try another language to see if this behavior is reproducible across different implementations
@anthonywritescode
@anthonywritescode 7 күн бұрын
I'm not actually using `time` but as I've replied in a few other comments this isn't specific to python
@cs233
@cs233 7 күн бұрын
Wrong approach! Figure out how it dies things in zero nanoseconds and leave those fancy quantum machines in the dust! You’ll make a fortune!
@senyai
@senyai 7 күн бұрын
Could it be that the process is migrated to different CCD with different timer?
@antonboch1244
@antonboch1244 7 күн бұрын
AMD issue... I think.
@antonboch1244
@antonboch1244 7 күн бұрын
It's also can be an execution cache.
@noahcuroe
@noahcuroe 8 күн бұрын
This feels like either a hypervisor and/or hardware virtualization bug or an intentional decision. Since you are on Windows I would try running a tool called InSpectre and disabling side channel mitigations there. If that alone doesn't work I'd comb through your host BIOS settings and Virtualbox settings for anything related to performance counters, hardware timers, or side channel attack mitigations and start flipping switches and checking. My (kind of a stretch) theory is that the system is adding noise to system time counters to mitigate side channel attacks - this was notably employed in response to Meltdown and Spectre but there have been other side channel attacks discovered since then that may have warranted heavier handed mitigations especially in virtualization. The implementation of this mitigation will be different on Intel and AMD which could be why you can't reproduce this on Intel.
@SashikaSandeepa
@SashikaSandeepa 8 күн бұрын
just dropped another X files episode 😅
@bluekeybo
@bluekeybo 8 күн бұрын
TempleOS never had this issue, just saying
@anthonywritescode
@anthonywritescode 8 күн бұрын
I'm nearly certain templeos will have the same behaviour -- will try it when I can figure out how to make a vm of it
@wenbozhao4325
@wenbozhao4325 8 күн бұрын
What if change the best-of script to a bash loop so it's at least not related to python at all and see if it's reproduced
@anthonywritescode
@anthonywritescode 8 күн бұрын
it reproduces with C so it's definitely not python at fault
@MelroyvandenBerg
@MelroyvandenBerg 8 күн бұрын
Yeah try set the vm to use directly the host cpu. And try also kvm.
@anthonywritescode
@anthonywritescode 8 күн бұрын
what does that mean?
@MelroyvandenBerg
@MelroyvandenBerg 7 күн бұрын
​@@anthonywritescode Well there are different hypervisors like Promox for example that uses QEMU/KVM kernel modules. You could check if for example Qemu is getting different results. I also notice a Virtualbox setting at: System -> Acceleration -> Para-virtualization interface to select KVM instead. Regarding the CPU type as host, I'm not fully sure if Virtualbox has this same feature as Proxmox (that is using KVM). I do can execute: VBoxManage list vms. And set the cpu profile host: VBoxManage modifyvm "{some-hex-value}" --cpu-profile host. But this seems already be the default. And again, maybe not the same as KVM CPU host type (which will not virtualize the CPU to the VM).
@anthonywritescode
@anthonywritescode 7 күн бұрын
kvm seems to be the default for Linux guests
@that_guy_0699
@that_guy_0699 8 күн бұрын
I know exactly what the problem is! Is what I would say, if I would know what the problem is. However, I have no idea.
@anthonywritescode
@anthonywritescode 8 күн бұрын
genius!
@sethdhanson
@sethdhanson 8 күн бұрын
Is it plugged in?
@anthonywritescode
@anthonywritescode 8 күн бұрын
ah shoot ya got me there
@leggysoft
@leggysoft 8 күн бұрын
I've seen failing clocks give the same value more than once so time is not stopping it's just repeating the time. This can cause drift, if no drift it's being stupid but the clock itself is working.
@anthonywritescode
@anthonywritescode 8 күн бұрын
what
@adminisnyator
@adminisnyator 8 күн бұрын
Really hope this is not going to be one of those weird Intel issues
@anthonywritescode
@anthonywritescode 8 күн бұрын
see that's the thing it's only broken on amd :'(
@Person1873
@Person1873 8 күн бұрын
Have you tried under different hypervisors? I believe virtualbox does some amount of CPU result caching which could potentially explain this.
@anthonywritescode
@anthonywritescode 8 күн бұрын
I haven't but vbox is working fine everywhere else with identical settings
@anathaetownsend1894
@anathaetownsend1894 8 күн бұрын
@@anthonywritescode There may be an interaction between the Virtual Box hypervisor and the AMD processor. If HyperV, or VMWare Workstation Pro reproduce the problem on that hardware, then hardware is involved in some way, if you can't replicate under a different hypervisor, then there is an edge condition that virtual box exposes somehow that the others do not. If you can get a chance to try with a live USB using Linux and KVM.
@berndeckenfels
@berndeckenfels 8 күн бұрын
Did you try to pin the cores to a single physical core (or package) or numa zone? What’s the host? I think perfcountere are per cpu - so is that a dual socket?
@anthonywritescode
@anthonywritescode 8 күн бұрын
how would one do that? host is windows and it's a single cpu -- ryzen 9 7950x
@xspager
@xspager 8 күн бұрын
Maybe try disabling the "Extended features"
@xspager
@xspager 8 күн бұрын
Also you could try to change System > Acceleration > Paravirtualization interface to Legacy or Minimal and see if it changes anything.
@anthonywritescode
@anthonywritescode 8 күн бұрын
hmmm you might be on to something -- will try when I am back from the wilderness
@K9Megahertz
@K9Megahertz 8 күн бұрын
It's cause you have that malware python installed. =)
@anthonywritescode
@anthonywritescode 8 күн бұрын
it reproduces in the holy C as well
@K9Megahertz
@K9Megahertz 8 күн бұрын
@@anthonywritescode Weird issue for sure, yeah I thought it might just be some python shenanigans but if C is doing it as well then I feel it's definitely in the virtualization layer. Does adding a delay in between the time calls prevent the issue from happening? I wouldn't use sleep but maybe just eat up some cycles, or hell, just try sleep as well maybe with 1ms.
@anthonywritescode
@anthonywritescode 7 күн бұрын
it's running a subprocess in between (literally thousands and thousands of instructions)
@K9Megahertz
@K9Megahertz 7 күн бұрын
@@anthonywritescode Oh, I do see that, I was just curious how far you could push that. Would you still get 0.000 with a 5 second delay in between?
@K9Megahertz
@K9Megahertz 7 күн бұрын
@@anthonywritescode I posted a reply to this but I guess YT censored it.... No, I understand you have something there taking up cycles. I guess I was suggesting to increase the time between the calls. What if you add a 5 second delay? Still 0s?
@hambroman
@hambroman 8 күн бұрын
Does it happen on a newly created VM? No idea if this is even a thing, but wonder if there's something host-specific baked in from the prior machine you built this image on?
@anthonywritescode
@anthonywritescode 8 күн бұрын
yep tried it in a fresh vm as well
@hambroman
@hambroman 8 күн бұрын
@anthonywritescode well dang