6 ms·
https://docs.python.org/3/library/fileinput.html https://docs.python.org/3/library/fileinput.html I've never had a strong opinion about "batteries included", b
by jstimpfle 7y ago
https://docs.python.org/3/library/fileinput.html https://docs.python.org/3/library/fileinput.html
I've never had a strong opinion about "batteries included", but boy there are some weird ones in there...
- ehsankia 7y agoEspecially when you start looking at the code for some of these https://github.com/python/cpython/blob/master/Lib/imghdr.py https://github.com/python/cpython/blob/master/Lib/imghdr.py
- Znafon 7y agoTo be fair, it dates from 1992. I don't think PyPi existed at the time, it's rather impressive the Core team supported this for so long.
- tomjakubowski 7y agoRuby includes something similar, ARGF. It's pretty useful over there. https://thoughtbot.com/blog/rubys-argf https://thoughtbot.com/blog/rubys-argf
- jstimpfle 7y agoWell, maybe fileinput is meant to be used for making shell one-liners using the -m switch or so, like in the link you posted, or like perl/awk. Does that play nice with Python's block syntax, though?
- mftrhu 7y agoWhy do you think fileinput is weird?
- jstimpfle 7y agoThe functionality is almost less than trivial. It's like, trading 3 lines of super straightforward code for one line of import statement and a library dependency. Pretty sure it tries to emulate awk's programming model, but even if I had a use for that I'd rather write these 3 lines of code myself - so the code is actually clear without looking at documentation, and so that it can be modified easily.
- naniwaduni 7y agoFilters over lines of text may be domain-specific, but it sure as hell spans a lot of domains.
- jstimpfle 7y agoIdk, it never occurred to me the need for a library that does more than "for line in sys.stdin:" but less than what you can easily write in under a minute.
- rabidrat 7y agoIt handles files being passed on the command line too. Your particular usage might only take a minute, but to make it play nice with other expected CLI usages, takes more thought (and you will probably forget something).
- masklinn 7y ago> Idk, it never occurred to me the need for a library that does more than "for line in sys.stdin:" Of course if you'd only used fileinput for that it would be worthless. However what it does is way more useful, namely iterate on the lines of all files provided as parameter (or sys.argv[:1]), fallback to sys.stdin if no files were provided, and swap the special sentinel `-` by sys.stdin.
- bpicolo 7y agoI actually really like fileinput - to me it's a great example of the good kind of batteries included. I appreciate easy built-in utilities for super common programming tasks! Those are great to have. Different strokes I suppose?
- pjungwir 7y agoNot just awk but cat, grep, etc.---even python itself. It's nice to have something that "recommends" such a common Unix convention, a lot like getopt but moreso. In Perl this would be `while (<>) { ... }` and in Ruby `ARGF.each_line do |line| ... end`. I'm glad they are keeping it!