Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Peter Otten <__peter__@web.de> Newsgroups: comp.lang.python Subject: Re: avoid for loop calling Generator function Date: Mon, 22 Feb 2016 14:34:08 +0100 Organization: None Lines: 40 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Trace: news.uni-berlin.de smBFU9m0o1xGyhV9XbmVFAn6f6sTQW9lnRFjaDH5AQew== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.007 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'none)': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'def': 0.13; 'file,': 0.15; 'demonstrable': 0.16; 'personally,': 0.16; 'readability.': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'received:t-ipconnect.de': 0.16; 'wrote:': 0.16; 'solution.': 0.18; 'code.': 0.23; 'slightly': 0.23; 'tried': 0.24; 'import': 0.24; 'header:User-Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'converting': 0.27; 'yield': 0.27; 'function': 0.28; 'another': 0.32; 'recommended': 0.34; 'text': 0.35; 'replace': 0.35; 'but': 0.36; 'possible': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'missing': 0.37; 'pdf': 0.37; 'skip:s 40': 0.38; 'skip:p 20': 0.38; 'hi,': 0.38; 'to:addr:python.org': 0.40; 'where': 0.40; 'received:de': 0.40; 'avoid': 0.61; 'real': 0.62; 'skip:n 10': 0.62; 'here': 0.66; 'reserve': 0.67; 'therefore': 0.67; 'picture': 0.70; 'obvious': 0.76 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: p57bd8af8.dip0.t-ipconnect.de User-Agent: KNode/4.13.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21rc2 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:103341 Arshpreet Singh wrote: > Hi, I am converting PDF into text file, I am using following code. > > from pypdf2 import PdfFileReader > > def read_pdf(pdfFileName): > > pdf = PdfFileReader(pdfFileName) > > yield from (pg.extractText() for pg in pdf.pages) > > for i in read_pdf('book.pdf'): > print(i) > > I want to avoid for loop , I also tried to create another function and > call read_pdf() inside that new function using yield from but I think I am > missing real picture here While it is possible to replace the loop with next(filter(print, read_pdf("book.pdf")), None) or the slightly less convoluted sys.stdout.writelines(map("{}\n".format, read_pdf("book.pdf"))) the for loop is the obvious and therefore recommended solution. Personally, I would also replace > yield from (pg.extractText() for pg in pdf.pages) with the good old for pg in pdf.pages: yield pg.extractText() and reserve the generator expression for occasions where it has a demonstrable advantage in readability.