Path: csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: 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; 'function,': 0.07; 'verbatim': 0.07; 'processing,': 0.09; 'cc:addr:python-list': 0.10; 'def': 0.10; 'subject:python': 0.11; "'q'": 0.16; '*you*': 0.16; 'code?': 0.16; 'filename:fname piece:signature': 0.16; 'personally,': 0.16; 'subprocess': 0.16; 'mon,': 0.16; 'wrote:': 0.17; 'yield': 0.17; 'import': 0.21; 'cc:2**0': 0.23; 'this:': 0.23; 'cc:no real name:2**0': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'skip:[ 10': 0.26; 'separate': 0.27; 'lines': 0.28; 'function': 0.30; 'code': 0.31; 'could': 0.32; '+0200,': 0.33; 'point,': 0.33; 'version:': 0.33; 'problem': 0.33; 'version': 0.34; 'received:192.168.2': 0.34; 'list': 0.35; 'pm,': 0.35; 'but': 0.36; "didn't": 0.36; 'skip:p 20': 0.36; 'bad': 0.37; 'why': 0.37; 'subject:: ': 0.38; 'received:192': 0.39; 'received:192.168': 0.40; '30,': 0.62; 'more': 0.63; 'jul': 0.65; 'introduce': 0.80 Date: Mon, 30 Jul 2012 13:59:15 +0200 From: Philipp Hagemeister User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:10.0.5) Gecko/20120624 Icedove/10.0.5 MIME-Version: 1.0 To: =?UTF-8?B?IkrDvHJnZW4gQS4gRXJoYXJkIg==?= Subject: Re: Linux shell to python References: <1343631941.7199.YahooMailNeo@web193104.mail.sg3.yahoo.com> <5016637A.7060008@phihag.de> <20120730113130.GA7968@jaerhard.com> In-Reply-To: <20120730113130.GA7968@jaerhard.com> X-Enigmail-Version: 1.4 OpenPGP: id=FAFB085C Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="------------enig765055BBD1AAAAA78BCD577A" Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 62 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1343649567 news.xs4all.nl 6896 [2001:888:2000:d::a6]:46153 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:26248 This is an OpenPGP/MIME signed message (RFC 2440 and 3156) --------------enig765055BBD1AAAAA78BCD577A Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable On 07/30/2012 01:31 PM, J=C3=BCrgen A. Erhard wrote: > On Mon, Jul 30, 2012 at 12:35:38PM +0200, Philipp Hagemeister wrote: >> import subprocess >> [ l.partition(' ')[0] # or l[:7], if you want to copy it verbatim= >> for l in subprocess.check_output(['lspci']).splitlines() >> if 'Q' in l and isp_str1 in l and isp_str2 in l >> ] >=20 > Ouch. A list comprehension spanning more than one line is bad code > pretty much every time. I didn't want to introduce a separate function, but as requested, here's the function version: def pciIds(searchWords=3D['Q', isp_str1, isp_str2]): for l in subprocess.check_output(['lspci']).splitlines(): if all(sw in l for sw in searchWords): yield l.partition(' ')[0] You could also separate the processing, like this: lines =3D subprocess.check_output(['lspci']).splitlines() lines =3D [l for l in lines if 'Q' in l and isp_str1 in l and isp_str2 in= l] # Or: lines =3D filter(lambda l: 'Q' in l and isp_str1 in l and isp_str2 in l, lines) [l.partition(' ')[0] for l in lines] # Or: map(lambda l: l.partition(' ')[0], lines) But personally, I have no problem with three-line list comprehensions. Can you elaborate why the list comprehension version is bad code? Or more to the point, how would *you* write it? - Philipp --------------enig765055BBD1AAAAA78BCD577A Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.12 (GNU/Linux) iEYEAREKAAYFAlAWdxUACgkQ9eq1gvr7CFxjRACfZ42cn6fVy9EIxX+eq+TMXHHD o7YAoLLWqX1zNkx2k8c+0w1vo/QI5vhq =f5yw -----END PGP SIGNATURE----- --------------enig765055BBD1AAAAA78BCD577A--