Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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...



Especially when you start looking at the code for some of these

https://github.com/python/cpython/blob/master/Lib/imghdr.py


To 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.


Ruby includes something similar, ARGF. It's pretty useful over there. https://thoughtbot.com/blog/rubys-argf


Well, 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?


Why do you think fileinput is weird?


The 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.


I 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?


Not 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!


Actually with perl its even easier. perl -ne '... Process lines...'


There’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


It 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.


Filters over lines of text may be domain-specific, but it sure as hell spans a lot of domains.


Idk, 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.


It 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).


> 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.


You depend on the standard library anyway.


So using the library doesn't have a cost?


The 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.


I found 330k references on GitHub.



That'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&...




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: