VidGear: Video processing library for Python lovers
github.com
github.com
# check if frame is None
if frame_stab is None:
#if True break the infinite loop
break
A single comment saying "continue if stream ends" would be much clearerIf the project holds up, it's definitely a teaching moment for me.
The weirdness you mention can be explained, I think, by the fact that the project has a single contributor.[1]
A person learns which patterns work or not when patterns clearly work or fail. Since bad comments are something that affects maintenance without breaking the code, it wouldn't be surprising if the internals are solid while the code is less maintainable.
And I predict that if it takes off, you'd see other contributors pushing back against the extraneous comments, and Abhishek's style in commenting evolve.
[1]: https://github.com/abhiTronix/vidgear/graphs/contributors
It's actually super simple to write a wrapper around it that lets you do:
for frame in video:
# your processing comes here and you forget
# having to check for None, while True s and things
# that should've stayed in the stone age
And I'm a little disappointed the posted library doesn't do it. for frame_stab, frame_org in zip(iter(stream_stab.read, None), iter(stream_org.read, None)):
...
This line replaces the first 12 lines of the loop in the example. while (frame_stab := stream_stab.read()) and (stream_org := stream_org.read()):
iter() works with all stable versions of Python though, but it's a bit exotic. When I use it, I tend to make aliases to make it clearer because most Python devs don't know about it, let alone this alternative form. stab_frames = iter(stream_stab.read, None)
org_frames = iter(stream_org.read, None)
for frame_stab, frame_org in zip(stab_frames, org_frames): stab_frames = iter(stream_stab.read, None)
org_frames = iter(stream_org.read, None)
for frame_stab, frame_org in zip(stab_frames, org_frames):
wow, I did'nt knew about this. Thanks for posting. I'm definetly adding this in the next release.[1]: https://github.com/abhiTronix/vidgear/issues/new/choose