[Patches] fileinput.py argument handling (and suggestion)
Greg Ward
gward@mems-exchange.org
Mon, 10 Apr 2000 09:20:56 -0400
On 08 April 2000, Greg Stein said:
> I might go one further, and ask for a way to *strip* the line, rather than
> just take the newline off the end. I can't think of a clean way to do so.
> For example:
>
> fileinput.input()
> fileinput.input(strip=fileinput.STRIP_NL)
> fileinput.input(strip=fileinput.STRIP_ALL)
>
> It just seems a bit wordy, especially compared to the "chop=1" thing.
See the distutils.text_file module for a rather overblown approach to
this problem: it takes care of stripping newlines, deleting blank lines,
stripping comments, trimming whitespace (leading, trailing, or both),
collapsing lines joined with trailing backslashes, and maybe a few other
things. All features are optional. The code is a bit hairy, and I make
no guarantees about performance (except that it probably sucks). But it
works, and I never have to write all that damn logic again.
I'd vote for keeping fileinput.py relatively simple (adding chop sounds
like a good idea, but anything else is overkill), and putting the
complicated logic in a higher-level module like my text_file.py. Or
maybe in a higher-level class also provided by fileinput.py. Whatever.
Greg
--
Greg Ward - software developer gward@mems-exchange.org
MEMS Exchange / CNRI voice: +1-703-262-5376
Reston, Virginia, USA fax: +1-703-262-5367