Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

Linux shell to python

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


vikas.choudhary at yahoo

Jul 30, 2012, 12:05 AM

Post #1 of 18 (1089 views)
Permalink
Linux shell to python

Dear friends,

I just joined the group.
I was trying porting from bash shell to python.

let me know if someone has tried to implement (grep and PIPE)  shell commands in python `lspci | grep Q | grep  "$isp_str1" | grep "$isp_str2" | cut -c1-7'
 
I tried to use python subprocess and OS.Popen modules.

Thanks & Regard's

Vikas Kumar Choudhary


(Yahoo,MSN-Hotmail,Skype) Messenger = vikas.choudhary
Please Add Me in Your Messenger List for Better Communication
P  Please consider the environment before printing this e-mail
Do not print this email unless it is absolutely necessary.
Save papers, Save tree.
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Note: This e-mail is confidential and may also be privileged, this is for the intended recipients only. If you are not the intended recipient, please delete the message and notify me immediately; you should not copy or use it for any purpose, nor disclose its contents to any other person.

Notice:  All email and instant messages (including attachments) sent to or from This E-mail id , may be retained, monitored and/or reviewed, or authorized law enforcement personnel, without further notice or consent.
----------------------------------------------------------------------------------------------------------------------


rosuav at gmail

Jul 30, 2012, 12:40 AM

Post #2 of 18 (1045 views)
Permalink
Re: Linux shell to python [In reply to]

On Mon, Jul 30, 2012 at 5:05 PM, Vikas Kumar Choudhary
<vikas.choudhary [at] yahoo> wrote:
>
> I was trying porting from bash shell to python.
>
> let me know if someone has tried to implement (grep and PIPE) shell commands in python `lspci | grep Q | grep "$isp_str1" | grep "$isp_str2" | cut -c1-7'

Welcome!

While it's technically possible to do exactly that in Python (using
subprocess as you describe), there's usually a more efficient and
cleaner method of achieving the same goal. With a port such as you
describe, it's probably best to go right back to the conceptual level
and work out what exactly you're trying to do, and then look at
implementing that in Python. You'll end up with much cleaner code at
the end of it.

For an initial guess, I would say that you'll use subprocess to invoke
lspci, but then everything else will be done in Python directly.

ChrisA
--
http://mail.python.org/mailman/listinfo/python-list


phihag at phihag

Jul 30, 2012, 3:35 AM

Post #3 of 18 (1043 views)
Permalink
Re: Linux shell to python [In reply to]

On 07/30/2012 09:05 AM, Vikas Kumar Choudhary wrote:
> `lspci | grep Q | grep "$isp_str1" | grep "$isp_str2" | cut -c1-7'

The rough Python equivalent would be

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
]

You can also just paste the whole pipeline with the shell=True
parameter. That's not recommended though, and it's hard to correctly
quote strings.

- Philipp
Attachments: signature.asc (0.19 KB)


zhangshaohua20101010 at gmail

Jul 30, 2012, 4:09 AM

Post #4 of 18 (1039 views)
Permalink
Re: Linux shell to python [In reply to]

you can use commands.getstatusoutput(command), the shell command special charactor (like "$ and so on )should be escaped!


在 2012å¹´7月30日星期一UTC+8下åˆ3æ—¶40分04秒,Chris Angelico写é“:
> On Mon, Jul 30, 2012 at 5:05 PM, Vikas Kumar Choudhary
>
> <vikas.choudhary [at] yahoo> wrote:
>
> >
>
> > I was trying porting from bash shell to python.
>
> >
>
> > let me know if someone has tried to implement (grep and PIPE) shell commands in python `lspci | grep Q | grep "$isp_str1" | grep "$isp_str2" | cut -c1-7'
>
>
>
> Welcome!
>
>
>
> While it's technically possible to do exactly that in Python (using
>
> subprocess as you describe), there's usually a more efficient and
>
> cleaner method of achieving the same goal. With a port such as you
>
> describe, it's probably best to go right back to the conceptual level
>
> and work out what exactly you're trying to do, and then look at
>
> implementing that in Python. You'll end up with much cleaner code at
>
> the end of it.
>
>
>
> For an initial guess, I would say that you'll use subprocess to invoke
>
> lspci, but then everything else will be done in Python directly.
>
>
>
> ChrisA
--
http://mail.python.org/mailman/listinfo/python-list


