Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: Linux shell to python

Reply
Thread Tools

Re: Linux shell to python

 
 
Philipp Hagemeister
Guest
Posts: n/a
 
      07-30-2012
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 inl]
# 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



-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.12 (GNU/Linux)

iEYEAREKAAYFAlAWdxUACgkQ9eq1gvr7CFxjRACfZ42cn6fVy9 EIxX+eq+TMXHHD
o7YAoLLWqX1zNkx2k8c+0w1vo/QI5vhq
=f5yw
-----END PGP SIGNATURE-----

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash shell to Python shell? edwithad Python 4 09-28-2009 07:56 AM
Re: How to pass shell variable to shell script from python D'Arcy J.M. Cain Python 0 02-27-2008 01:56 PM
Re: How to pass shell variable to shell script from python Gerardo Herzig Python 1 02-27-2008 12:19 PM
Re: How to pass shell variable to shell script from python Christian Heimes Python 0 02-27-2008 10:53 AM
can I run unix shell command in the ModelSim shell? clinton__bill@hotmail.com VHDL 2 02-18-2005 10:04 PM



Advertisments