Simple streams facade
Project description
creek
Simple streams facade.
To install: pip install creek
The Creek
base class offsers a layer-able wrap of the stream interface.
There are three layering methods -- pre_iter, data_to_obj, and post_filt -- whose use is demonstrated in the iteration code below:
for line in self.pre_iter(self.stream): # pre_iter: prepare and/or filter the stream
obj = self.data_to_obj(line) # data_to_obj: Transforms the data that stream yields
if self.post_filt(obj): # post_filt: Filters the stream further (but based on object now)
yield obj
Examples:
>>> from io import StringIO
>>> src = StringIO(
... '''a, b, c
... 1,2, 3
... 4, 5,6
... '''
... )
>>>
>>> from creek import Creek
>>>
>>> class MyCreek(Creek):
... def data_to_obj(self, line):
... return [x.strip() for x in line.strip().split(',')]
...
>>> stream = MyCreek(src)
>>>
>>> list(stream)
[['a', 'b', 'c'], ['1', '2', '3'], ['4', '5', '6']]
>>> stream.seek(0) # oh!... but we consumed the stream already, so let's go back to the beginning
0
>>> list(stream)
[['a', 'b', 'c'], ['1', '2', '3'], ['4', '5', '6']]
>>> stream.seek(0) # reverse again
0
>>> next(stream)
['a', 'b', 'c']
>>> next(stream)
['1', '2', '3']
Let's add a filter! There's two kinds you can use. One that is applied to the line before the data is transformed by data_to_obj, and the other that is applied after (to the obj).
>>> from creek import Creek
>>> from io import StringIO
>>>
>>> src = StringIO(
... '''a, b, c
... 1,2, 3
... 4, 5,6
... ''')
>>> class MyFilteredCreek(MyCreek):
... def post_filt(self, obj):
... return str.isnumeric(obj[0])
>>>
>>> s = MyFilteredCreek(src)
>>>
>>> list(s)
[['1', '2', '3'], ['4', '5', '6']]
>>> s.seek(0)
0
>>> list(s)
[['1', '2', '3'], ['4', '5', '6']]
>>> s.seek(0)
0
>>> next(s)
['1', '2', '3']
Recipes:
- pre_iter: involving itertools.islice to skip header lines
- pre_iter: involving enumerate to get line indices in stream iterator
- pre_iter = functools.partial(map, line_pre_proc_func) to preprocess all lines with line_pre_proc_func
- pre_iter: include filter before obj
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
creek-0.1.33.tar.gz
(35.5 kB
view details)
Built Distribution
creek-0.1.33-py3-none-any.whl
(40.1 kB
view details)
File details
Details for the file creek-0.1.33.tar.gz
.
File metadata
- Download URL: creek-0.1.33.tar.gz
- Upload date:
- Size: 35.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.14
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | bda1eb77fcee37dc97c24024329bef56a563357d152027891f60e5bfa2a15812 |
|
MD5 | eecd39e78d07e012143843f7a47432b5 |
|
BLAKE2b-256 | 9035e78d900f027ee4ac1391769e2b6a673a77c4893bff08da15b9a5e3bef804 |
File details
Details for the file creek-0.1.33-py3-none-any.whl
.
File metadata
- Download URL: creek-0.1.33-py3-none-any.whl
- Upload date:
- Size: 40.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.14
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1567e59a8768226a87753a0b1597277c880e612831bc83a3418adacb36152555 |
|
MD5 | 04f25081f74e34c2d0569f0215285e65 |
|
BLAKE2b-256 | affd23b10723dfa57c402949ef46059fda5eadaf6c636d48ff320421491e8ddc |