Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.016 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; "(it's": 0.09; 'accordingly': 0.09; 'fetch': 0.09; 'object.': 0.09; 'wrote:': 0.15; '4096': 0.16; 'iterable,': 0.16; 'received:192.168.1.40': 0.16; 'stdin,': 0.16; 'syntactic': 0.16; 'trivially': 0.16; 'object,': 0.16; 'this:': 0.16; 'def': 0.16; 'true,': 0.19; 'command': 0.19; 'ignore': 0.21; 'header:In-Reply-To:1': 0.22; 'code': 0.24; 'string': 0.26; '(and': 0.27; 'skip:p 30': 0.28; 'yield': 0.29; 'fact': 0.30; 'lines': 0.31; 'print': 0.32; 'break': 0.33; 'anyone': 0.33; 'to:addr:python-list': 0.34; 'header:User-Agent:1': 0.34; 'example,': 0.35; 'thread': 0.37; 'some': 0.37; 'but': 0.37; 'could': 0.37; 'another': 0.38; 'received:192': 0.38; 'subject:: ': 0.38; 'think': 0.38; 'put': 0.38; 'received:192.168.1': 0.39; 'either': 0.39; 'to:addr:python.org': 0.39; 'view': 0.67; 'received:62': 0.67; 'blank': 0.71; 'subject:The': 0.73; 'stream': 0.77; "'all'": 0.84; 'from:addr:t': 0.84 Date: Wed, 03 Aug 2011 18:39:12 +0200 From: Thomas Jollans User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:5.0) Gecko/20110628 Thunderbird/5.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Snippet: The leanest Popen wrapper References: <51b2d157-3fea-4f8e-80b4-e7142629eca8@s21g2000pre.googlegroups.com> In-Reply-To: <51b2d157-3fea-4f8e-80b4-e7142629eca8@s21g2000pre.googlegroups.com> X-Enigmail-Version: 1.2 OpenPGP: id=5C8691ED Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 67 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1312389541 news.xs4all.nl 23944 [2001:888:2000:d::a6]:58996 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:10814 On 03/08/11 17:29, Phlip wrote: > Groupies: > > This is either a code snippet, if you like it, or a request for a > critique, if you don't. > > I want to call a command and then treat the communication with that > command as an object. And I want to do it as application-specifically > as possible. Anyone could think of a way to productize this: > > def command(*cmdz): > > process = Popen( flatten(cmdz), > shell= True, > stdout= subprocess.PIPE, > stderr= subprocess.PIPE, > bufsize= 4096 ) > > def line(): > return process.stdout.readline().rstrip() > > def s(): > while True: > l = line() > if not l: break This will ignore everything after a blank line. Intended? It may be better not to use readline(), but to use the fact that it's an iterable, and use next(process.stdout) to get each line. (and deal with StopIteration accordingly -- or not) > yield l > > line.s = s > > return line > > That leads to some syntactic sugar. For example, one truly demented > way to stream in an entire block and then treat it as one big string > is this: > > print '\n'.join(command('ls').s()) > > The point of the command() complex is the ability to start a long > command and then fetch out individual lines from it: > > line = command('find', '../..') > > print 'lines' > print line() > print line() > print line() > > print 'all' > print list(line.s()) > > If you need different pipe abilities, such as stdin, you can trivially > add them to the contents of command() (it's not productized on > purpose). > > So I can take the line() functor and, for example, pin it to a View > object, or put it in another thread now, right? > > -- > Phlip > http://bit.ly/ZeekLand