jae+python at jaerhard

Jul 30, 2012, 4:31 AM

Post #5 of 18 (1042 views)
Permalink
Re: Linux shell to python [In reply to]

On Mon, Jul 30, 2012 at 12:35:38PM +0200, Philipp Hagemeister wrote:
> On 07/30/2012 09:05 AM, Vikas Kumar Choudhary wrote:
> > `lspci | grep Q | grep "$isp_str1" | grep "$isp_str2" | cut -c1-7'
>
> The rough Python equivalent would be
>
> 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.

But you did qualify it as "rough" :D

Grits, J
--
http://mail.python.org/mailman/listinfo/python-list


__peter__ at web

Jul 30, 2012, 4:58 AM

Post #6 of 18 (1040 views)
Permalink
Re: Linux shell to python [In reply to]

Vikas Kumar Choudhary wrote:


> let me know if someone has tried to implement (grep and PIPE) shell
> commands in python `lspci | grep Q | grep "$isp_str1" | grep "$isp_str2"
> | cut -c1-7'
>
> I tried to use python subprocess and OS.Popen modules.

subprocess is the way to go.

> I was trying porting from bash shell to python.

Here's an example showing how to translate a shell pipe:

http://docs.python.org/library/subprocess.html#replacing-shell-pipeline

But even if you can port the shell script literally I recommend a more
structured approach:

import subprocess
import itertools

def parse_data(lines):
for not_empty, group in itertools.groupby(lines, key=bool):
if not_empty:
triples = (line.partition(":") for line in group)
pairs = ((left, right.strip()) for left, sep, right in triples)
yield dict(pairs)

if __name__ == "__main__":
def get(field):
return entry.get(field, "").lower()
output = subprocess.Popen(["lspci", "-vmm"],
stdout=subprocess.PIPE).communicate()[0]
for entry in parse_data(output.splitlines()):
if "nvidia" in get("Vendor") and "usb" in get("Device"):
print entry["Slot"]
print entry["Device"]
print


--
http://mail.python.org/mailman/listinfo/python-list


phihag at phihag

Jul 30, 2012, 4:59 AM

Post #7 of 18 (1040 views)
Permalink
Re: Linux shell to python [In reply to]

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
Attachments: signature.asc (0.19 KB)


mail at paultjuh

Jul 30, 2012, 9:55 AM

Post #8 of 18 (1039 views)
Permalink
RE: Linux shell to python [In reply to]

You can do this with one subprocess.Popen and some python commands.

The alternative is to pipe some subprocess.Popen commands together.

Or for the quick way out (but I think you better stick with bash scripting then): http://pypi.python.org/pypi/sarge/

Don't know about it's stability/ubs/etc, never used it.
 
-----Original message-----
From:Vikas Kumar Choudhary <vikas.choudhary [at] yahoo>
Sent:Mon 30-07-2012 09:34
Subject:Linux shell to python
To:python-list [at] python;

Dear friends,

