Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion Linux shell to python

Received: by 10.180.105.38 with SMTP id gj6mr299955wib.0.1343910325897;
        Thu, 02 Aug 2012 05:25:25 -0700 (PDT)
Received: by 10.52.33.169 with SMTP id s9mr1334547vdi.5.1343910247652;
        Thu, 02 Aug 2012 05:24:07 -0700 (PDT)
Path: n2ni4546127win.0!nntp.google.com!7no4372471wig.0!news-out.google.com!a15ni1807qag.0!nntp.google.com!npeer03.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!border3.nntp.dca.giganews.com!border1.nntp.dca.giganews.com!border4.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!border5.newsrouter.astraweb.com!news.astraweb.com!border2.a.newsrouter.astraweb.com!newsfeed10.multikabel.net!multikabel.net!newsfeed20.multikabel.net!goblin3!goblin.stu.neva.ru!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path: <phi...@phihag.de>
X-Original-To: python-l...@python.org
Delivered-To: python-l...@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 <phi...@phihag.de>
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==?= <jae+pyt...@jaerhard.com>
Subject: Re: Linux shell to python
References: <1343631941.7199.YahooMail...@web193104.mail.sg3.yahoo.com>
	<5016637A.7060...@phihag.de> <20120730113130.GA7...@jaerhard.com>
In-Reply-To: <20120730113130.GA7...@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-l...@python.org
X-BeenThere: python-l...@python.org
X-Mailman-Version: 2.1.12
Precedence: list
List-Id: General discussion list for the Python programming language
	<python-list.python.org>
List-Unsubscribe: <http://mail.python.org/mailman/options/python-list>,
	<mailto:python-list-requ...@python.org?subject=unsubscribe>
List-Archive: <http://mail.python.org/pipermail/python-list>
List-Post: <mailto:python-l...@python.org>
List-Help: <mailto:python-list-requ...@python.org?subject=help>
List-Subscribe: <http://mail.python.org/mailman/listinfo/python-list>,
	<mailto:python-list-requ...@python.org?subject=subscribe>
Newsgroups: comp.lang.python
Message-ID: <mailman.2727.1343649567.4697.python-l...@python.org>
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: ab...@xs4all.nl
Bytes: 5089
X-Received-Bytes: 5197

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--