# creek
Simple streams facade.
To install: ```pip install creek```
[Documentation here](https://i2mint.github.io/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:
```pydocstring
>>> 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).
```pydocstring
>>> 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
Raw data
{
"_id": null,
"home_page": "https://github.com/i2mint/creek",
"name": "creek",
"maintainer": null,
"docs_url": null,
"requires_python": null,
"maintainer_email": null,
"keywords": "streams, facade",
"author": "OtoSense",
"author_email": null,
"download_url": "https://files.pythonhosted.org/packages/90/35/e78d900f027ee4ac1391769e2b6a673a77c4893bff08da15b9a5e3bef804/creek-0.1.33.tar.gz",
"platform": "any",
"description": "# creek\nSimple streams facade.\n\nTo install:\t```pip install creek```\n\n[Documentation here](https://i2mint.github.io/creek/)\n\nThe ``Creek`` base class offsers a layer-able wrap of the stream interface.\n\nThere are three layering methods -- pre_iter, data_to_obj, and post_filt\n-- whose use is demonstrated in the iteration code below:\n\n```\nfor line in self.pre_iter(self.stream): # pre_iter: prepare and/or filter the stream\n obj = self.data_to_obj(line) # data_to_obj: Transforms the data that stream yields\n if self.post_filt(obj): # post_filt: Filters the stream further (but based on object now)\n yield obj\n```\n\nExamples:\n\n\n\n```pydocstring\n>>> from io import StringIO\n>>> src = StringIO(\n... '''a, b, c\n... 1,2, 3\n... 4, 5,6\n... '''\n... )\n>>>\n>>> from creek import Creek\n>>>\n>>> class MyCreek(Creek):\n... def data_to_obj(self, line):\n... return [x.strip() for x in line.strip().split(',')]\n...\n>>> stream = MyCreek(src)\n>>>\n>>> list(stream)\n[['a', 'b', 'c'], ['1', '2', '3'], ['4', '5', '6']]\n>>> stream.seek(0) # oh!... but we consumed the stream already, so let's go back to the beginning\n0\n>>> list(stream)\n[['a', 'b', 'c'], ['1', '2', '3'], ['4', '5', '6']]\n>>> stream.seek(0) # reverse again\n0\n>>> next(stream)\n['a', 'b', 'c']\n>>> next(stream)\n['1', '2', '3']\n```\n\nLet's add a filter! There's two kinds you can use.\nOne that is applied to the line before the data is transformed by data_to_obj,\nand the other that is applied after (to the obj).\n\n```pydocstring\n>>> from creek import Creek\n>>> from io import StringIO\n>>>\n>>> src = StringIO(\n... '''a, b, c\n... 1,2, 3\n... 4, 5,6\n... ''')\n>>> class MyFilteredCreek(MyCreek):\n... def post_filt(self, obj):\n... return str.isnumeric(obj[0])\n>>>\n>>> s = MyFilteredCreek(src)\n>>>\n>>> list(s)\n[['1', '2', '3'], ['4', '5', '6']]\n>>> s.seek(0)\n0\n>>> list(s)\n[['1', '2', '3'], ['4', '5', '6']]\n>>> s.seek(0)\n0\n>>> next(s)\n['1', '2', '3']\n```\n\nRecipes:\n- pre_iter: involving itertools.islice to skip header lines\n- pre_iter: involving enumerate to get line indices in stream iterator\n- pre_iter = functools.partial(map, line_pre_proc_func) to preprocess all lines with line_pre_proc_func\n- pre_iter: include filter before obj\n",
"bugtrack_url": null,
"license": "apache-2.0",
"summary": "Simple streams facade",
"version": "0.1.33",
"project_urls": {
"Homepage": "https://github.com/i2mint/creek"
},
"split_keywords": [
"streams",
" facade"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "affd23b10723dfa57c402949ef46059fda5eadaf6c636d48ff320421491e8ddc",
"md5": "04f25081f74e34c2d0569f0215285e65",
"sha256": "1567e59a8768226a87753a0b1597277c880e612831bc83a3418adacb36152555"
},
"downloads": -1,
"filename": "creek-0.1.33-py3-none-any.whl",
"has_sig": false,
"md5_digest": "04f25081f74e34c2d0569f0215285e65",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 40075,
"upload_time": "2024-09-03T09:38:34",
"upload_time_iso_8601": "2024-09-03T09:38:34.500925Z",
"url": "https://files.pythonhosted.org/packages/af/fd/23b10723dfa57c402949ef46059fda5eadaf6c636d48ff320421491e8ddc/creek-0.1.33-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "9035e78d900f027ee4ac1391769e2b6a673a77c4893bff08da15b9a5e3bef804",
"md5": "eecd39e78d07e012143843f7a47432b5",
"sha256": "bda1eb77fcee37dc97c24024329bef56a563357d152027891f60e5bfa2a15812"
},
"downloads": -1,
"filename": "creek-0.1.33.tar.gz",
"has_sig": false,
"md5_digest": "eecd39e78d07e012143843f7a47432b5",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 35501,
"upload_time": "2024-09-03T09:38:35",
"upload_time_iso_8601": "2024-09-03T09:38:35.608953Z",
"url": "https://files.pythonhosted.org/packages/90/35/e78d900f027ee4ac1391769e2b6a673a77c4893bff08da15b9a5e3bef804/creek-0.1.33.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-09-03 09:38:35",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "i2mint",
"github_project": "creek",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "creek"
}