I just joined the group.
I was trying porting from bash shell to python.

 let me know if someone has tried to implement (grep and PIPE)  shell commands in python `lspci | grep Q | grep  "$isp_str1" | grep "$isp_str2" | cut -c1-7'
 I tried to use python subprocess and OS.Popen modules.

Thanks & Regard's

Vikas Kumar Choudhary


(Yahoo,MSN-Hotmail,Skype) Messenger = vikas.choudhary
Please Add Me in Your Messenger List for Better Communication

P  Please consider the environment before printing this e-mail
Do not print this email unless it is absolutely necessary.
Save papers, Save tree.
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Note: This e-mail is confidential and may also be privileged, this is for the intended recipients only. If you are not the intended recipient, please delete the message and notify me immediately; you should not copy or use it for any purpose, nor disclose its contents to any other person.

Notice:  All email and instant messages (including attachments) sent to or from This E-mail id , may be retained, monitored and/or reviewed, or authorized law enforcement personnel, without further notice or consent.
----------------------------------------------------------------------------------------------------------------------
 

--
http://mail.python.org/mailman/listinfo/python-list


barry at barrys-emacs

Jul 30, 2012, 2:26 PM

Post #9 of 18 (1045 views)
Permalink
Re: Linux shell to python [In reply to]

lspci gets all its information from the files in /sys/bus/pci/devices.

You can use os.listdir() to list all the files in the folder and then open
the files you want to get the data you need.

And of course you can write list comprehensions on as many lines as
it take to make the code maintainable.

Barry


On 30 Jul 2012, at 17:55, Paul van der Linden <mail [at] paultjuh> wrote:

> You can do this with one subprocess.Popen and some python commands.
> The alternative is to pipe some subprocess.Popen commands together.
> Or for the quick way out (but I think you better stick with bash scripting then): http://pypi.python.org/pypi/sarge/
> Don't know about it's stability/ubs/etc, never used it.
>
> -----Original message-----
> From: Vikas Kumar Choudhary <vikas.choudhary [at] yahoo>
> Sent: Mon 30-07-2012 09:34
> Subject: Linux shell to python
> To: python-list [at] python;
> Dear friends,
>
> I just joined the group.
> I was trying porting from bash shell to python.
>
>
> let me know if someone has tried to implement (grep and PIPE) shell commands in python `lspci | grep Q | grep "$isp_str1" | grep "$isp_str2" | cut -c1-7'
>
> I tried to use python subprocess and OS.Popen modules.
>
> Thanks & Regard's
>
> Vikas Kumar Choudhary
>
>
> (Yahoo,MSN-Hotmail,Skype) Messenger = vikas.choudhary
> Please Add Me in Your Messenger List for Better Communication
> P Please consider the environment before printing this e-mail
> Do not print this email unless it is absolutely necessary.
> Save papers, Save tree.
> --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
> Note: This e-mail is confidential and may also be privileged, this is for the intended recipients only. If you are not the intended recipient, please delete the message and notify me immediately; you should not copy or use it for any purpose, nor disclose its contents to any other person.
>
> Notice: All email and instant messages (including attachments) sent to or from This E-mail id , may be retained, monitored and/or reviewed, or authorized law enforcement personnel, without further notice or consent.
> ----------------------------------------------------------------------------------------------------------------------
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
> --
> http://mail.python.org/mailman/listinfo/python-list


drsalists at gmail

Jul 30, 2012, 3:56 PM

Post #10 of 18 (1039 views)
Permalink
Re: Linux shell to python [In reply to]

On Mon, Jul 30, 2012 at 9:26 PM, Barry Scott <barry [at] barrys-emacs> wrote:

> lspci gets all its information from the files in /sys/bus/pci/devices.
>


> You can use os.listdir() to list all the files in the folder and then open
> the files you want to get the data you need.
>
Gee, wouldn't it be more portable to parse lspci? I wouldn't put it past
the (Linux) kernel devs to move the pseudo-filesystems around again...


> And of course you can write list comprehensions on as many lines as
> it take to make the code maintainable.
>
Sigh, and I'm also not keen on multi-line list comprehensions, specifically
because I think they tend to make less readable code. It also becomes a
mess when you need to insert print statements to get some visibility into
what's going on.


emile at fenx

Jul 30, 2012, 4:14 PM

Post #11 of 18 (1039 views)
Permalink
Re: Linux shell to python [In reply to]

On 7/30/2012 3:56 PM Dan Stromberg said...
>
> On Mon, Jul 30, 2012 at 9:26 PM, Barry Scott <barry [at] barrys-emacs

> And of course you can write list comprehensions on as many lines as
> it take to make the code maintainable.
>
> Sigh, and I'm also not keen on multi-line list comprehensions,
> specifically because I think they tend to make less readable code. It
> also becomes a mess when you need to insert print statements to get some
> visibility into what's going on.

I tend to write long one-liners then convert them to loops the first
time I need to see what's going on.

Premature optimization otherwise. :)

Emile



--
http://mail.python.org/mailman/listinfo/python-list


drsalists at gmail

Jul 30, 2012, 4:46 PM

Post #12 of 18 (1037 views)
Permalink
Re: Linux shell to python [In reply to]

On Mon, Jul 30, 2012 at 11:14 PM, Emile van Sebille <emile [at] fenx> wrote:

