Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Joel Goldstick Newsgroups: comp.lang.python Subject: Re: Bash-like pipes in Python Date: Wed, 16 Mar 2016 11:09:04 -0400 Lines: 54 Message-ID: References: <56e97459$0$1600$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de 1PhlHgusWt8HGarqUZ4KkQt8t9kCDEKu5IZOI5Ugb2Gw== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '16,': 0.03; 'operator': 0.03; 'subject:Python': 0.05; 'definitions': 0.07; 'cc:addr :python-list': 0.09; 'filter,': 0.09; 'ruby,': 0.09; 'python': 0.10; 'python.': 0.11; 'output': 0.13; 'wed,': 0.15; '2016': 0.16; 'iterable:': 0.16; 'map(int,': 0.16; 'opposite': 0.16; 'pipelines': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'result:': 0.16; 'wrote:': 0.16; 'instance,': 0.18; 'string,': 0.18; 'transform': 0.18; 'input': 0.18; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'martin': 0.22; 'pipe': 0.22; 'am,': 0.23; 'import': 0.24; 'written': 0.24; 'header:In-Reply-To:1': 0.24; 'message-id:@mail.gmail.com': 0.27; 'function': 0.28; 'values': 0.28; '120': 0.29; 'extensively': 0.29; 'url:activestate': 0.29; 'convert': 0.29; "i'm": 0.30; 'url:mailman': 0.30; 'code': 0.30; 'operations': 0.31; 'url:python': 0.33; "d'aprano": 0.33; 'extract': 0.33; 'steven': 0.33; 'subject:like': 0.33; 'symbol': 0.33; 'url:listinfo': 0.34; '(for': 0.34; 'languages': 0.34; 'url:code': 0.34; 'skip:& 20': 0.35; 'received:google.com': 0.35; 'next': 0.35; 'done': 0.35; 'according': 0.36; 'but': 0.36; 'url:org': 0.36; 'received:209.85': 0.36; 'subject:: ': 0.37; 'method': 0.37; 'missing': 0.37; 'received:209': 0.38; 'received:209.85.214': 0.39; 'subject:-': 0.39; 'url:mail': 0.40; 'linked': 0.63; 'mar': 0.65; 'here': 0.66; 'finally': 0.70; 'url:info': 0.71; 'url:recipes': 0.84; 'to:none': 0.91; 'joel': 0.91; 'interesting,': 0.93; 'technique': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc; bh=T45YTZ+ZmOTYcaf3k++J6M9b7UGoaUYUAgnVOP6mXTg=; b=SmmnFXUQ1FxlcaANsGk3pgEPVZW9Cj2p+79tzO3keihOUBWdoyqSf+Yhy/00BCuB8S eUJgSSwKhsp4D61G/B5U3KR4VQPD6f+b+J4yKP9rdKFG5MECe6XMXBTev9ABHHxjnQU6 FBjypbUkYGrUJg/4QzL+WvOv1Ynwio6qPFpb6YPN5ia9Nd9X8ugYi6Edxow+SQDpAPii fKy9WdzsvFguUZD766t7d9jVygshYyQVrHoD7dQq2UV3Ix/KbIZ02fr6EKCL6EwzDbHO 97G2coLHcLVgMEN8BAIxFsXy5iIvB2kWmUth6Licygv7ti/Qhv3ua4StuRuxudfy5Cv8 xAZA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:cc; bh=T45YTZ+ZmOTYcaf3k++J6M9b7UGoaUYUAgnVOP6mXTg=; b=FRTtLEJSUsGOeqUxDDsKSEuNv2dF/a2YwWvuHHNuUxf1DMHFi1p0CdmJL+wATxjO0p XulLEeENrcsJL8LnQJMooaVBCczFEzETuUI6TcBWew8LB+hxCKSJPyGJNFN8yBBQsTmr DNxC4DQQV5qYVdd/ySET4GAo86tUIjZzk6aNjLX2hMbc4mF6Xzj5YvoxYQf/iRN6D6L/ bgl7DClOVDOp2XfnOR9baU3s85kA0aXoM1zU8mZFmy6xV4H0teY59q02o4uvAHLDwbIT HywWiaU8esefZjJket8eZXHmaxSaUFWKUnmmJyCfxCvYB9Or/MMsS+MCQErVPpyvI32E N8Lw== X-Gm-Message-State: AD7BkJI9VUmuV27jitFkejclJhWUQ01he7Tbdy97TRm/BM+MUWRuVORMnRGisARofrW3ibi4o6vzCE5qCQMB7A== X-Received: by 10.60.150.235 with SMTP id ul11mr2618711oeb.70.1458140944263; Wed, 16 Mar 2016 08:09:04 -0700 (PDT) In-Reply-To: <56e97459$0$1600$c3e8da3$5496439d@news.astraweb.com> X-Content-Filtered-By: Mailman/MimeDel 2.1.21 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:105044 On Wed, Mar 16, 2016 at 10:57 AM, Steven D'Aprano wrote: > There's a powerful technique used in shell-scripting languages like bash: > pipes. The output of one function is piped in to become the input to the > next function. > > According to Martin Fowler, this was also used extensively in Smalltalk: > > http://martinfowler.com/articles/collection-pipeline/ > > and can also be done in Ruby, using method chaining. > > Here is a way to do functional-programming-like pipelines to collect and > transform values from an iterable: > > https://code.activestate.com/recipes/580625-collection-pipeline-in-python/ > > For instance, we can take a string, extract all the digits, convert them to > ints, and finally multiply the digits to give a final result: > > py> from operator import mul > py> "abcd12345xyz" | Filter(str.isdigit) | Map(int) | Reduce(mul) > 120 > > > (For the definitions of Filter, Map and Reduce, see the code at the > ActiveState recipe, linked above). In my opinion, this is much nicer > looking that the standard Python `filter`, `map` and `reduce`: > > py> reduce(mul, map(int, filter(str.isdigit, "abcd12345xyz"))) > 120 > > as this requires the operations to be written in the opposite order to the > order that they are applied. > > > This is interesting, but the part I'm missing is the use of the Pipe symbol '|' in python. Can you elaborate > > -- > Steven > > -- > https://mail.python.org/mailman/listinfo/python-list > -- Joel Goldstick http://joelgoldstick.com/ http://cc-baseballstats.info/