Sharing: File Reader Generator with & w/o Policy

Mark H Harris harrismh777 at gmail.com
Sun Mar 16 02:19:52 EDT 2014


On 3/16/14 12:41 AM, Chris Angelico wrote:
>

    Good stuff Chris, and thanks for the footnotes, I appreciate it.

> If getline() is doing nothing that the primitive doesn't, and
> getnumline is just enumerate, then they're not achieving anything
> beyond shielding you from the primitives.
>

    Yes.  getline(fn) is returning the raw line minus the newline \n. 
And getnumline(fn) is 1) creating a name that is easily recognizable, 
and 2) shielding the 'user' from the primitives; yup.

> The trouble is that your idea of getnumline(file) might well differ
> from someone else's idea of getnumline(file). Using Python's
> primitives removes that confusion

    I am seeing that; esp for folks used to seeing the primitives; don't 
want confusion.

> To be quite frank, yes I do think it's a nutty idea. Like most nutty
> things, there's a kernel of something good in it, but that's not
> enough to build a system on :)

    Thanks for your candor. I appreciate that too. Well, like I said, 
I'm just experimenting with the idea right now, just playing around 
really. In the process I'm coming more up-to-speed with python3.3 all 
the time.   :)

>
> Python is already pretty simple.

    statement == True

>
> We had a discussion along these lines a little while ago, about
> designing a DSL [1] for window creation. On one side of the debate was
> "hey look how much cleaner the code is if I use this DSL", and on the
> other side was "hey look how much work you don't have to do if you
> just write code directly".

    Was that on python-dev, or python-ideas, or here?   I'd like to read 
through it sometime.

Well just for grins, here is the updated my_utils.py for compare with 
where I started tonight, ending like before with the code:

 >>>
 >>> for line in getline(path+"my_foxy"):
	print (line)

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!

 >>> for line in getnumline(path+"my_foxy"):
	print (line)
	
(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!')

 >>> for line in getline(path+"my_utils.py"):
	print (line)

#---------------------------------------------------------
# __fOpen__(filename)   generator: file open internal
#---------------------------------------------------------
def __fOpen__(filename):
     try:
         with open(filename, 'r') as fh:
             for linein in fh:
                 yield linein.strip('\n')
     except FileNotFoundError as err_code:
         print(err_code)
         # think about error handling, logging
     finally:
         pass

#---------------------------------------------------------
# getnumline(filename)  generator: enumerated file reader
#---------------------------------------------------------
def getnumline(filename):
     for count, line in enumerate(__fOpen__(filename)):
         yield((count, len(line), line))

#---------------------------------------------------------
# getline(filename)   generator: raw file reader iterable
#---------------------------------------------------------
def getline(filename):
     for line in __fOpen__(filename):
         yield(line)

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



More information about the Python-list mailing list