> On 7/30/2012 3:56 PM Dan Stromberg said...
>
>
>> On Mon, Jul 30, 2012 at 9:26 PM, Barry Scott <barry [at] barrys-emacs
>>
>
> And of course you can write list comprehensions on as many lines as
>> it take to make the code maintainable.
>>
>> Sigh, and I'm also not keen on multi-line list comprehensions,
>> specifically because I think they tend to make less readable code. It
>> also becomes a mess when you need to insert print statements to get some
>> visibility into what's going on.
>>
>
> I tend to write long one-liners then convert them to loops the first time
> I need to see what's going on.
>
> Premature optimization otherwise. :)
>

Premature optimization of what?


breamoreboy at yahoo

Jul 31, 2012, 12:15 AM

Post #13 of 18 (1042 views)
Permalink
Re: Linux shell to python [In reply to]

On 31/07/2012 02:20, Dennis Lee Bieber wrote:
> On Mon, 30 Jul 2012 22:56:48 +0000, Dan Stromberg <drsalists [at] gmail>
> declaimed the following in gmane.comp.python.general:
>
>
>> Sigh, and I'm also not keen on multi-line list comprehensions, specifically
>> because I think they tend to make less readable code. It also becomes a
>> mess when you need to insert print statements to get some visibility into
>> what's going on.
>
> Cleanly splitting the list-comp by
>
> [ result-computation
> for-item-selection-clause
> if-filter-clause ]
>
> isn't that unreadable... But anyone doing
>
> [. result-
> -computation for-item-
> -selection-clause if-
> -filter-
> -clause ]
>
> should be pecked to death by a dead parrot.
>

Any particular species?

--
Cheers.

Mark Lawrence.

--
http://mail.python.org/mailman/listinfo/python-list


rosuav at gmail

Jul 31, 2012, 1:05 AM

Post #14 of 18 (1038 views)
Permalink
Re: Linux shell to python [In reply to]

On Tue, Jul 31, 2012 at 5:15 PM, Mark Lawrence <breamoreboy [at] yahoo> wrote:
> On 31/07/2012 02:20, Dennis Lee Bieber wrote:
>>
>> should be pecked to death by a dead parrot.
>>
>
> Any particular species?

I'm sure that, if you're in Norway, you could find an appropriate
bird. But for those of us for whom that's not an option, I'd be
content to nail the programmer's feet to the cage and then return him
whence he came as a faulty model. Unfortunately local law enforcement
would want in on that deal.

ChrisA
--
http://mail.python.org/mailman/listinfo/python-list


alister.ware at ntlworld

Jul 31, 2012, 3:53 AM

Post #15 of 18 (1038 views)
Permalink
Re: Linux shell to python [In reply to]

On Tue, 31 Jul 2012 08:15:32 +0100, Mark Lawrence wrote:

> On 31/07/2012 02:20, Dennis Lee Bieber wrote:
>> On Mon, 30 Jul 2012 22:56:48 +0000, Dan Stromberg <drsalists [at] gmail>
>> declaimed the following in gmane.comp.python.general:
>>
>>
>>> Sigh, and I'm also not keen on multi-line list comprehensions,
>>> specifically because I think they tend to make less readable code. It
>>> also becomes a mess when you need to insert print statements to get
>>> some visibility into what's going on.
>>
>> Cleanly splitting the list-comp by
>>
>> [ result-computation
>> for-item-selection-clause if-filter-clause ]
>>
>> isn't that unreadable... But anyone doing
>>
>> [ result-
>> -computation for-item- -selection-clause if- -filter-
>> -clause ]
>>
>> should be pecked to death by a dead parrot.
>>
>>
> Any particular species?

as has already been hinted - Norwegian Blue



--
Question: Is it better to abide by the rules until they're changed or
help speed the change by breaking them?
--
http://mail.python.org/mailman/listinfo/python-list


dihedral88888 at googlemail

Jul 31, 2012, 12:01 PM

Post #16 of 18 (1025 views)
Permalink
Re: Linux shell to python [In reply to]

