Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #105048

Re: Bash-like pipes in Python

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Random832 <random832@fastmail.com>
Newsgroups comp.lang.python
Subject Re: Bash-like pipes in Python
Date Wed, 16 Mar 2016 11:20:17 -0400
Lines 41
Message-ID <mailman.217.1458141621.12893.python-list@python.org> (permalink)
References <56e97459$0$1600$c3e8da3$5496439d@news.astraweb.com>
Mime-Version 1.0
Content-Type text/plain
Content-Transfer-Encoding 7bit
X-Trace news.uni-berlin.de zJFP2cJzj8T7+1+abrobWgZFaUJz3mHwhWHHfWIF65tQ==
Return-Path <random832@fastmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; '16,': 0.03; 'operator': 0.03; 'subject:Python': 0.05; 'args)': 0.07; 'definitions': 0.07; 'filter,': 0.09; 'received:internal': 0.09; 'python': 0.10; 'def': 0.13; 'wed,': 0.15; 'map(int,': 0.16; 'message- id:@webmail.messagingengine.com': 0.16; 'opposite': 0.16; 'received:10.202': 0.16; 'received:10.202.2': 0.16; 'received:66.111': 0.16; 'received:66.111.4': 0.16; 'received:io': 0.16; 'received:messagingengine.com': 0.16; 'received:psf.io': 0.16; 'result:': 0.16; 'wrote:': 0.16; 'instance,': 0.18; 'string,': 0.18; 'import': 0.24; 'written': 0.24; 'header:In- Reply-To:1': 0.24; '120': 0.29; 'convert': 0.29; '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; 'url:listinfo': 0.34; '(for': 0.34; 'url:org': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:10': 0.37; 'received:66': 0.38; 'skip:p 20': 0.38; 'subject:-': 0.39; 'url:mail': 0.40; 'to:addr:python.org': 0.40; 'header:Message- Id:1': 0.61; 'linked': 0.63; 'mar': 0.65; 'finally': 0.70; 'func):': 0.84
DKIM-Signature v=1; a=rsa-sha1; c=relaxed/relaxed; d=fastmail.com; h= content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-sasl-enc :x-sasl-enc; s=mesmtp; bh=cUWpy2FVPWqrgEtsyVFDsxYlTiI=; b=AmT82s x4MzPbZyY6msZh/tvHFaIiIj7yp1UCpchGoOgAwaQgJKI5P+NiisczJrOfTcvmoc F1s9Z8dvxm50oZpTjADkZTt2o8oXiBm4Yvekz4fq+oI9mBIYB+WJDIeAbXSS/tHo YP/jcWbVXPggdwfycMDY/cU3hpPhGL5SQgZU0=
DKIM-Signature v=1; a=rsa-sha1; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-sasl-enc:x-sasl-enc; s=smtpout; bh=cUWpy2FVPWqrgEt syVFDsxYlTiI=; b=n8enW2wEN8wLqkCw+WQd2SZLEgJkOuWPjPmje4p18xi26NF eWn6IcRg2OKXGQfBmJEB5vUr42Lxo6pyPHHdYLUbJJPtN5SfuhIm4DKgnP+Xp3ff EOt/vw1wUzWERjA0M2odCYWSATvpcft/HJJX8Tvyqcc2WyhCwYXp5AlOAx8g=
X-Sasl-Enc oUOlR2k/29CuptETqv1Vlo6ejDMXZbtW60S7R4uuZhgM 1458141617
X-Mailer MessagingEngine.com Webmail Interface - ajax-872772a7
In-Reply-To <56e97459$0$1600$c3e8da3$5496439d@news.astraweb.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.21
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Xref csiph.com comp.lang.python:105048

Show key headers only | View raw



On Wed, Mar 16, 2016, at 10:57, Steven D'Aprano wrote:
> 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

How about:

from functools import partial, reduce
from operator import mul
def rcall(arg, func): return func(arg)
def fpipe(*args): return reduce(rcall, args)
pfilter = partial(partial, filter)
pmap = partial(partial, map)
preduce = partial(partial, reduce)

fpipe("abcd12345xyz", pfilter(str.isdigit), pmap(int), preduce(mul))

> (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.
> 
> 
> 
> -- 
> Steven
> 
> -- 
> https://mail.python.org/mailman/listinfo/python-list

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Bash-like pipes in Python Steven D'Aprano <steve@pearwood.info> - 2016-03-17 01:57 +1100
  Re: Bash-like pipes in Python Joel Goldstick <joel.goldstick@gmail.com> - 2016-03-16 11:09 -0400
    Re: Bash-like pipes in Python Christian Gollwitzer <auriocus@gmx.de> - 2016-03-16 16:16 +0100
  Re: Bash-like pipes in Python Random832 <random832@fastmail.com> - 2016-03-16 11:20 -0400
    Re: Bash-like pipes in Python Steven D'Aprano <steve@pearwood.info> - 2016-03-17 21:58 +1100
      Re: Bash-like pipes in Python Marko Rauhamaa <marko@pacujo.net> - 2016-03-17 13:49 +0200
        Re: Bash-like pipes in Python Sivan Greenberg <sivan@vitakka.co> - 2016-03-17 14:10 +0200
          Re: Bash-like pipes in Python Marko Rauhamaa <marko@pacujo.net> - 2016-03-17 14:44 +0200
  Re: Bash-like pipes in Python "Sven R. Kunze" <srkunze@mail.de> - 2016-03-16 16:20 +0100
  Re: Bash-like pipes in Python Random832 <random832@fastmail.com> - 2016-03-16 11:21 -0400
  Re: Bash-like pipes in Python Steven D'Aprano <steve@pearwood.info> - 2016-03-17 02:39 +1100
  Re: Bash-like pipes in Python Marko Rauhamaa <marko@pacujo.net> - 2016-03-16 19:04 +0200
    Re: Bash-like pipes in Python Steven D'Aprano <steve@pearwood.info> - 2016-03-18 01:10 +1100
      Re: Bash-like pipes in Python Chris Angelico <rosuav@gmail.com> - 2016-03-18 01:36 +1100
      Re: Bash-like pipes in Python Random832 <random832@fastmail.com> - 2016-03-17 12:31 -0400
      Re: Bash-like pipes in Python Sivan Greenberg <sivan@vitakka.co> - 2016-03-17 19:20 +0200

csiph-web