streams is a python library to use Java-like stream objects to manipulate an iterable collection lazily. In addition it offers elementwise exception handling.
To install the latest release:
$ py -m pip install https://github.com/ramsteak/streams/releases/download/v1.0.1/streams-1.0.1-py3-none-any.whl
To install the most up-to-date changes:
$ py -m pip install git+https://github.com/ramsteak/streams.git
from streams import Stream
stream = (Stream.range(10)
.filter(lambda x:x%2))
stream_list = stream.list()
>>> [1, 3, 5, 7, 9]
The stream object is able to catch exceptions at the item level without interrupting the stream, and the exceptions can be handled with the .exc()
method.
stream = (Stream.range(3)
.eval(lambda x:1/x)
.exc(ZeroDivisionError, 'replace', float('inf')))
stream_list = stream.list()
>>> [inf, 1.0, 0.5]
Unhandled exceptions will be raised after calling any other method.
stream = (Stream.range(3)
.eval(lambda x:1/x))
stream_list = stream.list()
>>> ZeroDivisionError: division by zero
See test_use_cases.py for some examples.