Sharing: File Reader Generator with & w/o Policy
Mark H Harris
harrismh777 at gmail.com
Sun Mar 16 01:36:21 CET 2014
On 3/15/14 4:56 PM, MRAB wrote:
> I don't like how it always swallows the exception, so you can't tell
> whether the file doesn't exist or exists but is empty, and no way to
> specify the file's encoding.
Yes, the error handling needs more robustness/ and instead of printing
the errcode, my actual model on system will log it.
> Why do you have the 'finally' clause with 'None' in it? Instead of None
> you should have 'pass', or, better yet, omit the clause entirely.
Its a stub-in really, and that's all at this point. The 'finally'
happens regardless of whether the exception occurs, and I don't need
anything there yet, just don't want to forget it.
I've been playing around with wrapping generators within generators for
readability and simplicity. Like this, where I'm going to wrap the
fnName(filename) generator within a getnumline(filename) wrapper:
>>> from my_utils import *
>>> def getnumline(filename):
for record in fnName(filename):
>>> line = getnumline("my_foxy")
(0, 26, 'The quick brown fox jumped')
(1, 25, "over the lazy dog's tail.")
Or this, where I put it all in memory as a dict:
>>> for line in getnumline("my_foxy"):
>>> for key in d1:
(26, 'The quick brown fox jumped')
(25, "over the lazy dog's tail.")
(23, 'Now is the time for all')
(25, 'good women to come to the')
(24, 'aid of computer science!')
More information about the Python-list