Skip to content

Instantly share code, notes, and snippets.

@fuktommy
Created May 18, 2012 09:40
Show Gist options
  • Select an option

  • Save fuktommy/2724318 to your computer and use it in GitHub Desktop.

Select an option

Save fuktommy/2724318 to your computer and use it in GitHub Desktop.
Iterator pipe in Python
"""Iterator Utility that use iterator with method chains.
"""
#
# Copyright (c) 2012 Satoshi Fukutomi <info@fuktommy.com>.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
__all__ = ['wrap', 'Command']
class Pipe(object):
"""Iterator pipe wrapper.
"""
def __init__(self, iterable, command = None):
"""Create new pipe with iterable object.
"""
self._inner = iterable
self._command = command
def __iter__(self):
for i in self._inner:
if self._command is None:
yield i
else:
for j in self._command.execute(i):
yield j
if self._command is not None:
for j in self._command.finalize():
yield j
def list(self):
"""Convert as list.
>>> wrap([1, 2, 3]).list()
[1, 2, 3]
"""
return list(self)
def pipe(self, command):
"""Create new instance with pipe command.
wrap([1, 2, 3]).filter(lambda e: (e, e * 2)).list()
[1, 2, 2, 4, 3, 6]
"""
if isinstance(command, Command):
return Pipe(self, command)
if callable(command):
return Pipe(self, CallbackCommand(command))
raise TypeError('invalid command')
def filter(self, callback):
"""Filter. Elements callback returns true are survive.
>>> wrap([1, 2, 3]).filter(lambda e: e % 2 == 1).list()
[1, 3]
"""
command = lambda e: (e,) if callback(e) else ()
return Pipe(self, CallbackCommand(command))
def map(self, callback):
"""Map callback all elements.
>>> wrap([1, 2, 3]).map(lambda e: e * 2).list()
[2, 4, 6]
"""
command = lambda e: (callback(e),)
return Pipe(self, CallbackCommand(command))
def wrap(iterable):
"""Create new iterator pipe.
>>> (wrap([1, 2, 3, 4, 5]).filter(lambda e: e % 2 == 1)
... .map(lambda e: e * 2).list())
[2, 6, 10]
"""
return Pipe(iterable)
class Command(object):
"""Pipe command interface.
>>> class Counter(Command):
... def __init__(self): self._count = 0
... def execute(self, element): self._count += 1; return ()
... def finalize(self): return (self._count,)
>>> wrap([2, 4, 6]).pipe(Counter()).list()
[3]
"""
def execute(self, element):
"""Execute an element, return iteratable.
"""
return (element,)
def finalize(self):
"""Finalize loop, return iteratable.
"""
return ()
class CallbackCommand(Command):
def __init__(self, callback):
self._callback = callback
def execute(self, element):
return self._callback(element)
def _test():
import doctest
doctest.testmod()
if __name__ == '__main__':
_test()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment