Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #105094
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: Bash-like pipes in Python |
| Date | 2016-03-18 01:36 +1100 |
| Message-ID | <mailman.272.1458225402.12893.python-list@python.org> (permalink) |
| References | <56e97459$0$1600$c3e8da3$5496439d@news.astraweb.com> <87egbacq2j.fsf@elektro.pacujo.net> <56eabae8$0$1599$c3e8da3$5496439d@news.astraweb.com> |
On Fri, Mar 18, 2016 at 1:10 AM, Steven D'Aprano <steve@pearwood.info> wrote:
> At the moment, the data being processed by the Map, Filter, etc. are
> ordinary lists or iterators. In order to give them a customer __repr__, I
> would have to change the Map and Filter __ror__ method to return some
> custom type which behaves as an iterable but has the appropriate __repr__.
> I don't want to do that: I want the pipeline functions to return ordinary
> lists or iterators, whichever is appropriate.
They don't have to be iterators, just iterables, right? They could
return a one of these:
class QuantumList:
def __init__(self, iterable):
self.iter = iter(iterable)
self.list = []
def __iter__(self):
yield from self.list
while "moar stuff":
try: val = next(self.iter)
except StopIteration: return
self.list.append(val)
yield val
def __repr__(self):
self.list.extend(self.iter)
return repr(self.list)
This object has a generator/list duality, but if you observe it, it
collapses to a list. When used interactively, it'd be pretty much the
same as calling list() as the last step, but in a script, they'd
operate lazily.
Quantum computing is here already!
ChrisA
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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