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


Groups > comp.lang.python > #26248

Re: Linux shell to python

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 <phihag@phihag.de>
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 <phihag@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 "Jürgen A. Erhard" <jae+python@jaerhard.com>
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 <python-list.python.org>
List-Unsubscribe <http://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 <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.2727.1343649567.4697.python-list@python.org> (permalink)
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

Show key headers only | View raw


[Multipart message — attachments visible in raw view] - view raw

On 07/30/2012 01:31 PM, Jürgen 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
>> ]
> 
> 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=['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 = subprocess.check_output(['lspci']).splitlines()
lines = [l for l in lines if 'Q' in l and isp_str1 in l and isp_str2 in l]
# Or:
lines = 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


Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Linux shell to python Philipp Hagemeister <phihag@phihag.de> - 2012-07-30 13:59 +0200

csiph-web