Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > inpipe and outpipe (and other useful functions)

Reply
Thread Tools

inpipe and outpipe (and other useful functions)

 
 
Ole Martin Bjørndalen
Guest
Posts: n/a
 
      08-28-2011
Hi!

Please excuse me if this i common knowledge, or if I've one again
re-implemented something
that turned out to be in the standard library, but I think I came up
with something rather neat.

I'm writing a lot of programs that call external programs, and as much
as I love subproces.Popen, I do get tired of writing
rather wordy things like:

args = ['cmd', 'arg1', 'arg2', 'etc']
p = subprocess.Popen(args, stdin=subprocess.PIPE)
for line in p:
line = line.decode('latin1')
do_something_with(line)

Bleh.

so last week I had enough, and I sat down and wrote a few functions.
No I can do:

from lib import inpipe

for line in inpipe(args, encoding='latin1'):
do_something_with(line)

and

from lib import outpipe

with outpipe(args, addnl=True) as write:
write('Python is awesome!')
write('')

Some code from a program I'm writing:

args = ['metaflac',
'--show-total-samples',
'--show-sample-rate',
file]

for line in stripped(inpipe(args)):
(name, value) = line.split('=')
# ... process tag

Now, that is a lot more readable than what I had before!

The library has a lot of other things in it as well, and is available here:

https://github.com/olemb/lib

I love Python!


--
Ole Martin,
http://nerdly.info/ole/
 
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
Full circle, considering that Apple and Adobe made each other useful - Bobb - Computer Information 0 05-12-2010 02:06 PM
Are MalwareBytes And AdAware Useful In Addition To Spyware Blaster And SuperAntiSpyware Internet Highway Traveler Computer Support 0 03-22-2010 11:55 PM
useful setting in device manager (may be useful to know!) jameshanley39@yahoo.co.uk Computer Information 2 07-07-2008 04:28 PM
Useful one-liners and other short Perl scripts J. Romano Perl Misc 18 07-13-2004 10:04 PM



Advertisments