creek


Namecreek JSON
Version 0.1.29 PyPI version JSON
download
home_pagehttps://github.com/i2mint/creek
SummarySimple streams facade
upload_time2023-02-03 11:15:23
maintainer
docs_urlNone
authorOtoSense
requires_python
licenseapache-2.0
keywords streams facade
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # 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": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "streams,facade",
    "author": "OtoSense",
    "author_email": "",
    "download_url": "https://files.pythonhosted.org/packages/4d/f1/c00cdbe049dbab1193de1fa3bdde00e7558731dd2c18d6c0c2200fa18f03/creek-0.1.29.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",
    "bugtrack_url": null,
    "license": "apache-2.0",
    "summary": "Simple streams facade",
    "version": "0.1.29",
    "split_keywords": [
        "streams",
        "facade"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "4df1c00cdbe049dbab1193de1fa3bdde00e7558731dd2c18d6c0c2200fa18f03",
                "md5": "84c230344b47721ebcbb582f904b206d",
                "sha256": "26bf8475a6a7944c6f4dfead6b7e426eda1fdf4a4867920c73240c2a5907a030"
            },
            "downloads": -1,
            "filename": "creek-0.1.29.tar.gz",
            "has_sig": false,
            "md5_digest": "84c230344b47721ebcbb582f904b206d",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 32660,
            "upload_time": "2023-02-03T11:15:23",
            "upload_time_iso_8601": "2023-02-03T11:15:23.122732Z",
            "url": "https://files.pythonhosted.org/packages/4d/f1/c00cdbe049dbab1193de1fa3bdde00e7558731dd2c18d6c0c2200fa18f03/creek-0.1.29.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-02-03 11:15:23",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": "i2mint",
    "github_project": "creek",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "creek"
}
        
Elapsed time: 0.03640s