Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #26248 > unrolled thread
| Started by | Philipp Hagemeister <phihag@phihag.de> |
|---|---|
| First post | 2012-07-30 13:59 +0200 |
| Last post | 2012-07-30 13:59 +0200 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: Linux shell to python Philipp Hagemeister <phihag@phihag.de> - 2012-07-30 13:59 +0200
| From | Philipp Hagemeister <phihag@phihag.de> |
|---|---|
| Date | 2012-07-30 13:59 +0200 |
| Subject | Re: Linux shell to python |
| Message-ID | <mailman.2727.1343649567.4697.python-list@python.org> |
[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 top | Article view | comp.lang.python
csiph-web