"""Lazily-evaluated stream with pipelining via the '>>' operator
Introduction
============
Streams are generalized iterators with a pipelining mechanism to enable
data-flow programming.
The idea is to take the output of a function that turn an iterable into
another iterable and plug that as the input of another such function.
While you can already do this using function composition, this package
provides an elegant notation for it by overloading the '>>' operator.
This approach focuses the programming on processing streams of data, step
by step. A pipeline usually starts with a generator, then passes through
a number of filters. Multiple streams can be branched and combined.
Finally, the output is fed to an accumulator, which can be any function
of one iterable argument.
**Generators**: anything iterable
+ from this module: seq, gseq, repeatcall, chaincall
**Filters**:
+ by index: take, drop, cut
+ by condition: filter, takewhile, dropwhile
+ by transformation: map, apply, fold
+ special purpose: attrgetter, itemgetter, methodcaller, splitter
**Combinators**: prepend, takei, dropi, tee, flatten
**Accumulators**: item, maximum, minimum, reduce
+ from Python: list, sum, dict, max, min ...
take() and item[] work similarly, except for notation and the fact that
item[] returns a list whereas take() returns a stream which can be further
piped to another filter.
Values are computed only when an accumulator forces some or all evaluation
(not when the stream are set up).
Examples
========
Better itertools.islice
-----------------------
::
>>> from itertools import count
>>> c = count()
>>> c >> item[1:10:2]
[1, 3, 5, 7, 9]
>>> c >> item[:5]
[10, 11, 12, 13, 14]
String processing
-----------------
Grep some lines matching a regex from a file, cut out the 4th field
separated by ' ', ':' or '.', strip leading zeroes, then save as a list::
import re
s = open('file') \
>> filter(re.compile(regex).search) \
>> map(splitter(' |:|\.')) \
>> map(itemgetter(3)) \
>> map(methodcaller('lstrip', '0')) \
>> list
Partial sums
------------
Compute the first few partial sums of the geometric series 1 + 1/2 + 1/4 + ..::
>>> gseq(0.5) >> fold(operator.add) >> item[:5]
[1, 1.5, 1.75, 1.875, 1.9375]
Random Walk in 2D
-----------------
Generate an infinite stream of coordinates representing the position of
a random walker in 2D::
from random import choice
vectoradd = lambda u,v: zip(u, v) >> apply(operator.add) >> list
directions = [[1,0], [0,1], [-1,0], [0,-1]]
rw = lambda: repeatcall(choice, directions) >> fold(vectoradd, [0, 0])
Calling choice repeatedly yields the series of unit vectors representing the
directions that the walker takes, then these vectors are gradually added to get
a series of coordinates.
To instantiate a random-walk, and get the first 10 coordinates::
walk = rw()
walk >> item[:10]
Question: what is the farthest point that the walker wanders upto the first
return to the origin? (Note that he might never return at all!)::
vectorlen = lambda v: v >> map(lambda x: x**2) >> sum
rw() >> drop(1) >> takewhile(lambda v: v != [0, 0]) >> maximum(key=vectorlen)
The first coordinate [0, 0], which is the origin, needs to be dropped otherwise
takewhile will truncate immediately.
We can also probe into the walker's chosen path::
probe = Stream()
rw() >> drop(1) >> takewhile(lambda v: v != [0, 0]) >> tee(probe) >> maximum(key=vectorlen)
Now you can see his exact coordinates, for example the first 10 are::
probe >> item[:10]
"""
__version__ = '0.6'
__author__ = 'Anh Hai Trinh'
__email__ = 'moc.liamg@hnirt.iah.hna:otliam'[::-1]
__all__ = [
'Stream',
'Filter',
'take',
'takeall',
'item',
'takei',
'drop',
'dropi',
'apply',
'map',
'cut',
'filter',
'fold',
'takewhile',
'dropwhile',
'tee',
'prepend',
'flatten',
'zipwith',
'seq',
'gseq',
'repeatcall',
'chaincall',
'itemgetter',
'attrgetter',
'methodcaller',
'splitter',
'maximum',
'minimum',
'reduce',
'itertools',
'operator',
]
import __builtin__
import collections
import itertools
import operator
import re
import string
from operator import itemgetter, attrgetter, methodcaller
#_____________________________________________________________________
#
# Base class for Stream
#_____________________________________________________________________
class BrokenPipe(Exception): pass
class Stream(collections.Iterator):
"""A class representing both a stream and a filter.
The outgoing stream is represented by the attribute 'iterator'.
The filter is represented by the method __call__(inpipe), which
combines self's iterator with inpipe's, returning a new iterator
representing a new outgoing stream.
A Stream subclass will usually implement __call__, unless it is an
accumulator and will not return a Stream, in which case it needs to
implement __pipe__. The default piping mechanism of Stream is appending
to the end of the its input (which had better terminate!).
>>> [1, 2, 3] >> Stream('foo') >> Stream('bar') >> list
[1, 2, 3, 'f', 'o', 'o', 'b', 'a', 'r']
"""
__slots__ = 'iterator'
def __init__(self, iterable=None):
"""Make a stream object from an interable"""
self.iterator = iter(iterable if iterable else [])
def __iter__(self):
return self.iterator
def next(self):
return next(self.iterator)
def __call__(self, inpipe):
"""Append to the end of inpipe (it had better terminate!)."""
return itertools.chain(inpipe, self.iterator)
def __pipe__(self, inpipe):
self.iterator = self.__call__(inpipe)
return self
@staticmethod
def pipe(inpipe, outpipe):
if hasattr(outpipe, '__pipe__'):
return outpipe.__pipe__(iter(inpipe))
elif hasattr(outpipe, '__call__'):
if hasattr(outpipe, '__name__') and outpipe.__name__ == 'list':
## For some reason `list` doesn't believe that inpipe is an iterator
return outpipe(iter(inpipe))
else:
return outpipe(inpipe)
else:
raise BrokenPipe('No connection mechanism defined')
def __rshift__(self, outpipe):
return Stream.pipe(self, outpipe)
def __rrshift__(self, inpipe):
return Stream.pipe(inpipe, self)
def __extend__(self, inpipe):
"""
Similar to __pipe__, except for the fact that both
self and inpipe must be Stream instances, in which case
inpipe.iterator is modified in place.
"""
inpipe.iterator = self.__call__(inpipe.iterator)
return inpipe
@staticmethod
def extend(inpipe, outpipe):
if hasattr(outpipe, '__extend__'):
return outpipe.__extend__(inpipe)
def __lshift__(self, outpipe):
return Stream.extend(self, outpipe)
def __len__(self): ### this will force all evaluation
"""
>>> Stream(range(20)) >> len
20
"""
return len(list(self.iterator))
def __repr__(self):
return 'Stream(%s)' % repr(self.iterator)
#_______________________________________________________________________
#
# Filtering streams by element indices
#_______________________________________________________________________
negative = lambda x: x and x<0 ### since None < 0 == True
class take(Stream):
"""Force some or all evaluation and use slice-like arguments to select elements.
Return a Stream.
>>> seq(1, 2) >> take(10)
Stream([1, 3, 5, 7, 9, 11, 13, 15, 17, 19])
>>> gseq(2) >> take(0, 16, 2)
Stream([1, 4, 16, 64, 256, 1024, 4096, 16384])
"""
__slots__ = 'items', 'slice'
def __init__(self, *args):
super(take, self).__init__()
self.slice = slice(*args)
self.items = []
def __call__(self, inpipe):
if negative(self.slice.stop) or negative(self.slice.start) \
or not (self.slice.start or self.slice.stop) \
or (not self.slice.start and negative(self.slice.step)) \
or (not self.slice.stop and not negative(self.slice.step)):
## force all evaluation ##
self.items = list(inpipe)
else:
## force some evaluation ##
if negative(self.slice.step):
stop = self.slice.start
else:
stop = self.slice.stop
try:
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
![preview](https://csdnimg.cn/release/downloadcmsfe/public/img/white-bg.ca8570fa.png)
共5个文件
py:2个
pkg-info:1个
license:1个
![preview-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/scale.ab9e0183.png)
资源分类:Python库 所属语言:Python 资源全名:stream-0.6.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
资源推荐
资源详情
资源评论
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![whl](https://img-home.csdnimg.cn/images/20210720083646.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
收起资源包目录
![package](https://csdnimg.cn/release/downloadcmsfe/public/img/package.f3fc750b.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
共 5 条
- 1
资源评论
![avatar-default](https://csdnimg.cn/release/downloadcmsfe/public/img/lazyLogo2.1882d7f4.png)
![avatar](https://profile-avatar.csdnimg.cn/277f6345dca0446498fbbc03843436aa_qq_38161040.jpg!1)
挣扎的蓝藻
- 粉丝: 13w+
- 资源: 15万+
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
最新资源
- labelImg安装指导书.docx
- 2023AI自有光-她经济消费新图鉴(2023)-百度营销.pdf
- 2022中国新能源汽车内容生态趋势洞察(1).pdf
- Docker技术:Docker安装与配置教程+运维技术+超融合+虚拟技术+云计算
- ZeRO Memory Optimizations Toward Training LLM.pdf
- 高效SQL语句编写(how-to-write-efficient-sql)
- ZeroTermux-release_sign.apk
- 非弹性斜碰撞物理课件模拟-HTML网页制作
- 软件测试学习日志-测试基础-day02
- 制作一个简单的进销存(库存管理)页面.rar
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback-tip](https://img-home.csdnimg.cn/images/20220527035111.png)
安全验证
文档复制为VIP权益,开通VIP直接复制
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)