Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #103341
| 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 | <mailman.40.1456148110.20994.python-list@python.org> (permalink) |
| References | <e5e4a934-4eeb-46ed-892f-cda9e903c1cd@googlegroups.com> |
| 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 | <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.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 <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> |
| Xref | csiph.com comp.lang.python:103341 |
Show key headers only | View raw
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.
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
avoid for loop calling Generator function Arshpreet Singh <arsh840@gmail.com> - 2016-02-22 03:15 -0800
Re: avoid for loop calling Generator function Peter Otten <__peter__@web.de> - 2016-02-22 14:34 +0100
Re: avoid for loop calling Generator function Arshpreet Singh <arsh840@gmail.com> - 2016-02-22 07:38 -0800
Re: avoid for loop calling Generator function Chris Angelico <rosuav@gmail.com> - 2016-02-23 02:46 +1100
Re: avoid for loop calling Generator function Ian Kelly <ian.g.kelly@gmail.com> - 2016-02-22 09:11 -0700
csiph-web