Sharing: File Reader Generator with & w/o Policy

MRAB python at mrabarnett.plus.com
Sat Mar 15 22:56:27 CET 2014


On 2014-03-15 21:38, Mark H Harris wrote:
> hi folks, I am posting to share a File Reader Generator which I have
> been playing with, that simplifies reading of text files on-demand:
> like log files, config files, small record flat data-bases, &c.
>
> I have two generators to share, one with & one without "policy".
> The idea is to have the generator open and close the file (with error
> checking:  try-finish block) and then maintain its state for on-demand
> reading either into memory (as list or dict) or for in-line processing.
>
> I will demonstrate the generators here, and then post the code
> following. The generator will be reading a path+filename of a local disk
> file and printing it as in this simple case without policy:
>   >>> from my_utils import *
>
>   >>> for record in fName(path+"my_fox"):
> 	      print(record)
>
> The quick brown fox jumped
> over the lazy dog's tail.
>
> Now is the time for all
> good women to come to the
> aid of computer science!
>   >>>
>
> The second generator adds "policy" to the generator processing and
> yields tuples, rather than strings. Each tuple contains the record
> number (from zero), and record length (minus the line end), and the
> record itself (stripped of the line end):
>   >>>
>   >>> for record in fnName(path+"my_fox"):
> 	      print(record)
>
> (0, 26, 'The quick brown fox jumped')
> (1, 25, "over the lazy dog's tail.")
> (2, 0, '')
> (3, 23, 'Now is the time for all')
> (4, 25, 'good women to come to the')
> (5, 24, 'aid of computer science!')
>   >>>
>   >>>
>
> I will now share the source by allowing the fName(filename) utility
> to expose itself.  Enjoy:
>   >>>
>   >>> for record in fName(path+"my_utils.py"):
> 	      print(record)
>
> #---------------------------------------------------------
> # fName(filename)   generator: file reader iterable
> #---------------------------------------------------------
> def fName(filename):
>       try:
>           fh = open(filename, 'r')
>       except FileNotFoundError as err_code:
>           print (err_code)
>       else:
>           while True:
>               linein = fh.readline()
>               if (linein!=''):
>                   yield(linein.strip('\n'))
>               else:
>                   break
>           fh.close()
>       finally:
>           None
>
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.

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.

You can also shorten it somewhat:

def fName(filename):
     try:
         with open(filename, 'r') as fh:
             for linein in fh:
                 yield linein.strip('\n')
     except FileNotFoundError as err_code:
         print(err_code)

[snip]




More information about the Python-list mailing list