5 ms·
Why do you think fileinput is weird?
by mftrhu 7y ago
Why 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!
- simcop2387 7y agoActually with perl its even easier. perl -ne '... Process lines...'
- fanf2 7y agoThere’s a big risk with using <> in perl, if any of the command line arguments might come from somewhere untrusted - if you use perl it’s worth reviewing the discussion under https://perldoc.perl.org/perlop.html#I%2fO-Operators https://perldoc.perl.org/perlop.html#I%2fO-Operators
- anoncake 7y agoYou depend on the standard library anyway.
- jstimpfle 7y agoSo using the library doesn't have a cost?
- Liquid_Fire 7y agoThe cost to importing an extra module is trivial. Why reinvent the wheel, when Python ships with an already written and tested solution? The 3 lines of code you would write are probably going to miss some corner case. If I'm reading some unfamiliar code I would rather see one use of a standard library function call than several lines that I have to read and understand. And if it was really the first time I came across the `fileinput` module, I only have to pay the cost of reading the documentation once, and then forever benefit from having to read and understand less code whenever it is used.
- daveFNbuck 7y agoIt was on the PEP's original list of things to remove, but they decided to keep it because it's handy for quick scripts. I use it all the time for scripts that I delete shortly after use.
- jteppinette 7y agoI found 330k references on GitHub.
- deleted 7y ago[deleted]
- jstimpfle 7y agoThey all look the same. https://github.com/search?l=Python&q=fileinput&type=Code https://github.com/search?l=Python&q=fileinput&type=Code
- aepiepaey 7y agoThat's because it includes all the repos with Python forks/copies. Here's a better search, still 100k+: https://github.com/search?l=Python&q=%22import+fileinput%22&type=Code https://github.com/search?l=Python&q=%22import+fileinput%22&...