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


Groups > comp.lang.python > #68375

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

Path csiph.com!usenet.pasdenom.info!gegeweb.org!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.003
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'from:addr:yahoo.co.uk': 0.04; 'mrab': 0.05; 'filename': 0.09; 'lawrence': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'try:': 0.09; 'def': 0.12; 'language.': 0.14; 'doing,': 0.16; 'iterable': 0.16; 'length,': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'shorten': 0.16; 'subject:File': 0.16; 'language': 0.16; 'thanks,': 0.17; 'wrote:': 0.18; 'header :User-Agent:1': 0.23; '---': 0.24; 'subject:/': 0.26; '(for': 0.26; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'errors': 0.30; 'file': 0.32; 'url:python': 0.33; 'reader': 0.33; 'subject:with': 0.35; 'except': 0.35; 'thanks': 0.36; 'url:org': 0.36; 'url:library': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'free': 0.61; 'length': 0.61; 'subject: ': 0.61; 'url:3': 0.61; 'viruses': 0.61; 'protection': 0.63; 'our': 0.64; 'occur': 0.65; 'here': 0.66; 'close': 0.67; 'antivirus': 0.68; 'subject: & ': 0.68
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Mark Lawrence <breamoreboy@yahoo.co.uk>
Subject Re: Sharing: File Reader Generator with & w/o Policy
Date Sun, 16 Mar 2014 01:32:10 +0000
References <lg2h87$6fj$1@speranza.aioe.org> <mailman.8156.1394920773.18130.python-list@python.org> <lg2te7$oq$1@speranza.aioe.org>
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host host-78-147-22-16.as13285.net
User-Agent Mozilla/5.0 (Windows NT 6.1; rv:24.0) Gecko/20100101 Thunderbird/24.3.0
In-Reply-To <lg2te7$oq$1@speranza.aioe.org>
X-Antivirus avast! (VPS 140315-1, 15/03/2014), Outbound message
X-Antivirus-Status Clean
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.8157.1394933542.18130.python-list@python.org> (permalink)
Lines 47
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1394933542 news.xs4all.nl 2857 [2001:888:2000:d::a6]:41795
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:68375

Show key headers only | View raw


On 16/03/2014 01:06, Mark H Harris wrote:
> On 3/15/14 4:56 PM, MRAB wrote:
>
>>
>> You can also shorten it somewhat:
>
> Thanks, I like it... I shortened the fnName() also:
>
> #---------------------------------------------------------
> # fn2Name(filename)   generator: file reader iterable
> #---------------------------------------------------------
> def fn2Name(filename):
>      try:
>          with open(filename, 'r') as fh:    <=========== can you tell me
>              line_count = 0
>              for linein in fh:
>                  lineout = linein.strip('\n')
>                  length = len(lineout)
>                  yield((line_count, length, lineout))
>                  line_count+=1
>      except FileNotFoundError as err_code:
>          print(err_code)
>
> #---------------------------------------------------------
>
>
> ...  where I can go to find out (for specific contexts) what the
> __init__() and __exit__() are actually doing, like for instance in this
> case does the filename get closed in __exit__(), and also if errors
> occur does the file close automatically?  thanks
>
> marcus

Start here 
http://docs.python.org/3/library/stdtypes.html#context-manager-types

-- 
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.

Mark Lawrence

---
This email is free from viruses and malware because avast! Antivirus protection is active.
http://www.avast.com

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