Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #68371

Re: Sharing: File Reader Generator with & w/o Policy

Date 2014-03-15 21:56 +0000
From MRAB <python@mrabarnett.plus.com>
Subject Re: Sharing: File Reader Generator with & w/o Policy
References <lg2h87$6fj$1@speranza.aioe.org>
Newsgroups comp.lang.python
Message-ID <mailman.8156.1394920773.18130.python-list@python.org> (permalink)

Show all headers | View raw


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]

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Sharing: File Reader Generator  with & w/o Policy Mark H Harris <harrismh777@gmail.com> - 2014-03-15 16:38 -0500
  Re: Sharing: File Reader Generator  with & w/o Policy MRAB <python@mrabarnett.plus.com> - 2014-03-15 21:56 +0000
    Re: Sharing: File Reader Generator  with & w/o Policy Mark H Harris <harrismh777@gmail.com> - 2014-03-15 19:36 -0500
    Re: Sharing: File Reader Generator  with & w/o Policy Mark H Harris <harrismh777@gmail.com> - 2014-03-15 19:45 -0500
    Re: Sharing: File Reader Generator  with & w/o Policy Mark H Harris <harrismh777@gmail.com> - 2014-03-15 20:06 -0500
      Re: Sharing: File Reader Generator  with & w/o Policy Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-03-16 01:32 +0000
        Re: Sharing: File Reader Generator  with & w/o Policy Mark H Harris <harrismh777@gmail.com> - 2014-03-15 21:52 -0500
  Re: Sharing: File Reader Generator  with & w/o Policy Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-03-16 02:01 +0000
    Re: Sharing: File Reader Generator  with & w/o Policy Mark H Harris <harrismh777@gmail.com> - 2014-03-15 22:34 -0500
      Re: Sharing: File Reader Generator with & w/o Policy Chris Angelico <rosuav@gmail.com> - 2014-03-16 14:48 +1100
        Re: Sharing: File Reader Generator with & w/o Policy Mark H Harris <harrismh777@gmail.com> - 2014-03-15 23:47 -0500
          Re: Sharing: File Reader Generator with & w/o Policy Chris Angelico <rosuav@gmail.com> - 2014-03-16 16:41 +1100
            Re: Sharing: File Reader Generator with & w/o Policy Mark H Harris <harrismh777@gmail.com> - 2014-03-16 01:19 -0500
              Re: Sharing: File Reader Generator with & w/o Policy Chris Angelico <rosuav@gmail.com> - 2014-03-16 17:37 +1100
          Re: Sharing: File Reader Generator with & w/o Policy Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-03-16 10:36 +0000

csiph-web