Sharing: File Reader Generator with & w/o Policy

Mark H Harris harrismh777 at gmail.com
Sat Mar 15 17:38:18 EDT 2014


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

#---------------------------------------------------------
# fnName(filename)   generator: file reader iterable
#---------------------------------------------------------
def fnName(filename):
     try:
         fh = open(filename, 'r')
     except FileNotFoundError as err_code:
         print (err_code)
     else:
         line_count = 0
         while True:
             linein = fh.readline()
             if (linein!=''):
                 lineout = linein.strip('\n')
                 length = len(lineout)
                 yield((line_count, length, lineout))
                 line_count+=1
             else:
                 break
         fh.close()
     finally:
         None

#---------------------------------------------------------
# {next util}
#---------------------------------------------------------
 >>>

mark h harris



More information about the Python-list mailing list