Mark Lawrenceæ–¼ 2012å¹´7月31日星期二UTC+8下åˆ3時15分32秒寫é“:
> On 31/07/2012 02:20, Dennis Lee Bieber wrote:
>
> > On Mon, 30 Jul 2012 22:56:48 +0000, Dan Stromberg <drsalists [at] gmail>
>
> > declaimed the following in gmane.comp.python.general:
>
> >
>
> >
>
> >> Sigh, and I'm also not keen on multi-line list comprehensions, specifically
>
> >> because I think they tend to make less readable code. It also becomes a
>
> >> mess when you need to insert print statements to get some visibility into
>
> >> what's going on.
>
> >
>
> > Cleanly splitting the list-comp by
>
> >
>
> > [ result-computation
>
> > for-item-selection-clause
>
> > if-filter-clause ]
>
> >
>
> > isn't that unreadable... But anyone doing
>
> >
>
> > [ result-
>
> > -computation for-item-
>
> > -selection-clause if-
>
> > -filter-
>
> > -clause ]
>
> >
>
> > should be pecked to death by a dead parrot.
>
> >
>
>
>
> Any particular species?
>
>
>
> --
>
> Cheers.
>
>
>
> Mark Lawrence.

For each item in the known list that passes the required conditional test,
then use the item to perform the tasks desired as instructed.

It is not necessary that a new list has to be constructed in the tasks.

A method which can produce a new object always involves some memory management.
Therefore, some error handling part should not be missing.


--
http://mail.python.org/mailman/listinfo/python-list


dihedral88888 at googlemail

Jul 31, 2012, 12:01 PM

Post #17 of 18 (1024 views)
Permalink
Re: Linux shell to python [In reply to]

Mark Lawrenceæ–¼ 2012å¹´7月31日星期二UTC+8下åˆ3時15分32秒寫é“:
> On 31/07/2012 02:20, Dennis Lee Bieber wrote:
>
> > On Mon, 30 Jul 2012 22:56:48 +0000, Dan Stromberg <drsalists [at] gmail>
>
> > declaimed the following in gmane.comp.python.general:
>
> >
>
> >
>
> >> Sigh, and I'm also not keen on multi-line list comprehensions, specifically
>
> >> because I think they tend to make less readable code. It also becomes a
>
> >> mess when you need to insert print statements to get some visibility into
>
> >> what's going on.
>
> >
>
> > Cleanly splitting the list-comp by
>
> >
>
> > [ result-computation
>
> > for-item-selection-clause
>
> > if-filter-clause ]
>
> >
>
> > isn't that unreadable... But anyone doing
>
> >
>
> > [ result-
>
> > -computation for-item-
>
> > -selection-clause if-
>
> > -filter-
>
> > -clause ]
>
> >
>
> > should be pecked to death by a dead parrot.
>
> >
>
>
>
> Any particular species?
>
>
>
> --
>
> Cheers.
>
>
>
> Mark Lawrence.

For each item in the known list that passes the required conditional test,
then use the item to perform the tasks desired as instructed.

It is not necessary that a new list has to be constructed in the tasks.

A method which can produce a new object always involves some memory management.
Therefore, some error handling part should not be missing.


--
http://mail.python.org/mailman/listinfo/python-list


barry at barrys-emacs

Jul 31, 2012, 3:36 PM

Post #18 of 18 (1025 views)
Permalink
Re: Linux shell to python [In reply to]

On 30 Jul 2012, at 23:56, Dan Stromberg <drsalists [at] gmail> wrote:

>
> On Mon, Jul 30, 2012 at 9:26 PM, Barry Scott <barry [at] barrys-emacs> wrote:
> lspci gets all its information from the files in /sys/bus/pci/devices.
>
> You can use os.listdir() to list all the files in the folder and then open
> the files you want to get the data you need.
> Gee, wouldn't it be more portable to parse lspci? I wouldn't put it past the (Linux) kernel devs to move the pseudo-filesystems around again...

No. The app is less stable the the linux user api gureentee.
I have been bitten with app output format changes but the /sys files have been
Reliable and far better focused at answering questions.

>
> And of course you can write list comprehensions on as many lines as
> it take to make the code maintainable.
> Sigh, and I'm also not keen on multi-line list comprehensions, specifically because I think they tend to make less readable code. It also becomes a mess when you need to insert print statements to get some visibility into what's going on.
>

With out trading examples of production code this is resolverable.

Barry

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.